2014-07-14 81 views
0

我正在運行ListView的以下代碼onItemClickListener。但它表明ClassCastException當我把串在位置ARG2在字符串s爲:從列表視圖中獲取字符串時拋出類異常

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; 
    } 

其餘類是:

package bitcream.whats.your.score; 

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

是否聲明arg0.getItemAtPosition(ARG2)返回字符串?如果在將它們存儲到字符串之前沒有先將它們轉換爲字符串。 – CENT1PEDE

+0

發佈適配器代碼 – Raghunandan

+0

發佈其餘類代碼。你有吸氣和吸氣的嗎?也張貼適配器代碼,看看http://developer.android.com/reference/android/widget/AdapterView.html#getItemAtPosition(int) – Raghunandan

回答

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

你的類型不是String,但它可以是任何Object(因爲?型的,在這種情況下它恰好是bitcream.whats.your.score.Rest)。我相信你想使用toString()

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

編輯

根據您的更新,您應該添加toString()休息 - 類似,

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

arg0.getItemAtPosition(arg2)它本身返回一個字符串,所以如果我把它轉換爲字符串什麼錯了tostring()函數也做同樣的我猜 – Shubhankar

+1

如果這個字符串s =(String)arg0.getItemAtPosition(arg2);'給你一個ClassCastException那麼它不是一個'String'。 –

+0

獲取與列表中指定位置關聯的數據。 http://developer.android.com/reference/android/widget/AdapterView.html#getItemAtPosition(int) – Shubhankar

相關問題