2013-04-03 69 views
0

我喜欢从ListView传递选定的项目值到另一个活动。listview lazyadapter

我正在使用此代码来获取与散列图相关的详细信息,但我得到了java.lang.Integer无法转换为java.util.HashMap

public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 


       HashMap<String, String> o = (HashMap<String, String>) list.getItemAtPosition(position); 
       Toast.makeText(CustomizedListView.this, "ID '" + o.get("KEY_TITLE") + "' was clicked.", Toast.LENGTH_SHORT).show(); 

      }  

我该如何解决这个问题。我真的很感谢帮助。

在此先感谢。

public class CustomizedListView extends Activity { 
    // All static variables 
    static final String URL = "https://itunes.apple.com/us/rss/topalbums/limit=20/json"; 
    // XML node keys 
    static final String KEY_SONG = "song"; // parent node 
    static final String KEY_ID = "id"; 
    static final String KEY_TITLE = "title"; 
    static final String KEY_ARTIST = "artist"; 
    static final String KEY_DURATION = "duration"; 
    static final String KEY_THUMB_URL = "thumb_url"; 

    ListView list; 
    LazyAdapter adapter; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


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

     JSONObject json = JSONfunctions.getJSONfromURL(URL); 


     try { 
      JSONObject arr2 = json.getJSONObject("feed"); 
      JSONArray arr = arr2.getJSONArray("entry"); 

      for (int i = 0; i < arr.length(); i++) { 
       JSONObject e1 = arr.getJSONObject(i); 

       JSONArray arr3 = e1.getJSONArray("im:image"); 

       JSONObject arr8 = e1.getJSONObject("im:name"); 

       JSONObject arr10 = e1.getJSONObject("im:artist"); 

        JSONObject e12 = arr3.getJSONObject(0); 

      // creating new HashMap 
      HashMap<String, String> map = new HashMap<String, String>(); 

      map.put(KEY_THUMB_URL, e12.getString("label")); 

      map.put(KEY_ARTIST, arr8.getString("label")); 
      map.put(KEY_TITLE, arr10.getString("label")); 
      // adding HashList to ArrayList 
      songsList.add(map); 
      } 

     } catch (JSONException e) { 
      // Log.e("log_tag", "Error parsing data "+e.toString()); 
      Toast.makeText(getBaseContext(), 
        "Network communication error!", 5).show(); 
     } 


     list=(ListView)findViewById(R.id.list); 

     // Getting adapter by passing xml data ArrayList 
     adapter=new LazyAdapter(this, songsList);   
     list.setAdapter(adapter); 

     // Click event for single list row 
     list.setOnItemClickListener(new OnItemClickListener() { 

      @SuppressWarnings("unchecked") 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 


       HashMap<String, String> o = (HashMap<String, String>) list.getItemAtPosition(position); 
       Toast.makeText(CustomizedListView.this, "ID '" + o.get("KEY_TITLE") + "' was clicked.", Toast.LENGTH_SHORT).show(); 

      } 
     });  
    } 
} 

但同样的下面的代码工作:为什么?

这是一个SimpleAdapter在这种情况下。

public class Main extends ListActivity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.listplaceholder); 

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


     JSONObject json = JSONfunctions.getJSONfromURL("http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo"); 

     try{ 

      JSONArray earthquakes = json.getJSONArray("earthquakes"); 

      for(int i=0;i<earthquakes.length();i++){       
       HashMap<String, String> map = new HashMap<String, String>();  
       JSONObject e = earthquakes.getJSONObject(i); 

       map.put("id", String.valueOf(i)); 
       map.put("name", "Earthquake name:" + e.getString("eqid")); 
       map.put("magnitude", "Magnitude: " + e.getString("magnitude")); 
       mylist.add(map);    
      }  
     }catch(JSONException e)  { 
      Log.e("log_tag", "Error parsing data "+e.toString()); 
     } 

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

     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_SHORT).show(); 

      } 
     }); 
    } 
} 

回答

1

尝试更换

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

int o = (int) list.getItemAtPosition(position); 

看来你的列表视图项包含int类型值

+0

我会得到这个职位,但我需要得到的数据附加到单击的行。我从** HashMap 得到这个o =(HashMap )list.ge tItemAtPosition(position); **所以我得到与** o.get(“KEY_TITLE”)**相关的数据。 – 2013-04-04 12:35:30

+0

@詹姆斯帕特里克对你有帮助吗? – 2013-04-05 21:17:55

+0

不完全,但我不得不在自定义类中添加返回类型,这就是为什么它崩溃。但感谢您的帮助。 – 2013-04-06 21:32:57