Skip to content Skip to sidebar Skip to footer

Get Clicked Item From Listview

I've got a problem. Before modification in my XML file, my listview was able to work perfectly. But now, after some modifications in xml, its not working properly. My listview is c

Solution 1:

lv.setOnItemClickListener(newOnItemClickListener() {
    publicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {

        // selected itemStringselected= ((TextView) view.findViewById(R.id.your_textView_item_id)).getText().toString();

        Toasttoast= Toast.makeText(getApplicationContext(), selected, Toast.LENGTH_SHORT);
        toast.show();
    }
});

Solution 2:

listView = (ListView)view.findViewById(R.id.list);
listView.setOnItemClickListener(newAdapterView.OnItemClickListener() {
    @OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {
        Stringselected= ((TextView) view.findViewById(R.id.complaint)).getText().toString();
        Toast.makeText(context,selected,Toast.LENGTH_LONG).show();
    }
});

Solution 3:

If you want onClickListener on the TextView only, its better to define onClickListener in the Adapter itself. like

yourTextView.setOnClickListener(newView.OnClickListener() {
      publicvoidonClick(View v) {
        // showToast()
      }
});

Solution 4:

Apart from the above solution, this worked for me i was facing a similar situation, I was able to solve it after i added the following

android:descendantFocusability="blocksDescendants"

to my listview in xml. It is working now. To the real curious user here's more info about that http://developer.android.com/reference/android/R.attr.html#descendantFocusability

Solution 5:

Because i have not enough reputation to comment Here i am commenting on your question on the answer of SKK Just add if statement to your and perform different actions...

lv.setOnItemClickListener(newOnItemClickListener() {
publicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {

    // selected itemStringselected= ((TextView) view.findViewById(R.id.your_textView_item_id)).getText().toString();

    if(selected.equals("Your comparing string, like Games"){                                               

    Toasttoast= Toast.makeText(getApplicationContext(), selected, Toast.LENGTH_SHORT);
    toast.show(); }                                                                                                                 

    else {                                            
    if(selected.equals("Your comparing string, like Games")){                                            
    Toasttoast= Toast.makeText(getApplicationContext(), selected, Toast.LENGTH_SHORT);
    toast.show();}}


}});                                                   

Post a Comment for "Get Clicked Item From Listview"