0

I've got a little problem, and i don't see it.

I retrieve Json data (the JSONArray) and i wanted to make a List of all the names in the JSONArray, something like this.

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

And i wanted to take this list in an `ListView' so i did this :

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 is one of the method i created to take json data from an api . Eclipse doesn't see any error, and me neither.

Can somebody help me please ?

4

3 回答 3

0

您尝试使用ArrayListwith Hashmap

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);
    }
});
于 2012-07-04T05:59:56.200 回答
0

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

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

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

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

于 2010-11-21T20:45:58.820 回答
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);
于 2011-12-06T21:27:27.203 回答