Dodi
Dodi

Reputation: 2269

getting item's name when clicking a ListView

Is there any way I can capture a name of an item clicked in a list view when using "onItemLongClickListiner" ? I know I can capture position etc but I need a name

Upvotes: 2

Views: 15682

Answers (4)

Ahsan
Ahsan

Reputation: 377

It has worked for me.

listtview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> adapterView, View view, int position, long l){
       String selected = (String) adapterView.getItemAtPosition(i);
       Toast.makeText(getApplicationContext(),"This is"+selected,Toast.LENGTH_LONG).show();
  }
});

Upvotes: 1

Silambarasan Poonguti
Silambarasan Poonguti

Reputation: 9442

Try this..

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            //inflated row layout textview
            TextView tagText = (TextView) view.findViewById(R.id.txt_text);
            String tag = tagText.getText().toString();
            Toast.makeText(getApplicationContext(),
            "Element Name " + tag + " Clicked", Toast.LENGTH_SHORT).show();
        }
    });

Upvotes: 1

Raghunandan
Raghunandan

Reputation: 133560

You can use lv.getItemAtPosition(position) to get the text based on the index position

lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position,
        long arg3) 
{
    String name = arg0.getItemAtPosition(position).toString()
}
});

For more info

getItemAtPosition

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157457

I suppose that you ListView is filled up with String object:

public boolean onItemLongClick (AdapterView<?> parent, View view, int position, long id) {
 String name = (String) parent.getItemAtPosition(position);
}

AdapterView.getItemAtPosition(position) gets the data associated with the specified position in the list.

Upvotes: 11

Related Questions