ListView setOnItemClickListener抛出异常

时间:2013-05-28 22:16:58

标签: android android-layout android-listview

我正在尝试访问setOnItemClick侦听器,并抛出异常。我在那里添加了一个try catch,并在logcat异常中登录。我正在做的是什么导致它抛出异常?

这是自定义适配器:

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;


public class StudentAdapter extends ArrayAdapter<Student> {
  Context context;
  int layoutResourseId;
  Student data[] = null;
    public StudentAdapter(Context context, int layoutResourceId, Student[] data) {
        super(context, layoutResourceId, data);
        this.context = context;
        this.layoutResourseId = layoutResourceId;
        this.data = data;
        // TODO Auto-generated constructor stub
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        StudentHolder holder = null;
        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourseId, parent, false);
            holder = new StudentHolder();
            //Associate the Items in the holder with a view in the XML file.
            holder.tvStNumber = (TextView)row.findViewById(R.id.tvStNumber);
            row.setTag(holder);
        } else
        {
            holder = (StudentHolder) row.getTag();
        }

        //Setting the text for the items
        Student item =data[position];
        holder.tvStNumber.setText(item.GetStudentNumber());


        return row;
    }
  //Student Holder
    static class StudentHolder
    {
        TextView tvStNumber;
        TextView tvStDescription;

    }
}

以下是监听方法:

listView1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
                try{
                String listValue = ((TextView)view).getText().toString(); //This is where the exception is happening. The below text should have been commented out.
                // String text = (String) listView1.getItemAtPosition(position); // this is where the excexption is happening. 
                  Toast.makeText(getApplicationContext(), "You click" +  position, Toast.LENGTH_LONG).show();
                  String d = "Test toast.";
                }catch(Exception e)
                {
                    Log.e("Error Message", e.getMessage());
                }
            }
        });

这是XML文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout 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"
    android:orientation="vertical"
    tools:ignore="HardcodedText" 
    >

    <ScrollView 
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       >
        <LinearLayout 
           android:id="@+id/linearylayout1"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:orientation="horizontal"
            >
            <ListView
              android:id="@+id/lvStudent"
              android:layout_width="match_parent"
              android:layout_height="144dp" 
              android:descendantFocusability="beforeDescendants"
              >
            </ListView>
        </LinearLayout>

    </ScrollView>

</LinearLayout>

这就是行的样子。

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

    <TextView
        android:id="@+id/tvStNumber"
        android:layout_width="100sp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10sp"
        android:layout_marginTop="5sp"
        android:text="EE#" />

     <TextView
         android:id="@+id/tvStDescription"
         android:layout_width="100sp"
         android:layout_height="wrap_content"
         android:layout_alignParentTop="true"
         android:layout_marginLeft="10sp"
         android:layout_toRightOf="@+id/tvStNumber"
         android:text="Item Description" />
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

您最有可能获得ClassCastException

使用此:

String listValue = (((TextView) ((RelativeLayout) v).getChildAt(0)).getText().toString());

而不是:

String listValue = ((TextView)view).getText().toString();

View.getChildAt(int i)会在您传入的位置返回View作为参数。因此,如果您使用View.getChildAt(0)(就像我上面所做的那样),您应该获得标识为TextView的{​​{1}}的文字。

但你为什么要这样做呢?您可以通过适配器找到文本。

答案 1 :(得分:0)

onItemClick中的view参数表示行而不是textview。你可以尝试

listView1.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
            try{
            String listValue = parent.getItemAtPosition(position).GetStudentNumber(); //This is where the exception is happening. The below text should have been commented out.
            // String text = (String) listView1.getItemAtPosition(position); // this is where the excexption is happening. 
              Toast.makeText(getApplicationContext(), "You click" +  position, Toast.LENGTH_LONG).show();
              String d = "Test toast.";
            }catch(Exception e)
            {
                Log.e("Error Message", e.getMessage());
            }
        }
    });