3

我正在尝试打印在 listView 中单击的值,但随后出现以下异常:

07-04 10:40:56.482: E/AndroidRuntime(1356): FATAL EXCEPTION: main
07-04 10:40:56.482: E/AndroidRuntime(1356): java.lang.ClassCastException:      android.widget.LinearLayout cannot be cast to android.widget.TextView
07-04 10:40:56.482: E/AndroidRuntime(1356):     at com.passwordkeeper.ui.ActivityHomeScreen$1.onItemClick(ActivityHomeScreen.java:88)

这是一个代码片段:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mTextView = (TextView) findViewById(R.id.labelList);
    db = new DatabaseHandler(this);
    createList();
    displayList();
}   

createList 函数工作正常。这是我的 displayList 方法:

public void displayList(){
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_home_screen, R.id.listTextView, mAccountNames));
    mListView = getListView();
    mListView.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            String product = ((TextView) view).getText().toString();
            Log.v("mSelectedProduct", product);
        }           
    });
}

我的文件 activity_home_screen.xml 的 xml 代码是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
    android:id="@+id/listTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="28dp"
    android:layout_marginTop="26dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />  

任何帮助都会很有用!

谢谢你。

4

4 回答 4

4

你可以试试这个

mListView.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            TextView txtview = (TextView)view.findViewById(R.id.listTextView);
            String product = txtview.getText().toString();
            Log.v("mSelectedProduct", product);
        }           
    });
于 2013-07-04T05:27:29.670 回答
1

您在 OnItemClickListener 中获得的 View 将返回父布局,在您的情况下是 LinearLayout。

您可以通过执行以下操作来获取您的文本视图:

mListView.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            TextView txtview = (TextView)view.findViewById(R.id.listTextView);
            String product = txtview.getText().toString();
            Log.v("mSelectedProduct", product);
        }           
    });
于 2013-07-04T05:36:37.407 回答
1

用这个:

String product = ((TextView) view.findViewById(R.id.listTextView)).getText().toString();
于 2013-07-04T05:24:37.063 回答
0
mListView.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            TextView textview= (TextView)view.findViewById(R.id.TEXTVIEW_ID);
            String product = textView.getText().toString();
            Log.v("mSelectedProduct", product);
        }           
    });

将“TEXTVIEW_ID”替换为您在列表视图项 xml 中给出的 textview 的 ID(如果没有给出,请给出一个)。

于 2013-07-04T05:29:21.747 回答