使用SimpleCursorAdapter通过onClick ImageView更新/编辑Sqlite数据库

问题描述 投票:0回答:1

Ilustration

请先查看图像。

如何获取值“ Test 1”,然后将sqlite数据库中的collumn(收藏夹)更新为“是”?我正在寻找解决方案,但与我的问题不符,或者我是一个不了解的人。: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();
    }
}
java android sqlite simplecursoradapter
1个回答
0
投票

这很奇怪,无法回答我自己的问题。: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. 忘记
© www.soinside.com 2019 - 2024. All rights reserved.