javaandroidsqlitesimplecursoradapter

Update/Edit Sqlite Database using SimpleCursorAdapter via onClick ImageView


Illustration

Please see image first..

How can i get value "Test 1" and after that i want update column (favorite) in sqlite database to "Yes"?

I was searching to find answer for my case but there is no match with my problem, or maybe I am the one who doesn't understand.. :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();
    }
}

Solution

  • this is weird to answer my own question.. :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. I am forgot.
    3. Forgot