【问题标题】:JSonarray to Arraylist and Arraylist in ListAdapter doesn't work. (In Android)ListAdapter 中的 Arraylist 和 Arraylist 的 JSONarray 不起作用。 (在安卓中)
【发布时间】:2010-11-21 20:30:08
【问题描述】:

我有一个小问题,我没看到。

我检索 Json 数据(JSONArray),我想在JSONArray 中的所有名称中创建一个List,类似这样。

List list = new ArrayList<String>();
for(int i=0;i < data.length();i++){ 
    list.add(data.getJSONObject(i).getString("names").toString());
}

我想把这个列表放在“ListView”中,所以我这样做了:

ArrayList<String> test = history_share.list;
names_list = (String[]) test.toArray();
ArrayAdapter<String> adapter = new ArrayAdapter(this, 
    android.R.layout.simple_list_item_1, names_list);
setListAdapter(adapter);

(history_share 是我创建的从 api 获取 json 数据的方法之一。 Eclipse 没有看到任何错误,我也没有。

有人可以帮帮我吗?

【问题讨论】:

  • 什么不起作用?您收到了哪些错误消息?
  • 我没有任何问题,当我启动 android 应用程序时,它会出现:“应用程序意外停止”的事情。
  • @Tsunaue:这个异常必须有一个 logcat 输出。
  • 第一个错误是java.lang.runtimeexeption(我不知道如何复制logcat)

标签: android json listview arraylist android-arrayadapter


【解决方案1】:

为什么您的方法名称中有下划线?按照惯例,方法以小写字母开头。例如myMethod()。类名以大写字母开头,如MyClass。你应该坚持下去。

另外,history_share 不是您发布代码的方法,而且您无法通过这种方式调用方法从方法中检索任何内容。

getter 方法只返回定义的成员。我很惊讶 Eclipse 没有强调这一点。您确定打开了错误检查吗?

更新: 将您的类命名为已经存在的类通常是一个非常糟糕的主意,如果您打算在某处使用原始类或派生该类的任何类,情况会变得更糟。在原来的Connection 类中,我找不到任何名为 list 的静态成员,这导致假设您已经创建了自己的 Connection 类。这不一定是这里的问题,但如果您继续这样做,将来可能会引发问题。

【讨论】:

  • 是的,我有两个类:Main.java 和 Connection.java,我这样创建了 history_share: Connection history_share = new Connection(url); history_share 是一个对象(很抱歉传达错误)
  • 嗯,是的,我不知道有一个连接类,但对于其他功能,我的连接类工作正常。这只是列表的问题。
【解决方案2】:
for(int i=0;i < data.length();i++){ 
    list.add(data.getJSONObject(i).getString("names").toString());
}


.getString("names") returns String, remove .toString()

还有,

ArrayAdapter<String> adapter = new ArrayAdapter(this, 
    android.R.layout.simple_list_item_1, names_list);

替换为

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
    android.R.layout.simple_list_item_1, names_list);

【讨论】:

    【解决方案3】:

    您可以尝试使用 ArrayListHashmap

    ArrayList<HashMap<String, String>> comunitylist = new ArrayList<HashMap<String, String>>();
    
    String url =_url + _uid + uid;
    
    JSONParstring jParser = new JSONParstring();
    
    // getting JSON string from URL
    String json = jParser.getJSONFromUrl(url,apikey);
    
    Log.e("kPN", json);
    try
    {
        JSONObject jobj = new JSONObject(json);
        Log.e("kPN", json.toString());
        System.out.print(json);
        JSONArray comarray = jobj.getJSONArray(TAG_COMMU);
    
        for(int i = 0; i <= comarray.length(); i++){
            JSONObject c = comarray.getJSONObject(i);
            Log.w("obj", c.toString());
            JSONObject d = c.getJSONObject(TAG_PERSON);
            Log.w("obj", d.toString());
            String name =d.getString(TAG_NAME);
            Log.w("name", name);
    
            String nick =d.getString(TAG_NICK);
            String home = d.getString(TAG_HOME);
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(TAG_NAME, name);
            map.put(TAG_NICK, nick);
        }
    }
    catch (JSONException ie)
    {
    
    }
    
    list=(ListView)findViewById(R.id.list);
    adapter=new Lazycommunity(this,listz);
    
    list.setAdapter(adapter);
    
    list.setOnItemClickListener(new OnItemClickListener() {
    
        @SuppressWarnings("unchecked")
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
    
            //Having Trouble with this line, how to retrieve value???
            HashMap<String, String> map2 = (HashMap<String, String>) list.getAdapter().getItem(position);
    
            Intent in = new Intent(getApplicationContext(), Communityprofile.class);
            in.putExtra(TAG_NAME, map2.get(TAG_NAME));
            in.putExtra(TAG_IMG, map2.get(TAG_IMG));
    
            startActivity(in);
        }
    });
    

    【讨论】:

    • 您应该删除 Log 呼叫,因为它们对您的回答没有帮助。
    猜你喜欢
    • 1970-01-01
    • 2021-07-16
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多