I want to change an imageview in a listview based on some intent value. To do this I did override the class of simple cursor adapter as the following,
public class NotesAdapter extends SimpleCursorAdapter {
Intent intent;
public NotesAdapter(Context context, int layout, Cursor c, String[] from, int[] to, Intent i) {
super(context, layout, c, from, to);
intent=i;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = super.getView(position, convertView, parent);
ImageView image = (ImageView) item.findViewById(R.id.icon);
ImageView image2= (ImageView) item.findViewById(R.id.icon2);
if (intent.getStringExtra("category")!=null && intent.getStringExtra("category").equalsIgnoreCase("draft")){
image.setVisibility(View.GONE);
image2.setVisibility(View.VISIBLE);
}
return item;
}
}
In the activity which contains the list view I've defined NotesAdapter and initialize it inside the fragments onCreateView method as the following,
mSimpleCursorAdapter = new NotesAdapter(getActivity(), R.layout.notes_row, null, from, to, getActivity().getIntent());
This the code where I initialize the adapter,
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_todo_list, container, false);
mSimpleCursorAdapter = new NotesAdapter(getActivity(), R.layout.notes_row, null, from, to, getActivity().getIntent());
getLoaderManager().initLoader(LOADER_ID, null, this); //once this is done onCreateLoader will be called.
final ListView listView = (ListView) rootView.findViewById(R.id.notes_list); //findViewById must be called using the rootView because we are inside a fragment.
listView.addFooterView(new View(getActivity()), null, true);
text= (TextView) rootView.findViewById(R.id.empty_list);
listView.setAdapter(mSimpleCursorAdapter);
if(mSimpleCursorAdapter.isEmpty()) {
text.setVisibility(View.VISIBLE);
listView.setEmptyView(text);
if (getActivity().findViewById (R.id.fragment_container)!=null){
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id. fragment_container, new EmptyEditor()).commit();
}
}
else {
text.setVisibility(View.INVISIBLE);
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Cursor cursor = mSimpleCursorAdapter.getCursor();
if (cursor != null && cursor.moveToPosition(position)) {
String category= cursor.getString(1);
String summary= cursor.getString(2);
String description=cursor.getString(3);
long id= cursor.getLong(cursor.getColumnIndex(NotesContract.NotesTable._ID));
int locationId= cursor.getInt(cursor.getColumnIndex(NotesContract.NotesTable.COLUMN_LOCATION));
String [] retrievedData= {category, summary, description};
if (getActivity().findViewById (R.id.fragment_container)!=null){
//two pane layout
listView.setFocusable(true);
listView.setSelection(position);
Bundle args = new Bundle();
args.putStringArray("data",retrievedData);
/*args.putInt("update", 1);*/
args.putLong("id", id);
args.putInt("locationId", locationId);
mCallback.onlistElementClicked(args );/*this is available in the parent activity*/
}
else {
// one pane layout:
Intent intent = new Intent(getActivity(), NotesDetails.class);
intent.putExtra(Intent.EXTRA_TEXT, retrievedData);
/*intent.putExtra("update", 1); */ //to indicate that the query should be update not insert.
intent.putExtra("id", id);
intent.putExtra("locationId", locationId); //whether it is 0 or 1
startActivity(intent);
}
}//end outer cursor if.
}
});
return rootView;
}
This is the xml file for the row layout,
<?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"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:src="@drawable/staricon"
android:visibility="visible">
</ImageView>
<ImageView
android:id="@+id/icon2"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:src="@drawable/drafticon"
android:visibility="gone">
</ImageView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:lines="1"
android:text="@+id/TextView01"
android:textSize="24dp"
>
</TextView>
</LinearLayout>
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> <!--text will be set dynamically -->
</LinearLayout>
But there is a problem, I only want the image of specific item to be changed (based on the intent value), however when the image is changed all other images are changed. I think this happens because simplecursoradapter uses the same xml file for all items. But, I couldn't find a solution for that, it just keeps changing all imageviews. The item that I need to change its imageview is the last saved item. I have another activity in which the user creates notes, and specify their type (draft or important), when the user clicks save, the item is added to the list and the imageview should be changed based on the type (using intents), this is also applicable when the user edits an existing item in the list.
Can any one please suggest a solution for this problem?
Any help is highly appreciated.
Thank you.
I only want the image of specific item to be changed (based on the intent value)
Exactly so you have to compare your intent's value with this Item. And Item is based on position in ListView.
So just get data object from list based on position and compare it with Intent's value and change visibility of Imageview based on that.
And also add else part in getView()
method of Image visibility as opposite.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = super.getView(position, convertView, parent);
Cursor cursor = getCursor();
ImageView image = (ImageView) item.findViewById(R.id.icon);
ImageView image2= (ImageView) item.findViewById(R.id.icon2);
// Add one more comparison expression for data object position value which has draft value
if (cursor.getString(1).equalsIgnoreCase("draft")){
image.setVisibility(View.GONE);
image2.setVisibility(View.VISIBLE);
}else if (cursor.getString(1).equalsIgnoreCase("important"))
{
image.setVisibility(View.VISIBLE);
image2.setVisibility(View.GONE);
}
return item;
}