2015-09-03 81 views
0

這裏是OnPostExecute這retuns列表項[1,名稱2,名稱3]:OnPostExecute項目的ListView

@Override 
    protected void onPostExecute(JSONObject json) { 
     try { 

      if (json.getString(KEY_SUCCESS) != null) { 
       String result = json.getString(KEY_SUCCESS); 
       if (Integer.parseInt(result) == 1) { 
        pDialog.setMessage("Loading View"); 
        pDialog.setTitle("Getting Friends"); 


        //JSONArray root = new JSONArray(json); 
        JSONArray friend = json.getJSONArray("users"); 
        List<String> item = new ArrayList<String>(); 

        for (int i = 0; i < friend.length(); i++) { 
         JSONObject att = (JSONObject) friend.getJSONObject(i); 

         String res = att.getString("username"); 
         item.add(res); 
        } 
        SharedPreferences FriendList = getSharedPreferences("FriendList", Context.MODE_PRIVATE); 
        SharedPreferences.Editor edit = FriendList.edit(); 
        edit.putString("KEY_FRIENDS", item.toString()); 
        edit.commit(); 
       } else { 

        pDialog.dismiss(); 

       } 

      }else { 

       pDialog.dismiss(); 

      } 

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

     pDialog.dismiss(); 


    } 

我希望它在列表視圖中,但即時stucked,因爲我不能在OnPostExecute使用ListAdapter。

public class FriendList extends ListActivity { ...  
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

    // setContentView(R.layout.activity_friend_list); 

    // listt = (TextView) findViewById(R.id.testyL); 
    // list = (TextView) findViewById(R.id.textView4); 
    // list = (ListView) findViewById(R.id.label); 
    try { 
     new GetFriends().execute(); 
    }catch (Exception e) { 
     e.printStackTrace() ; 
     Log.e("ERR ", "AsyncTASK" + e.toString()); 
    } 
    SharedPreferences FriendList = getSharedPreferences("FriendList", Context.MODE_PRIVATE); 
    String u = FriendList.getString("KEY_FRIENDS", "0"); 

我試圖把項目SharedPref但似乎我不知道如何正確,並康特retrevie到可以通過磁盤陣列/表適配器一起使用的變量。 這是問題所在:)

// Binding resources Array to ListAdapter 
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, item)); 


    ListView lv = getListView(); 

    // listening to single list item on click 
    lv.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 

      // selected item 
      String product = ((TextView) view).getText().toString(); 

      // Launching new Activity on selecting single List Item 
      Intent i = new Intent(getApplicationContext(), Logged.class); 
      // sending data to new activity 
      i.putExtra("friend", product); 
      startActivity(i); 

     } 
    }); 

} 
+0

爲什麼你不能在OnPostExecute使用ListAdapter? – wviana

+0

因爲即時通訊總新手,但我需要使用它-.- – Szamus

回答

0

變化GetFriends構造函數添加引用好友列表對象:

try { 
     new GetFriends(FriendList.this).execute(); 
    }catch (Exception e) { 
     e.printStackTrace() ; 
     Log.e("ERR ", "AsyncTASK" + e.toString()); 
    } 

類:

public class GetFriends extends [...]{ 

    private ListActivity mList; 

    public GetFriends(ListActivity list){ 

     mList = list; 
    } 
    [...] 
    @Override 
    protected void onPostExecute(JSONObject json) { 

     //set Adapter to list 
     mList. 
    } 
} 
+0

好,不過如果我有item.add我不能簡單地 ArrayAdapter 適配器=新ArrayAdapter (這一點, android.R.layout.simple_list_item_1使用這個變量,項目); ,因爲android無法解決它。 Sry愚蠢的,但我不知道該怎麼做:D – Szamus