对ListAdapter中的Arraylist和Arraylist的JSonarray不起作用。 (在Android中)

问题描述 投票:0回答:3

我有一点问题,我没看到。

我检索Json数据(JSONArray),我想制作List中所有名字的JSONArray,就像这样。

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 json listview arraylist android-arrayadapter
3个回答
0
投票

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

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

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

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


0
投票
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);

0
投票

你使用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);
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.