从所选列表项中选择文本视图

时间:2014-09-17 10:08:11

标签: java android listview listviewitem

这是我的代码:

    mListView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            // TODO Auto-generated method stub
            View vie = (View)parent.getParent();
            TextView tv = (TextView) vie.findViewById(R.id.tv_id);
            String genus = (String) tv.getText();
            Toast.makeText(getBaseContext(), genus+"--"+id, Toast.LENGTH_SHORT).show();
        }
    });

    return adapter;
}

每次为每个项目返回相同的值。这是我的清单项目:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/tv_country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="20dp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/iv_flag"
        android:layout_width="100dip"
        android:layout_height="100dip"
        android:layout_below="@id/tv_country"
        android:layout_centerVertical="true"
        android:padding="5dp"
       />

    <TextView
        android:id="@+id/tv_country_details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv_flag"
        android:layout_below="@id/tv_country"  />
     <TextView
        android:id="@+id/tv_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_country_details"
       />
 </RelativeLayout>

这是我的视图文件。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/lv_countries"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:context=".NearbyGymList" />
    <Button android:id="@+id/btnNoResults"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/lv_countries"
                android:text="No More Results"/>

</RelativeLayout>

如何解决这个问题?

4 个答案:

答案 0 :(得分:1)

删除此行:

View vie = (View)parent.getParent();

并从参数更改为使用视图,因为它是单击项目的视图:

    mListView.setOnItemClickListener(new OnItemClickListener() {

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

            // TODO Auto-generated method stub
            //Remove this line: View vie = (View)parent.getParent();
            //Use view because it is the view of item which is clicked.
            TextView tv1 = (TextView) view.findViewById(R.id.tv_id);

            //Add this
            TextView tv2 = (TextView) view.findViewById(R.id.tv_country_details);
            String genus = (String) tv1.getText().toString();
            String genus2 = (String) tv2.getText().toString();

            Toast.makeText(getBaseContext(),genus+"--"+id+" "+genus2 , Toast.LENGTH_SHORT).show();
        }
    });

    return adapter;
}

答案 1 :(得分:0)

您没有添加对其他TextView的引用。这样做:

删除此行

View vie = (View)parent.getParent();

所以你的代码将成为:

    mListView.setOnItemClickListener(new OnItemClickListener() {

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

            // TODO Auto-generated method stub
            TextView tv1 = (TextView) view.findViewById(R.id.tv_id);
            TextView tv2 = (TextView) view.findViewById(R.id.tv_country_details);
            String genus = (String) tv1.getText().toString();
            String genus2 = (String) tv2.getText().toString();

            Toast.makeText(getBaseContext(),genus+"--"+id+" "+genus2 , Toast.LENGTH_SHORT).show();
        }
    });

    return adapter;
}

答案 2 :(得分:0)

要访问位置数据集中的元素,您可以使用

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

      Object object = parent.getItemAtPosition(position);

并将其转换为正确的类型。

答案 3 :(得分:0)

在适配器类

public View getView(final int position, View convertView,
                    ViewGroup parent) {

    LayoutInflater inflater = getLayoutInflater();
    View row = inflater.inflate(R.layout.yourlayout, parent, false);
    TextView textView = (TextView) row.findViewById(R.id.textView);

    textView .setOnClickListener(new OnClickListener() {        
        public void onClick(View v) {
            //Do whatever you want

    });
相关问题