1

我正在尝试访问 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>
4

2 回答 2

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带有 id的文本tvStNumber

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

于 2013-05-28T22:38:48.153 回答
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());
            }
        }
    });
于 2013-05-28T23:14:56.360 回答