I am having trouble with changing the state of ImageView
which marks list items as bookmarked.
People have mentioned to create a new OnClickListener()
which I have but when I click on the ImageView it does go to the focused state but when you finish clicking it does not change to the pressed state image. Furthermore, when I click on the list item I notice that the ImageView
is focused again. I am not sure what I am missing:
public class DxSimpleCursorAdapter extends SimpleCursorAdapter {
Context context;
Activity activity;
ImageView image;
public DxSimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.context=context;
this.activity=(Activity) context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View view = super.getView(position, convertView, parent);
long id = getItemId(position);
final ImageView image = (ImageView) view.findViewById(R.id.fav);
image.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
image.setBackgroundResource(R.drawable.ic_fav);
final Button btn = (Button) v.findViewById(R.id.fav);
btn.setPressed(true);
}
});
return view;
}
}
I have created the xml for the ImageView state as follows:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_star_off_normal"
android:state_checked="false"
android:state_window_focused="false" />
<item android:drawable="@drawable/btn_star_on_normal"
android:state_checked="true"
android:state_window_focused="false" />
<item android:drawable="@drawable/btn_star_on_pressed"
android:state_checked="true"
android:state_pressed="true" />
<item android:drawable="@drawable/btn_star_off_pressed"
android:state_checked="false"
android:state_pressed="true" />
<item android:drawable="@drawable/btn_star_on_focused"
android:state_checked="true"
android:state_focused="true" />
<item android:drawable="@drawable/btn_star_off_focused"
android:state_checked="false"
android:state_focused="true" />
<item android:state_checked="false" android:drawable="@drawable/btn_star_off_normal" />
<item android:state_checked="true" android:drawable="@drawable/btn_star_on_normal" />
</selector>
Within my activity I have created the following OnListItemClick()
to create a toast message which pops up but also seems to put focus on the ImageView
.
public void onListItemClick(ListView parent, View view, int position, long id) {
SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
Cursor c = (Cursor) getListAdapter().getItem(position);
String arg = c.getString(c.getColumnIndex("_id"));
Cursor cursor = db.rawQuery("Select category, subcategory From DiagLookup Where _id = ?", new String[]{""+arg});
cursor.moveToFirst();
Context context = getApplicationContext();
CharSequence text = "Category: " + cursor.getString(cursor.getColumnIndex("category")) + "\nSubcategory: " + cursor.getString(cursor.getColumnIndex("subcategory"));
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
CLARIFICATION on what I want to happen:
I have list items which I want to do two things; click on the item and get a toast message and click on a imageview and toggle the imageview to a bookmarked state in image and also in the backend.
At the moment I have the toast message working but the imageview is not entirely working...
I have attached a screenshot of a list item to show how it is setup and explain what is occurring when I click on the various areas.
The top list item has been clicked on and held, what I get is the image changes to the focussed but unclicked image as shown in my drawable xml above but when I finish the click the star gets darker grey then the other entries but it does not turn to the clicked state image which is a yellow star.
When I click anywhere else and not on the star then the toast message pops up which is what I intended to occur but I also notice that the star becomes focussed as well. I feel like I am having some inheritance issue and also not getting the state to stick for the ImageView.