Update/Edit Sqlite 数据库通过 onClick ImageView 使用 SimpleCursorAdapter

Update/Edit Sqlite Database using SimpleCursorAdapter via onClick ImageView

Illustration

请先看图..

如何获取值 "Test 1",然后我想将 sqlite 数据库中的列(收藏夹)更新为 "Yes"?

我一直在寻找适合我的案例的答案,但找不到与我的问题相符的答案,或者我可能不明白..:D

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_channel_list);

    channelList = this.findViewById(R.id.channelList);
    mDB = openDB();

    if (mDB != null) {
        mCsr = mDB.query(CHANNELTABLE,
                new String[]{KEY_NO + " AS _id",
                        KEY_NAME, KEY_CATEGORY, KEY_LOGO, KEY_SERVER1, KEY_SERVER2, KEY_SERVER3, KEY_SERVER4, KEY_SERVER5, KEY_LIKE
                },
                null,null,null,null,null);

        mSCA = new SimpleCursorAdapter(this,R.layout.custom_listview,mCsr,
                new String[]{KEY_NAME, KEY_CATEGORY, KEY_LOGO, KEY_LIKE},
                new int[]{R.id.channel_name, R.id.category, R.id.logo, R.id.like},0);

        mSCA.setViewBinder(new SimpleCursorAdapter.ViewBinder(){
            public boolean setViewValue(final View view, Cursor cursor, int columnIndex){
                if(view.getId() == R.id.logo){
                    channelName = cursor.getString(3);
                    int resID = getResources().getIdentifier(channelName, "drawable", getPackageName());
                    //Toast.makeText(Channel_List_Act.this, cursor.getString(9), Toast.LENGTH_LONG).show();
                    ((ImageView)view.findViewById(R.id.logo)).setImageDrawable(getResources().getDrawable(resID, getApplicationContext().getTheme()));
                    return true;
                }
                if(view.getId() == R.id.like){
                    liked = cursor.getString(9);
                    final int resID = getResources().getIdentifier(liked, "drawable", getPackageName());
                    ((ImageView)view.findViewById(R.id.like)).setImageDrawable(getResources().getDrawable(resID, getApplicationContext().getTheme()));

                    ((ImageView)view.findViewById(R.id.like)).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            I AM STUCK IN HERE, OR MAYBE NOT HERE?

                        }
                    });

                    return true;
                }
                return false;
            }

        });

        channelList.setAdapter(mSCA);
    } else {
        Toast.makeText(this,"Unable to open Database.",Toast.LENGTH_LONG).show();
    }
}

回答我自己的问题很奇怪..:D

//This is for getting Textview and Imageview from custom_listview.xml
RelativeLayout vwParentRow = (RelativeLayout)v.getParent();
TextView child = (TextView)vwParentRow.getChildAt(1);
ImageView btnChild = (ImageView)vwParentRow.getChildAt(3);

//This is for changing image to favorited or no
assert (R.id.like == btnChild.getId()); 

Integer integer = (Integer) btnChild.getTag();
integer = integer == null ? 0 : integer;

switch(integer) {
   case R.drawable.yes:
        btnChild.setImageResource(R.drawable.no);
        btnChild.setTag(R.drawable.no);
        //This is to update database
        mDB.execSQL("UPDATE Channel_Info SET Favourite='no' WHERE name='" + child.getText() + "'");
        break;
   case R.drawable.no:
   default:
        btnChild.setImageResource(R.drawable.yes);
        btnChild.setTag(R.drawable.yes);
        mDB.execSQL("UPDATE Channel_Info SET Favourite='yes' WHERE name='" + child.getText() + "'");
        break;
}
  1. Get the ID of a drawable in ImageView
  2. 忘记了
  3. 忘了