Android:提取列表中TextView的字符串

时间:2011-08-04 10:46:13

标签: android listview textview arguments

以下是我的listview布局。现在我在listview中有一个图像和textview。我想提取textview的内容并在Toast中显示它。

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="#E6E7E2">
    <ImageView android:id="@+id/Thumbnail" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:src="@drawable/icon" />
    <TextView android:id="@+id/FilePath" android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:textColor="#000"/>
</LinearLayout>

当我这样做时,通常的方式不起作用,应用程序崩溃

// on click lister for list view
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                String sdcarduri = (((TextView) arg1).getText()).toString();
                //Toast.makeText(AndroidThumbnailList.this, "BEEEEEEE", 2000).show();


            }

        });

2 个答案:

答案 0 :(得分:2)

@Override
 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
LinearLayout layout = (LinearLayout) arg1;
TextView v = (TextView) layout.getChildAt(1);
 String sdcarduri =v.getText().toString();

Toast.makeText(AndroidThumbnailList.this, v.getText().toString(), 2000).show();


            }

        });

答案 1 :(得分:2)

使用此代码

    lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                TextView tv=(TextView)arg1.findViewById(R.id.FilePath);
String sdcardUri=tv.getText().toString();
                //Toast.makeText(AndroidThumbnailList.this, "BEEEEEEE", 2000).show();


            }

        });