当我使用简单的适配器时,为什么每次都不会出现listview?

时间:2015-07-30 01:44:03

标签: android listview android-listview

我从网上获得了String t 作为Json并解析了它。然后我使用SimpleAdapter添加我在ListView.But上运行的数据,我遇到了问题listView有时消失,有时出现成功。为什么会发生这种情况?

private void parseData(String t){
                // TODO Auto-generated method stub
                try {
                        JSONObject jsonObject = new JSONObject(t);
                        JSONArray datas = (JSONArray)jsonObject.getJSONArray("datas");
                        HashMap<String, String> item;
                        for(int i = 0;i<datas.length();i++){
                            JSONObject info = (JSONObject)datas.getJSONObject(i);
                            String number = info.getString("number");
                            String stocktakingTime = info.getString("stocktakingTime");
                            JSONObject category = info.getJSONObject("category");
                            String text = category.getString("text");
                                item = new HashMap<String, String>();
                                item.put("number", number);
                                item.put("stocktakingTime", stocktakingTime);
                                item.put("text",text);  
                                goodsList.add(item);
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
            }       


public void initWidget() {
    SimpleAdapter adapter = new SimpleAdapter(this, goodsList,
            R.layout.check_task_list_view, new String[] {
                    "number", "stocktakingTime", "text" },
            new int[] { R.id.checkNumber, R.id.stocktakingTime,
                    R.id.checkText});
    listView = new ListView(this);
    listView.setAdapter(adapter);
    listView.setPadding(10, 10, 10, 10);
    listView.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub

            switch (position) {
            case 0:
                Intent it = new Intent(CheckTaskActivity.this,CheckTaskCustomSellerActivity.class);
                startActivity(it);
                break;
            case 1:
                Intent it1 = new Intent(CheckTaskActivity.this,CheckTaskCustomSellerActivity.class);
                startActivity(it1);
                break;


            default:
                break;
            }

        }

    });

    LinearLayout checkTaskList = (LinearLayout) findViewById(R.id.checkTaskList);
    checkTaskList.addView(listView);
    bindListener();
}

2 个答案:

答案 0 :(得分:0)

在哪里找到&#34; goodsList&#34;变量?它是创建表单的列表变量 哈希地图(List<? extends Map<String, ?>> item )

然后你犯了一个错误 以下代码

for(int i = 0;i<datas.length();i++){
                        JSONObject info = (JSONObject)datas.getJSONObject(i);
                        String number = info.getString("number");
                        String stocktakingTime = info.getString("stocktakingTime");
                        JSONObject category = info.getJSONObject("category");
                        String text = category.getString("text");
                            item = new HashMap<String, String>();
                            item.put("number", number);
                            item.put("stocktakingTime", stocktakingTime);
                            item.put("text",text);  
                            goodsList.add(item);
                    }

每次for循环运行时都会初始化项变量 那么只有一个项目可以包含h​​asmap。

做到.....................

HashMap<String, String> item = new HashMap<String, String>();
                        for(int i = 0;i<datas.length();i++){
                            JSONObject info = (JSONObject)datas.getJSONObject(i);
                            String number = info.getString("number");
                            String stocktakingTime = info.getString("stocktakingTime");
                            JSONObject category = info.getJSONObject("category");
                            String text = category.getString("text");

                                item.put("number", number);
                                item.put("stocktakingTime", stocktakingTime);
                                item.put("text",text);  
                                goodsList.add(item);
                        }

答案 1 :(得分:0)

JSONArray数据可能为空,因此您无法填充goodsList,然后listview没有项目所以请检查数据JSONArray