I am creating an list view with dynamically added checkboxes. However i want to add a onClickListener to it so when i change the state of a certain checkbox, i do something. However i cannot seem to find out how to create Uniqe ID's from an array.
this is my code:
public class SingleContactActivity extends Activity implements
OnCheckedChangeListener {
private int Array_Count = 0;
private String url = "http://www.EXAMPLE.com";
private JSONArray items = null;
private String product;
private String id;
private String store;
private String state;
private String uniqeID;
ArrayList<HashMap<String, String>> listitems;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_contact);
listitems = new ArrayList<HashMap<String, String>>();
// getting intent data
// Get JSON values from previous intent
new downloadJsonitems().execute();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
}
private class downloadJsonitems extends AsyncTask<Void, Void, Void> {
LinearLayout my_layout = (LinearLayout) findViewById(R.id.test);
LinearLayout my_checked_layout = (LinearLayout) findViewById(R.id.checked);
@Override
protected Void doInBackground(Void... arg0) {
ServiceHandler sh = new ServiceHandler();
url = url + "1";
Log.i("pref", "url =" + url);
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
JSONObject jsonObj;
try {
jsonObj = new JSONObject(jsonStr);
Log.i("pref", "json =" + jsonObj);
items = jsonObj.getJSONArray("items");
for (int x = 0; x < items.length(); x++) {
JSONObject lists = items.getJSONObject(x);
id = lists.getString("primaryKey");
store = lists.getString("store");
product = lists.getString("items");
state = lists.getString("state");
uniqeID = lists.getString("uniqeID");
HashMap<String, String> contacts = new HashMap<String, String>();
contacts.put("id", id);
contacts.put("store", store);
contacts.put("product", product);
contacts.put("state", state);
contacts.put("cbid", uniqeID);
Log.i("pref", "" + listitems);
listitems.add(contacts);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
for (int n = 0; n < listitems.size(); n++) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setId(Integer.parseInt(listitems.get(n).get("cbid")));
cb.setText(listitems.get(n).get("product"));
cb.setTextColor(Color.BLACK);
if (listitems.get(n).get("state").toString().equals("true")) {
cb.setChecked(true);
my_checked_layout.addView(cb);
cb.setBackgroundColor(Color.argb(200, 8, 242, 2));
cb.setTextColor(Color.parseColor("#CFCFCF"));
} else {
cb.setChecked(false);
cb.setBackgroundColor(Color.argb(100, 5, 214, 0));
cb.setTextColor(Color.parseColor("#F7F7F7"));
my_layout.addView(cb);
}
}
Intent in = getIntent();
String name = in.getStringExtra(MainActivity.TAG_FIRSTNAME);
String email = in.getStringExtra(MainActivity.TAG_EMAIL);
String list = in.getStringExtra(MainActivity.TAG_LIST);
String[] seperatedString = list.split(", ");
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblEmail = (TextView) findViewById(R.id.email_label);
// Array_Count=seperatedString.length;
LinearLayout my_layout = (LinearLayout) findViewById(R.id.test);
/*
* for (int i = 0; i < Array_Count; i++) { TableRow row =new
* TableRow(null); row.setId(i); row.setLayoutParams(new
* LayoutParams
* (LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); CheckBox
* checkBox = new CheckBox(getActivity());
* checkBox.setOnCheckedChangeListener(this); checkBox.setId(i);
* checkBox.setText(seperatedString[i]); row.addView(checkBox);
* my_layout.addView(row); }
*/
lblName.setText(name);
lblEmail.setText(email);
}
The following is my XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<!-- Name Label -->
<TextView android:id="@+id/name_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dip"
android:textStyle="bold"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:textColor="#43bd00"/> <!-- green -->
<!-- Email Label -->
<TextView
android:id="@+id/email_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#EB0505" /> <!-- red -->
<!-- Mobile Label -->
<TextView
android:id="@+id/mobile_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#E5FF00"
android:textStyle="bold"
android:text="Empty, Mobile_label"/> <!-- yellow -->
<TextView
android:id="@+id/list_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FF00FF" /> <!-- pink -->
<LinearLayout
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
<LinearLayout
android:id="@+id/checked"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
What i thought would work is just add a number behind the id's
so my could would like like this:
Checkbox cb[n] = new Checkbox(getApplicationContext());
cb[n].setId(...);
cb[n].onClickListener() = new OnClickListener(){
//TODO add unimplemented code
}
But this didn't seem to work out... Any suggestions?