4

我正在开发一个应用程序,我在其中创建了一个自定义列表视图:

列表视图 xml 代码为:

<ListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="15dip"
    android:divider="#623b42"
    android:dividerHeight="0.5dip"
    android:cacheColorHint="#00000000"
    android:overScrollMode="never" >
</ListView>

适配器 xml 是:

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

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/textview_rupee_mlsaa"
        android:gravity="center_vertical"
        android:padding="15dip"
        android:textColor="@color/color_black"
        android:textSize="15sp"
        tools:ignore="SelectableText" />

    <TextView
        android:id="@+id/textview2"
        android:layout_width="60sp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:gravity="center_vertical"
        android:padding="15dip"
        android:textColor="@color/color_black"
        android:textSize="15sp"
        tools:ignore="SelectableText" />

</RelativeLayout>

适配器的 .java 文件是:

public class ListAdapter extends ArrayAdapter<String> {
/** Global declaration of variables. As there scope lies in whole class. */
private Context context;
private String[] dataset1;  
private int[] dataset2;

    /** Constructor Class */
    public ListAdapter(Context context,String[] ListArray1, int[] ListArray2) {
        super(context,R.layout.list_adapter_layout,ListArray);
        this.context = context;
            this.dataset1= ListArray1;
            this.dataset2= ListArray2;
    }

    /** Implement getView method for customizing row of list view. */
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        // Creating a view of row.
        View rowView = inflater.inflate(R.layout.list_adapter_layout, parent, false);   

        TextView tv1 = (TextView)rowView.findViewById(R.id.textview1);      
        TextView tv2 = (TextView)rowView.findViewById(R.id.textview2);

        tv1.setText(data1[position]);       
        tv2.setText(""+data2[position]);

        return rowView;
    }
}

然后我将列表视图上的项目单击侦听器设置为:

listView = (ListView) findViewById(R.id.listview);
listView.setOnItemClickListener(this);    

ListAdapter adapter = new ListAdapter(this,list1,list2);
listView.setAdapter(adapter);

然后我在项目点击上使用 onItemClickListener 为:

public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
name = (String) arg0.getItemAtPosition(position);       
}

在这里,我得到了自定义列表视图的第一个文本视图的值。但我也想要第二个文本视图的值。如果我添加更多,那么如何获得它的值。

请建议我这样做的方法。

4

3 回答 3

17

在你的 OnItemClick 中尝试这样

TextView text = (TextView) arg1.findViewById(R.id.urtextID);
                           ^^^^^^
String tEXT = text.getText().toString();
于 2013-03-20T10:54:21.463 回答
2

只是上述答案的简化版本

    public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    // code in the commented section works in case of normal single textView

    // TextView temp=(TextView) view; 
    // Toast.makeText(this,
    // temp.getText()+" is pressed "+position,Toast.LENGTH_SHORT
    // ).show();

    TextView temp = (TextView) view.findViewById(R.id.textView1);

    String str = temp.getText().toString();
    Toast.makeText(this,
            str + " is pressed " + position,
            Toast.LENGTH_SHORT).show();
}
于 2014-08-12T18:03:12.210 回答
0

尝试这个,

public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
TextView textView = (TextView) arg1.findViewById(R.id.urtextID);
String text = textView.getText().toString();   
Log.d("TEXT", "Text is" + text);   
}

上面的答案和我的答案一样......

于 2020-10-28T10:28:03.943 回答