从listview获取String时抛出异常

时间:2014-07-14 03:35:46

标签: java android listview classcastexception

我正在运行onItemClickListener的以下代码ListView。但是当我将字符串arg2放在字符串s中时,它显示ClassCastException

String s= (String) arg0.getItemAtPosition(arg2);

代码段:

lv1.setOnItemClickListener(new OnItemClickListener() {

     @Override
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
       String out;
       String s = (String) arg0.getItemAtPosition(arg2);
       //some code
     }
}

我也在发布堆栈跟踪:

07-14 02:35:42.798: E/AndroidRuntime(412): FATAL EXCEPTION: main
07-14 02:35:42.798: E/AndroidRuntime(412): java.lang.ClassCastException: bitcream.whats.your.score.Rest
07-14 02:35:42.798: E/AndroidRuntime(412):  at bitcream.whats.your.score.Intermediate$1.onItemClick(Intermediate.java:55)
07-14 02:35:42.798: E/AndroidRuntime(412):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
07-14 02:35:42.798: E/AndroidRuntime(412):  at android.widget.ListView.performItemClick(ListView.java:3513)
07-14 02:35:42.798: E/AndroidRuntime(412):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
07-14 02:35:42.798: E/AndroidRuntime(412):  at android.os.Handler.handleCallback(Handler.java:587)
07-14 02:35:42.798: E/AndroidRuntime(412):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-14 02:35:42.798: E/AndroidRuntime(412):  at android.os.Looper.loop(Looper.java:123)
07-14 02:35:42.798: E/AndroidRuntime(412):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-14 02:35:42.798: E/AndroidRuntime(412):  at java.lang.reflect.Method.invokeNative(Native Method)
07-14 02:35:42.798: E/AndroidRuntime(412):  at java.lang.reflect.Method.invoke(Method.java:507)
07-14 02:35:42.798: E/AndroidRuntime(412):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-14 02:35:42.798: E/AndroidRuntime(412):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-14 02:35:42.798: E/AndroidRuntime(412):  at dalvik.system.NativeStart.main(Native Method)
07-14 02:35:45.468: I/Process(412): Sending signal. PID: 412 SIG: 9

请建议修复。

适配器代码为:

public class Myadapter extends ArrayAdapter<Rest>{

    Context ctx;
    List<Rest> list;
    Rest r1;
    ImageView iv1;
    TextView tv1, tv2;
    public Myadapter(Context context, int textViewResourceId,
            List<Rest> objects) {
        super(context, textViewResourceId, objects);
        ctx = context;
        list = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater lii = (LayoutInflater) ctx.getSystemService(ctx.LAYOUT_INFLATER_SERVICE);
        View row = lii.inflate(R.layout.li, null, false);
        tv1 = (TextView) row.findViewById(R.id.textView1);
        r1 = list.get(position);
        tv1.setText(r1.name);
        work();
        return row;
    }

Rest类如下:

package bitcream.whats.your.score;

public class Rest {
    String name;
    public Rest(String name) {
        this.name = name;
    }
}

2 个答案:

答案 0 :(得分:3)

您的列表由Rest填充。 public class Myadapter extends ArrayAdapter<Rest>{

public Object getItemAtPosition (int position)

Added in API level 1
Gets the data associated with the specified position in the list.

Parameters
position    Which data to get
Returns
The data associated with the specified position in the list

因此,此arg0.getItemAtPosition(arg2)不返回String并将其转换为字符串,导致ClassCastException

所以在onItemClick

Rest rs = (Rest) arg0.getItemAtPosition(arg2);
String value= rs.name

答案 1 :(得分:1)

您的类型不是String,但它可以是Object(由于?类型,在这种情况下恰好是bitcream.whats.your.score.Rest )。我相信你想使用toString()

String s = arg0.getItemAtPosition(arg2).toString();

修改

根据您的更新,您应该将toString()添加到Rest - 类似于

public class Rest {
  String name;
  public Rest(String name) {
    this.name = name;
  }
  @Override
  public String toString() {
    return name;
  }
}