Reputation: 115
I have a class that extends ListActivity and contains a ListView that is populated via an ArrayList of objects and a custom Adapter I have created that subclasses ArrayAdapter.
I would like one field of the object to reference whether the object row should be checked or unchecked.
Then when the ListView gets populated some of the rows will be pre-selected (checked)
I have overriden ArrayAdapter.getView() so that I can populate my layout via an object.
I thought that it would be here that I would set the row to be checked or not - but I cannot come up with a solution - any ideas?
here is my getView() code:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.contactedit_onerow, null);
}
ContactVO mObject = items.get(position);
if (mObject != null) {
CheckedTextView nameV = (CheckedTextView) convertView.findViewById(R.id.rowText1);
TextView phoneV = (TextView) convertView.findViewById(R.id.rowText2);
TextView emailV = (TextView) convertView.findViewById(R.id.rowText3);
TextView headerV = (TextView) convertView.findViewById(R.id.alphaHeader);
if (alphaIndexer.get(mObject.name.substring(0, 1).toUpperCase()) == position ) {
headerV.setText(mObject.name.substring(0,1));
headerV.setVisibility(View.VISIBLE);
} else {
headerV.setText("");
headerV.setVisibility(View.GONE);
}
nameV.setText(mObject.name);
phoneV.setText("Phone: " + mObject.phone);
emailV.setText("Email: " + (mObject.email != null ? mObject.email : ""));
// THE FOLLOWING LINE UNCOMMENTED DOES NOTHING
// (any suggestions to make it work)
// nameV.setChecked(true);
}
return convertView;
}
Thanks in advance!
Upvotes: 1
Views: 5093
Reputation: 115
I still don't know how to pre-select items as the ListView is getting populated by the adapter, thus avoiding have to later iterate over the ArrayList data source.
However here is a solution that should work with a ListView backed by an ArrayList of objects:
// In an activity that extends ListActivity
// With a CustomAdapter that extends ArrayAdpater
ArrayList arrayList = createArrayListOfObjectsForListView();
CustomAdapter mAdapter = new CustomAdapter(this, R.layout.mLayout, arrayList);
setListAdapter(mAdapter);
// Iterate over arrayList to check if item/row should be pre selected in the ListView
int j = 0;
for (Object item : arrayList) {
// Check a field/condition in the object
if (item.selected == true) {
listView.setItemChecked(j,true);
}
j++;
}
Alternatively I was personally checking if my Object's id matched a set of selected ids so my for loop looked like this:
int j = 0;
for (Object item : arrayList) {
if (mySet.contains(item.id)) {
listView.setItemChecked(j, true);
}
j++;
}
Upvotes: 4
Reputation: 1
Hi Friends finally got it working .. Here you go
I just had a ListView with ImageView, textview and CheckedTextView. I am displaying the installed apps in device.
In my Listview Activity
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
/*Your Code */
myAdapter.setPosition(position);
myAdapter.notifyDataSetChanged();
}
In your List Adapter class
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
holder.ctv = (CheckedTextView) convertView
.findViewById(android.R.id.text1);
/*Your Code */
if(selectedPos == position){
holder.ctv.setChecked(true);
}else{
holder.ctv.setChecked(false);
}
return convertView;
}
That should work fine. Its working fine for me after long try.. I've mentioned everything in shortcut. Please let me know if any one requires further help with this.
For Single TextView it is easier as we just do this ---
ArrayAdapter<String> lAdapter = new ArrayAdapter<String>(this,
R.layout.mylayout, android.R.id.text1, String array[]);
And a Class
public class CheckedLinearLayout extends LinearLayout/RelativeLayout
implements Checkable { //Class body with its methods }
But for Custom Adapter I found the way I just mentioned....
Upvotes: 0
Reputation: 7505
What I did with the SimpleCursorAdapter in my project is temporary set a SimpleCursorAdapter.ViewBinder. The method ViewBinder.setViewValue(...) is called when the list items become visible on the screen. Do the pre-selecting and remove itself.
final SimpleCursorAdapter sca = new SimpleCursorAdapter(this, layout, c, from, to);
mProjectListView.setAdapter(sca);
// Add temporary ViewBinder for pre-selected ListView item
// i.e. the current project should be selected
final ViewBinder viewBinder = new ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
String text = cursor.getString(columnIndex);
String currentProject = mPreferences.getString(PREF_NAME, sDefName);
if (currentProject.equals(text)) {
mProjectListView.setItemChecked(cursor.getPosition(), true);
// Remove itself
sca.setViewBinder(null);
}
// Let CursorAdapter handle the binding, so return false
return false;
}
};
sca.setViewBinder(viewBinder);
Unfortunately the ArrayAdapter doesn't have this listener. So i override ArrayAdapter.getItem(int position) and introduce OnGetItemListener interface.
public class CustomArrayAdapter<T> extends ArrayAdapter<T> {
public static interface OnGetItemListener<T> {
void onGetItem(int position, T item);
}
private OnGetItemListener<T> mGetItem = mNullGetItem;
public void setOnGetItemListener(OnGetItemListener<T> listener) {
if (listener != null) {
mGetItem = listener;
} else {
mGetItem = mNullGetItem;
}
}
@Override
public T getItem(int position) {
T item = super.getItem(position);
mGetItem.onGetItem(position, item);
return item;
}
}
Using the CustomArrayAdapter:
caa.setOnGetItemListener(new CustomArrayAdapter.OnGetItemListener<String>() {
@Override
public void onGetItem(int position, String item) {
if (item.equals(<is it your item?>)) {
mProjectListView.setItemChecked(position, true);
caa.setOnGetItemListener(null);
}
}
});
Upvotes: 0