简体   繁体   中英

ImageView onClickListener()

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.

带有ImageView可点击的ListItem

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.

So what you're saying is that you want a Toast message to appear when you click the image and that is all you want happening right?

If so, here's a way that I would do it.

in you XML, you need to first make sure that you have set the onClick onto your buttons or images. You need to add this to each variable

android:onClick = "Image1"

Then in your java file, you need to refer to it. You may want to implement the OnClickListener into your Activity

public class DxSimpleCursorAdapter extends SimpleCursorAdapter implements OnClickListener{

Then refer to it in your java file

public void Image1 (View v) {
    Toast.makeText(this, R.string.TEXT, Toast.LENGTH_SHORT).show();
}

You can do your method of creating the toast but mine only contains 1 line of code.

I'm not sure if this completely answers your question but it's all I know about coding for Android (Java) so far. I hope this helps.

Check this link out.

https://stackoverflow.com/a/9754248/1244489

Its a similar problem. Its a little more complicated than the way you have implemented.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM