OnPostExecute 项目到 ListView

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

这是返回

List<String>item
[name1,name2,name3]
:

的 OnPostExecute
@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();
    }

我想要它在列表视图中,但我被卡住了,因为我无法在

ListAdapter
中使用
OnPostExecute

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 但似乎我不知道如何正确检索它并与数组/列表适配器可以使用的变量相对应。

问题来了:)

    // 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);

        }
    });

}
android listview android-asynctask
1个回答
0
投票

更改 GetFriends 构造函数添加对 FriendList 对象的引用:

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.
   }  
}
© www.soinside.com 2019 - 2024. All rights reserved.