Search code examples
androidbuttonviewflipperonclicklistener

How to get particular info about a button inside a viewflipper view on android?


I try to make an aplication that use a viewFlipper to flip some text. The content is populated from a cursor and views are added programaticaly.

Each view is based on the same xml layout file and each of the views contain 2 buttons.

When I press one button I fire onClick event, but how can I pass a custom variable to the onclick event?

I have a var id witch I need to be different for each view and I need to get this value on button press.

Here is my code:

Cursor cur = db.rawQuery(
                "SELECT * FROM items WHERE id_cat = " + id_cat + "", null);

        TextView t = new TextView(this);

        int total_items = cur.getCount();
        if (cur.moveToFirst()) {
            do {
                String data = cur.getString(cur.getColumnIndex("item_text"));
                String item_id= cur.getString(cur.getColumnIndex("_id"));

                int current = cur.getPosition()+1;

                // load xml template
                 LayoutInflater inflater = LayoutInflater.from(this);
                 View v = inflater.inflate(R.layout.item_flip, null, false);
                     ToggleButton btnf=(ToggleButton)v.findViewById(R.id.favorite);
                 btnf.setOnClickListener(new OnClickListener() { 

                        public void onClick(View v) {
                            switch(v.getId()){
                             case R.id.favorit:
                                // switch  favorite
                                SQLiteDatabase db = dbAdapter.openDataBase();
                                if(status==1) {
                       //// HERE I NEED THE ITEM_ID DISTINCT FOR EACH VIEW
                                    db.execSQL("UPDATE items SET favorite = 0 WHERE items._id = " + item_id);

                                 } else {
                                db.execSQL("UPDATE items SET favorite = 1 WHERE items._id = " + item_id);

                                 }

                                System.out.println("Switch");
                                db.close();
                             break;

                            }       
                        }
                      });


                 vf.addView(v);

            } while (cur.moveToNext());

The button works ok but the item ID is not passed.

If I have 5 items in my viewflipper, item_id will become 1, then 2, then 3, then .... then 5 and no mather whiitch of the buttons I press the item_id will be the id of the last view.

Thanks in advance if u have any solution.


Solution

  • OMG,

    is the 3rd time when I post a question here and in next 10 minutes I fine the solution by myself :)

    he is the solution if anyone need something similar:

    In my XML I added a tag:

            <ToggleButton
            android:id="@+id/favorite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/share"
            android:layout_marginTop="6dip"
            android:textOn="Favorit"
            android:textOff="Favorit"
            android:text="Favorit"
            android:tag="999" />
    

    then when I build the specified view I modify the tag on each separated view with my item_id:

           ToggleButton swc_favorite = (ToggleButton) v.findViewById(R.id.favorite);
    
                 if(status==1) {
                     swc_favorit.setChecked(true);
                 } else {
                     swc_favorit.setChecked(false);
                 }
                 // set favorit tag
                 swc_favorit.setTag(item_id);
    

    Next in the clicklistener I get that tag from the clicked view :

     final ToggleButton btnf=(ToggleButton)v.findViewById(R.id.favorite);
                 btnf.setOnClickListener(new OnClickListener() {  
     public void onClick(View v) {
        String item_id_clicked = (String) btnf.getTag();
     ........
    

    Need to set ToggleButton btnf as final to work.

    Thanks and hope to be a help for some of you.