sam
sam

Reputation: 11

Android : get id of listview row when button clicked

i have a listview that contains relative views on each list item that are populated by a database and contains two buttons. when one of the buttons in the list view item is pressed i need to get the id of that row in the list view. in other words i am trying to find the same thing as the last argument in the onItemClick methos for the onItemClickListener(), which would return a click for the entire item. however i am just listening for the button in the listview item so i am unable to use onItemClickListener().

i have this method being called when i click the button

public void plusClick(View v) 
    {

        //get the row the clicked button is in
        RelativeLayout vwParentRow = (RelativeLayout)v.getParent();

        //i need to get the id of this RelativeLayout item in the list view

        TextView child = (TextView)vwParentRow.getChildAt(2);
        Button btnChild = (Button)vwParentRow.getChildAt(1);


        int c = Color.CYAN;


        vwParentRow.setBackgroundColor(c); 
        vwParentRow.refreshDrawableState();       
    }

it need to find the id of the relative layout which i can pass ass arg for

public Cursor getRow(long rowId) {
    String where = KEY_ROWID + "=" + rowId;
    Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                    where, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

to create a new cursor contiain the data in this listview item

Upvotes: 0

Views: 2215

Answers (3)

bGorle
bGorle

Reputation: 1996

try this code:

private OnItemClickListener itemClickListener = new OnItemClickListener() {
        public void onItemClick(AdapterView<?> av, View v, int position,
                long id) {  
                // write your logic here        
                // position starts from 0
                // id starts from 1

        }
    };

Upvotes: 1

Mart&#237;n Alcubierre
Mart&#237;n Alcubierre

Reputation: 4389

You need some kind of adapter, in getView method you have to declare your event

There is an example:

holder.hMention.setOnClickListener(new VerTweet(mention));

and...

public class VerTweet implements View.OnClickListener 
{

    Mention mention;

    public VerTweet ( Mention mention) {
        this.mention = mention;
    }

    public void onClick( View view ){

        Bundle bundle = new Bundle();
        //Le ponemos la variable parametro con el contenido test
        bundle.putInt("idTrack", mention.getIdTrack());
        bundle.putString("Gifo", mention.getGifo());
        bundle.putString("From", mention.getFromUser());
        bundle.putString("Date", mention.getDateMentions());
        bundle.putString("Text", mention.getText());
        bundle.putLong("Status", mention.getLastId());
        Intent i = new Intent(getContext(), Tweet.class);
        i.putExtras(bundle);
        getContext().startActivity(i);

    }
}

https://github.com/m-alcu/SkoltiAlert/blob/master/src/com/alcubier/skoltialert/mentions/GDMentionsList.java

Upvotes: 0

MHP
MHP

Reputation: 2731

when you set onclicklistener for your button you can set tag for it(tag can be id of row), and then when you click on button get it's tag. (of course if you use custom adapter)

Upvotes: 0

Related Questions