【问题标题】:Selecting a textivew by ID based on listview object position根据列表视图对象位置按 ID 选择 textivew
【发布时间】:2014-09-10 19:13:12
【问题描述】:

编辑:这是针对 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 = FindViewById<ListView>(Resource.Id.LocationsList);
    var item = listView.GetChildAt(e.Position);

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

非常感谢您的帮助!

【问题讨论】:

  • e 中的 e.Position 是什么?您使用的是 ExpandableListview 吗?

标签: c# android android-listview textview xamarin


【解决方案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);

【讨论】:

  • @user2463732 如果这是正确答案,请选中复选标记将其标记为答案,以便其他人可以看到它是正确的。
【解决方案2】:

正确的做法是使用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);
      }
    });

【讨论】:

  • 我正在寻找c#解决方案,抱歉我应该把它添加到描述中,只在标签中提到。
【解决方案3】:

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

【讨论】:

  • 我正在寻找c#解决方案,抱歉我应该把它添加到描述中,只在标签中提到。
  • 我知道它是 c# 但我认为 onItemClickListener 即使在 C# IDE 版本的情况下也会存在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多