I have what I consider to be a strange dilemma, although YMMV.
I’m using a layout file that describes each line/row in a ListView (nothing too exotic about that). I have an id assigned to each one, such as:
android:id=”@+id/checkBox1″
android:id=”@+id/checkBox2″
android:id=”@+id/checkBox3″
android:id=”@+id/contactLabel” // a TextView
Now this doesn’t seem to make sense, as these ids should be unique, so what is the id of the second
row? That is, if “row 1” honors the specified ids of checkbox1, checkbox2, checkbox3, and contactLabel, what would the “row 2” ids be?
I’m curious, but also I need to know because I want to save the values of the checkboxes to a SharedPreferences object.
Who has a clue about how to get around this?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Update
The first thing I need to solve is how to respond to a click on the ListView. This is my current conundrum related to all of this:
ListView doesn’t know it’s been clicked, or won’t tell me
I’ve added this event handler to my ListActivity:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
…but it’s not getting called. I click on the Contacts that display, but no go.
I also tried it this way:
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_LONG)
.show();
}
});
…still no joy…I put breakpoints on both “Toast” lines, and they never get reached.
I read here:
http://www.vogella.de/articles/AndroidListView/article.html#listsactivity_layout
…that, “In case you need more the just a ListView in your Activity, you can use you own layout for
ListActivity.”
…which I do, because I add a header and a footer in addition to the listview.
It goes on to say, “In this case your layout must have an ListView element with the android:id
attribute set to @android:id/list.”
So I added this to my layout:
…but it makes no difference one way or the other.
The ID’s for the items within the
ListViewwidget are referenced through their parent view when you inflate it in yourgetView()method.To elaborate, you would have something like this is you
ListViewadapter.Nown, a new view instance exists as the convertView. You can access your widgets using
covertView.findViewById(R.id.checkBox1),convertView.findViewById(R.id.checkBox2), etc.Each of these views is a child of your
ListView. You can reference each individual view from yourListViewusing thegetChildCount()andgetChildAt()methods from theListViewobject.However, since it is recommended to use the
convertViewview to recycle views, in that case you will only have reference to the views on screen at a time.Also, with regards to the
SharedPreferences, all the views in yourListVieware populated by anAdaptersubclass which would be the actual object that puts the values in theCheckboxandTextViewwidgets. ThisAdapterhas a dataset that you provide it. Why not reference the values from the dataset directly, instead of trying to find them from the list items which are populated from the dataset in any case ? You can write to the dataset from theListViewwhen someone clicks aCheckBoxso you have an easy ordered reference to all the items in theListView.UPDATE: Added dummy source code
OK. Let’s start with a hypothical list. We want to display say five items on the list. For simplicity, I’ll assume each has a
TextViewand aCheckbox. So my container class is:Now in my
Activitywhere I want to display this list, I put anArrayListof items (you can use just about any datastructure) as a class variable. Then I get the ListView reference and assign it an adapter.When you want to retrieve what items were clicked, just iterate through the Item class and find which ones are true or you perform whatever action you want within the
Listenersince you have a reference identifying individual members of the ListView dataset (here listItems ArrayList).Apologies for any errors. Didn’t do any checking.