user2463732
user2463732

Reputation: 39

Selecting a textivew by ID based on listview object position

Edit: This is for C#, not Java.

I feel like this should not be as hard as it is, but I've been struggling with hours for this, hoping someone can help me figure it out.

Basically I have a listview, with each row it is populated with 5 textviews.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
  <TextView
      android:id="@+id/id"
      android:layout_height="wrap_content"
      android:layout_width="0px"
      android:layout_weight="1"
      android:text="id"/>
  <TextView
      android:id="@+id/Name"
      android:layout_height="wrap_content"
      android:layout_width="0px"
      android:layout_weight="1"
      android:text="Name" />
  <TextView
    android:id="@+id/Status"
    android:layout_height="wrap_content"
    android:layout_width="0px"
    android:layout_weight="1"
    android:text="Status" />
  <TextView
    android:id="@+id/Lat"
    android:layout_height="wrap_content"
    android:layout_width="0px"
    android:layout_weight="1"
    android:text="Lat"/>
  <TextView
   android:id="@+id/Long"
   android:layout_height="wrap_content"
   android:layout_width="0px"
   android:layout_weight="1"
   android:text="Long"/>
</LinearLayout>

So what I want, is when someone clicks the row, I can get the text from any of the children textviews within that specific row.

So I'm able to get the position in the listview using e.Position, or the object using:

    ListView listView = FindViewById<ListView>(Resource.Id.LocationsList);
    var item = listView.GetChildAt(e.Position);

But how can I then get the 5 textviews within that object?

Thanks a ton for any help!

Upvotes: 0

Views: 295

Answers (3)

kevskree
kevskree

Reputation: 4592

Here is the C# version

listView.ItemClick += (object sender, AdapterView.ItemClickEventArgs e) => {
    var textView1 = e.View.FindViewById(Resource.Id.textview1);
    var textView2 = e.View.FindViewById(Resource.Id.textview2);
};

or if you have the position already you can use

ListView listView = FindViewById<ListView>(Resource.Id.LocationsList);
var item = listView.GetChildAt(e.Position);
var textView1 = item.FindViewById(Resource.Id.textview1);

Upvotes: 1

harshal
harshal

Reputation: 592

ListView OnItemClickListener

ListView listView = (ListView)findViewById(R.id.LocationsList);
lv.setOnItemClickListener(new OnItemClickListener()
{
    @Override 
    public void onItemClick(AdapterView<?> arg0, View ItemView,int position, long arg3)
    { 
        // ItemView is the view of the list item that was clicked
        // since u said there will be 5 text views i.e. fixed number of text views below code seemb to be reasonable.
        // if the number of text is varying then better approach would be to get all the child nodes and proceed
        TextView txt1 = (TextView )findViewById(R.id.TextView1);
        ...
        ...
        TextView txt5 = (TextView )findViewById(R.id.TextView5);
    }
});

Upvotes: 0

Nyx
Nyx

Reputation: 2243

The right way to do this would be using the OnItemClickListener:

 listView.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TextView nameView = (TextView) view.findViewById(R.id.Name);
      }
    });

Upvotes: 0

Related Questions