2011-12-26 45 views
0

我想發送的內容點擊列表到另一個畫面,但得到的錯誤...傳值列表視圖中單擊從一個屏幕到另一個機器人

我的繼承人主文件...

package com.bmc; 

import java.util.ArrayList; 
import java.util.HashMap; 

import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.NodeList; 

import android.app.ListActivity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.AdapterContextMenuInfo; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.Toast; 

public class Main extends ListActivity { 
    /** Called when the activity is first created. */ 
    private Context mCtx; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.myrequest); 
     mCtx = this; 

     ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 


     String xml = XMLfunctions.getXML("http://10.0.2.2/ss.htm"); 
     Document doc = XMLfunctions.XMLfromString(xml); 

     int numResults = XMLfunctions.numResults(doc); 

     if((numResults <= 0)){ 
      Toast.makeText(Main.this, "Error", Toast.LENGTH_LONG).show(); 
      finish(); 
     } 

     NodeList nodes = doc.getElementsByTagName("result"); 

     for (int i = 0; i < nodes.getLength(); i++) {       
      HashMap<String, String> map = new HashMap<String, String>();  

      Element e = (Element)nodes.item(i); 
      map.put("id", XMLfunctions.getValue(e, "id")); 
      map.put("name", XMLfunctions.getValue(e, "id") + "-" + XMLfunctions.getValue(e, "name") + " :"); 
      mylist.add(map);    
     }  


     ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
         new String[] { "name"}, 
         new int[] { R.id.item_title }); 

     setListAdapter(adapter); 

     final ListView lv = getListView(); 
     lv.setTextFilterEnabled(true); 
     lv.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {    
       @SuppressWarnings("unchecked") 
       HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);     
       Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_LONG).show(); 

       String selectedFromList = (String) (lv.getItemAtPosition(position)); 

       Intent i = new Intent(mCtx, Details.class); 

       Bundle extras=new Bundle(); 
       extras.putSerializable("obj_to_pass", selectedFromList); 
       i.putExtras(extras); 

      startActivity(i); 
    //   Main.this.finish(); 

      } 
     }); 

    } 
} 

我的繼承人Details.java文件...

package com.bmc; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.EditText; 


public class Details extends Activity 
{ 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.details); 

     EditText txt; 

     Bundle extras = getIntent().getExtras(); 
     Object my_obj = extras.getSerializable("obj_to_pass"); 

     txt=(EditText) findViewById(R.id.editText_desc1); 

     txt.setText((CharSequence) my_obj); 
    } 

} 

日誌文件...

12-27 01:46:55.994: E/AndroidRuntime(311): FATAL EXCEPTION: main 
12-27 01:46:55.994: E/AndroidRuntime(311): java.lang.ClassCastException: java.util.HashMap 
12-27 01:46:55.994: E/AndroidRuntime(311): at com.bmc.Main$1.onItemClick(Main.java:73) 
12-27 01:46:55.994: E/AndroidRuntime(311): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 
12-27 01:46:55.994: E/AndroidRuntime(311): at android.widget.ListView.performItemClick(ListView.java:3382) 
12-27 01:46:55.994: E/AndroidRuntime(311): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696) 
12-27 01:46:55.994: E/AndroidRuntime(311): at android.os.Handler.handleCallback(Handler.java:587) 
12-27 01:46:55.994: E/AndroidRuntime(311): at android.os.Handler.dispatchMessage(Handler.java:92) 
12-27 01:46:55.994: E/AndroidRuntime(311): at android.os.Looper.loop(Looper.java:123) 
12-27 01:46:55.994: E/AndroidRuntime(311): at android.app.ActivityThread.main(ActivityThread.java:4627) 
12-27 01:46:55.994: E/AndroidRuntime(311): at java.lang.reflect.Method.invokeNative(Native Method) 
12-27 01:46:55.994: E/AndroidRuntime(311): at java.lang.reflect.Method.invoke(Method.java:521) 
12-27 01:46:55.994: E/AndroidRuntime(311): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
12-27 01:46:55.994: E/AndroidRuntime(311): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
12-27 01:46:55.994: E/AndroidRuntime(311): at dalvik.system.NativeStart.main(Native Method) 

運行上面的項目後得到錯誤和味精框選項「強制關閉」...所以,如果任何人有解決方案,然後幫助... !!!感謝名單.. !!

+0

你能否在這裏粘貼一些日誌。有例外,所以很容易追蹤問題 – AAnkit

回答

1

如果你只是想傳遞字符串,所有你需要的是:

intent.putExtra("myString", selectedFromList); 

,然後在接下來的活動:

String s = getIntent().getStringExtra("myString", "some default value"); 

像往常一樣,發佈日誌。

編輯

這些轉換不能工作:

​​

 HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);     

你需要投給一個或另一個,並從你的代碼,它看起來像正確的演員陣容是HashMap。一旦你有地圖,你需要通過其key檢索string

這就是爲什麼你在第73行得到ClassCastException;您正在將HashMap投射到String

+0

上面的變更後的錯誤仍然保持不變......我的日誌文件如上面的問題所示............. –

+0

@BhushanBaviskar see我編輯上面 – LuxuryMode

+0

雅...它的工作..謝謝你.. !!! –

相關問題