Change Text Of Items In Listview When It Is Displayed Using Simplecursoradapter
How to change text of items in ListView when it is displayed using SimpleCursorAdaptor? Here is my code. Cursor allTaskcursor = databaseHelper.getAllTasks(); String[] from = {'name
Solution 1:
If you want to update single list item and you know the index
of item, you can call getChildAt(int)
on the ListView
to get the view and update it like -
TextViewtext2= (TextView) v.findViewById(R.id.text2);
text2.setText("Updated Text");
Or If, you want to update all values in array, you can update the array and call notifyDataSetChanged
on adapter to reflect the updated values.
Solution 2:
SimpleCursorAdapter.ViewBinder did the job. As answered here, I changed the code to..
CursorallTaskcursor= databaseHelper.getAllTasks();
String[] from = {"name", "date"};
int[] to = newint[] {android.R.id.text1, android.R.id.text2};
SimpleCursorAdaptercursorAdapter=newSimpleCursorAdapter(context, android.R.layout.simple_list_item_2, allTaskcursor, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
cursorAdapter.setViewBinder(newSimpleCursorAdapter.ViewBinder() {
@OverridepublicbooleansetViewValue(View view, Cursor cursor, int columnIndex) {
if (view.getId() == android.R.id.text2) {
intgetIndex= cursor.getColumnIndex("date");
intdate= cursor.getInt(getIndex);
TextViewdateTextView= (TextView) view;
dateTextView.setText(date + " days");
returntrue;
}
returnfalse;
}
});
allTaskListView.setAdapter(cursorAdapter);
Post a Comment for "Change Text Of Items In Listview When It Is Displayed Using Simplecursoradapter"