I m Android
I m Android

Reputation: 85

how to gettext in listview

How to gettext in listview OnItemClickListener??

I can't get text in listview toast work.but gettext(); not work

This is my code. Thanks in advance.

      listView.setOnItemClickListener(new OnItemClickListener() {
        @SuppressWarnings("unused")
        @Override
        public void onItemClick(AdapterView<?> parent, View view, 
            int position, long id) {
          // ListView Clicked item index
          int itemPosition = position;

          //parent.getChildAt(position - listView.getLastVisiblePosition()).findViewById(R.id.listView1);   
          String itemValue = (String) listView.getItemAtPosition(position);

          //???????????????????????????.getText().toString();

          // Show Alert 
          Toast.makeText(
            getApplicationContext(),
            "Position :" + itemPosition + " ListItem: " + itemValue,
            Toast.LENGTH_LONG
          ).show();
        }
      });

Upvotes: 1

Views: 3011

Answers (5)

Renan A.
Renan A.

Reputation: 21

Try this:

lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override

     public void onItemClick(AdapterView<?> parent, View view, int position, long      id){

           TextView txt = (TextView) view.findViewById(R.id.lblId);
           String s = textView.getText().toString();
           Toast.makeText(getActivity(), "Your id "+s, Toast.LENGTH_SHORT).show();
    }
});

Upvotes: 1

user1992
user1992

Reputation: 176

just type this code and import textview in your project

 String itemValue = (String) listView.getItemAtPosition(position);
 String values=((TextView)view).getText().toString();

Upvotes: 3

Neerajlal K
Neerajlal K

Reputation: 6828

Instead of getting the text from the view. Get it from the dataset values, which you used to bind the data to the ArrayAdapter.

String itemValue = values[position];
Toast.makeText(YourActivty.this, "Clicked " + itemValue + " at " + position, Toast.LENGTH_LONG).show();

Upvotes: 0

Alexander Dadukin
Alexander Dadukin

Reputation: 564

I think you can try this:

@Override
public void onItemClick(AdapterView<?> view, View v, int position, long arg3) {
ListView l = (ListView) view;
String s = (String) l.getItemAtPosition(position);
}

Upvotes: 0

veer
veer

Reputation: 83

You have to get the id of the textView from the view parameter like this

public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

TextView textView =(TextView) view.findViewById(R.Id.YourTextViewId);
String str=textView.getText();

}

Upvotes: 0

Related Questions