Android: Get the listview item from button clicked in custom listview

后端 未结 5 588
终归单人心
终归单人心 2021-02-02 16:17

I have a custom ListView with two button and I when I click either button on any row I want to get the text label on the Listview and for now just popup a toast with it. So far

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-02 16:55

    If you have a ListActivity, and you're not using your own adapter, you can still get the list item belonging to the tapped button, like so:

    In your layout file of the list row:

    
    

    In your ListActivity:

    public void callBuddy(View view) {
    
        int position = getListView().getPositionForView((View) view.getParent());
        Buddy buddyToCall = (Buddy) getListView().getItemAtPosition(position);
    
        Toast.makeText(MyListActivity.this, String.format("Calling your buddy %s.", buddyToCall.name), Toast.LENGTH_SHORT).show();
    
    }
    

提交回复
热议问题