0

enter image description here

I have a list of players whos name are displayed listview. and each row of listview contains button, textview and imageview. How can I get the value of textview?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Programmer
  • 5,360
  • 10
  • 37
  • 61

3 Answers3

0

Is it a static list or a dynamically generated one?
If its a static one you can assign a different id to each textview in the xml itself, and then use FindViewById to access it.
If it's not this is what you've got to do: you will obviously have one row and display it many times. So multiple textviews will have same ID.
Use a for loop, inside which use FindViewById(Remember FindViewByID will only access the first element with mentioned Id, set its Id to something else, in the next iteration the next textview is selected, set its Id to something) then use these new ids to access them, thus you can access each textview

GMD
  • 698
  • 5
  • 12
Black Mamba
  • 205
  • 1
  • 5
  • 21
  • then use them like this: EditText et1 = (EditText) findViewById(R.id.editText1); String Username = et1.getText().toString(); – Black Mamba Nov 08 '11 at 07:43
0

If you are using Custom Adapter class to populate the listview,then in your adapter,you can use HashMap for saving key-value pair,saving position of listitem with the data into that textview.and then you can easily retrieve it on OnItemClickListener of listview.

Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
0

Depending on the specifics of your implementation, I would go with one of the following approaches.

Option A. Use setOnItemClickListener to register a click listener with the list (or if you're using a ListActivity or ListFragment simply use [onListItemClick](http://developer.android.com/reference/android/app/ListActivity.html#onListItemClick%28android.widget.ListView, android.view.View, int, long%29)). onItemClick gets passed in the View that was clicked and can be used to retrieve nested views, e.g. the TextView you're looking for.

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TextView tv = (TextView) view.findViewById(R.id.textview);
        String tvText = tv.getText();
    }
}

Option B Assuming you fill your list from some sort of data collection, you may be able to do something similar to above, but use the passed position parameter as an index to directly get the text from the objects in your collection; i.e.:

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SomeObject so = myCollection.get(position);
        String tvText = so.getTextViewText();
    }
}

There are lots of more options though. I kind of like creating my own extension of ArrayAdapter to hold the models for the views of the items in the list. That way you could also call getItemAtPosition(int position) or getItem(int position) and cast the returned object to your data type.

MH.
  • 45,303
  • 10
  • 103
  • 116