列表视图lazyadapter [英] listview lazyadapter

查看:257
本文介绍了列表视图lazyadapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢从ListView控件传递所选项目的值另一项活动。

我用这code来获得与HashMap的相关细节,但我得到的 java.lang.Integer中不能转换到的java.util.HashMap

 公共无效onItemClick(适配器视图<>母公司,观景,
                    INT位置,长的id){
                HashMap的<字符串,字符串> O =(HashMap的<字符串,字符串>)list.getItemAtPosition(位置);
                Toast.makeText(CustomizedListView.this,ID+ o.get(KEY_TITLE)+'被单击。,Toast.LENGTH_SHORT).show();            }

我如何解决它。我真的AP preciate的帮助。

先谢谢了。

 公共类CustomizedListView延伸活动{
    //所有静态变量
    静态最终字符串URL =htt​​ps://itunes.apple.com/us/rss/topalbums/limit=20/json;
    // XML节点键
    静态最后弦乐KEY_SONG =歌; //父节点
    静态最后弦乐KEY_ID =ID;
    静态最后弦乐KEY_TITLE =称号;
    静态最后弦乐KEY_ARTIST =艺术家;
    静态最后弦乐KEY_DURATION =时间;
    静态最后弦乐KEY_THUMB_URL =thumb_url;    ListView控件列表;
    LazyAdapter适配器;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        ArrayList的<&HashMap的LT;字符串,字符串>> songsList =新的ArrayList<&HashMap的LT;字符串,字符串>>();        JSONObject的JSON = JSONfunctions.getJSONfromURL(URL);
        尝试{
            JSONObject的ARR2 = json.getJSONObject(饲料);
            JSONArray ARR = arr2.getJSONArray(项);            的for(int i = 0; I< arr.length();我++){
                的JSONObject E1 = arr.getJSONObject(ⅰ);                JSONArray ARR3 = e1.getJSONArray(IM:图像);                JSONObject的arr8 = e1.getJSONObject(IM:名称);                JSONObject的arr10 = e1.getJSONObject(IM:艺术家);                    的JSONObject E12 = arr3.getJSONObject(0);            //创建新的HashMap
            HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();            map.put(KEY_THUMB_URL,e12.getString(标签));            map.put(KEY_ARTIST,arr8.getString(标签));
            map.put(KEY_TITLE,arr10.getString(标签));
            //添加HashList到ArrayList的
            songsList.add(地图);
            }        }赶上(JSONException E){
            // Log.e(log_tag,错误分析数据+ e.toString());
            Toast.makeText(getBaseContext(),
                    网络通信错误!,5).​​show();
        }
        名单=(ListView控件)findViewById(R.id.list);        //通过传递XML数据的ArrayList获取适配器
        适配器=新LazyAdapter(这一点,songsList);
        list.setAdapter(适配器);        // Click事件的单排列表
        list.setOnItemClickListener(新OnItemClickListener(){            @燮pressWarnings(未登记)
            @覆盖
            公共无效onItemClick(适配器视图<>母公司,观景,
                    INT位置,长的id){
                HashMap的<字符串,字符串> O =(HashMap的<字符串,字符串>)list.getItemAtPosition(位置);
                Toast.makeText(CustomizedListView.this,ID+ o.get(KEY_TITLE)+'被单击。,Toast.LENGTH_SHORT).show();            }
        });
    }
}

但同样code节的工作原理:为什么?

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

 公共类主要扩展ListActivity {
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.listplaceholder);        ArrayList的<&HashMap的LT;字符串,字符串>> MYLIST =新的ArrayList<&HashMap的LT;字符串,字符串>>();
        JSON的JSONObject = JSONfunctions.getJSONfromURL(\"http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo\");        尝试{            JSONArray地震= json.getJSONArray(地震);            的for(int i = 0; I< earthquakes.length();我++){
                HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
                JSONObject的E = earthquakes.getJSONObject(I)                map.put(ID,将String.valueOf(i)段);
                map.put(名,地​​震的名字:+ e.getString(eqid));
                map.put(大小,震级:+ e.getString(大小));
                mylist.add(地图);
            }
        }赶上(JSONException E){
             Log.e(log_tag,错误分析数据+ e.toString());
        }        ListAdapter适配器=新SimpleAdapter(这一点,MYLIST,R.layout.main,
                        新的String [] {名,大小},
                        新的INT [] {R.id.item_title,R.id.item_subtitle});        setListAdapter(适配器);        最终的ListView LV = getListView();
        lv.setTextFilterEnabled(真);
        lv.setOnItemClickListener(新OnItemClickListener(){
            公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
                @燮pressWarnings(未登记)
                HashMap的<字符串,字符串> O =(HashMap的<字符串,字符串>)lv.getItemAtPosition(位置);
                Toast.makeText(Main.this,ID+ o.get(ID)+'被点击了。Toast.LENGTH_SHORT).show();            }
        });
    }
}


解决方案

尝试更换

 的HashMap<字符串,字符串> O =(HashMap的<字符串,字符串>)list.getItemAtPosition(位置);

  INT O =(int)的list.getItemAtPosition(位置);

看来你的列表视图项包含 INT 键入值

I like to pass selected item value from ListView to another activity.

I am using this code to get the details associated with the hashmap but I get java.lang.Integer cannot be cast to 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(); 

            }     

How do I fix it.I really appreciate the help.

Thanks in Advance.

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(); 

            }
        });     
    }   
}

BUT the same code BELOW works : WHY ?

It is a SimpleAdapter in this case.

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(); 

            }
        });
    }
}

解决方案

try to replace

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

with

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

it seems your listview items contains the int type values

这篇关于列表视图lazyadapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆