基于listview对象位置按ID选择文本视图

时间:2014-09-10 19:13:13

标签: c# android android-listview textview xamarin

编辑:这是针对C#而不是Java。

我觉得这不应该像现在这么难,但我一直在努力争取这个时间,希望有人可以帮助我解决这个问题。

基本上我有一个列表视图,每行填充5个文本视图。

<?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>

所以我想要的是,当有人点击该行时,我可以从该特定行中的任何子文本视图中获取文本。

所以我可以使用e.Position获取listview中的位置,或使用以下对象获取位置:

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

但是,我怎样才能在该对象中获得5个文本视图?

非常感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

这是C#版本

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

或者如果您已经拥有该职位,则可以使用

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

答案 1 :(得分:0)

执行此操作的正确方法是使用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);
      }
    });

答案 2 :(得分:0)

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);
    }
});