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
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 に答える
0

ArrayListでこれを試してみてください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 に答える