Listview onClickItem函数不起作用

时间:2014-07-09 15:34:45

标签: android listview android-listview onclick listitem

我使用ArrayAdapter String类型的自定义布局而不是android.R.layout.simple_list_item_1,但是当我点击列表项时,我的应用程序崩溃了。 这是我的logcat。

07-09 20:48:35.549: E/AndroidRuntime(1822): FATAL EXCEPTION: main
07-09 20:48:35.549: E/AndroidRuntime(1822): java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
07-09 20:48:35.549: E/AndroidRuntime(1822):     at com.faisalahsan.healthdroid.DoctorCategoryListActivity$1.onItemClick(DoctorCategoryListActivity.java:55)
07-09 20:48:35.549: E/AndroidRuntime(1822):     at android.widget.AdapterView.performItemClick(AdapterView.java:292)
07-09 20:48:35.549: E/AndroidRuntime(1822):     at android.widget.AbsListView.performItemClick(AbsListView.java:1066)
07-09 20:48:35.549: E/AndroidRuntime(1822):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2522)
07-09 20:48:35.549: E/AndroidRuntime(1822):     at android.widget.AbsListView$1.run(AbsListView.java:3182)
07-09 20:48:35.549: E/AndroidRuntime(1822):     at android.os.Handler.handleCallback(Handler.java:605)
07-09 20:48:35.549: E/AndroidRuntime(1822):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-09 20:48:35.549: E/AndroidRuntime(1822):     at android.os.Looper.loop(Looper.java:137)
07-09 20:48:35.549: E/AndroidRuntime(1822):     at android.app.ActivityThread.main(ActivityThread.java:4424)
07-09 20:48:35.549: E/AndroidRuntime(1822):     at java.lang.reflect.Method.invokeNative(Native Method)
07-09 20:48:35.549: E/AndroidRuntime(1822):     at java.lang.reflect.Method.invoke(Method.java:511)
07-09 20:48:35.549: E/AndroidRuntime(1822):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
07-09 20:48:35.549: E/AndroidRuntime(1822):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
07-09 20:48:35.549: E/AndroidRuntime(1822):     at dalvik.system.NativeStart.main(Native Method)

这是我的活动代码。

public class DoctorCategoryListActivity extends ListActivity {

    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_doctor_category_list);

        String[] doctorCategories =  {"Acupuncturist", "Addiction Specialist"};

        mListView = (ListView) findViewById(android.R.id.list);
        mListView = getListView();

        ArrayAdapter<String> mAdapte = new ArrayAdapter<String>(DoctorCategoryListActivity.this, 
        R.layout.layout_doctor_catagory_list_item, R.id.tv_doctor_catagory_list_item, doctorCategories);

                mListView.setAdapter(mAdapte);

        mListView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                      int position, long id) {                     
                      // selected item 
                    String speciality = ((TextView) view).getText().toString();
                        Toast.makeText(DoctorCategoryListActivity.this, speciality+" clicked", Toast.LENGTH_SHORT).show();

                }
            });
        }
    }

这是XML代码layout_doctor_catagory_list_item

<?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"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/tv_doctor_catagory_list_item"
        android:layout_width="match_parent"
        android:layout_height="43dp" 
        android:text="Test"
        android:textSize="18sp"
        android:gravity="center_vertical|left"
        android:layout_marginLeft="10dp"        
        android:layout_marginTop="5dp"/>

    <View
        android:layout_width="fill_parent"
        android:layout_height="0.5dp"
        android:layout_marginLeft="10dp"
        android:background="@android:color/darker_gray"/>

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

您应该使用:

TextView tv = (TextView)view.findViewById(R.id.tv_doctor_catagory_list_item);
String speciality = tv.getText().toString();

传递给onItemClick()的视图是项目布局的最外层视图(即LinearLayout中的layout_doctor_catagory_list_item.xml),而资源ID是您提供标识其中的TextView

答案 1 :(得分:0)

在onclickitem方法中你应该view.findViewById(Textview)而不是((TextView) view).getText().toString();你在methode onItemClick中收到的视图是你在ArrayAdapters构造函数中传递的整个Listview项。在你的情况下,整个视图是

LinearLayout
----TextView
----View(Spacer)