Search code examples
androidlistviewandroid-asynctask

OnPostExecute item to ListView


Here is OnPostExecute that returns List<String>item [name1,name2,name3]:

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

I want it in list view but I'm stuck because I can't use ListAdapter in 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");

I tried to put item to SharedPref but it seems I don't know how to properly retrieve it and counter to variable that can be used by Array/List Adapter.

Here is the problem :)

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

        }
    });

}

Solution

  • Change GetFriends constructor add reference to FriendList object:

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

    class:

    public class GetFriends extends [...]{
    
       private ListActivity mList;
    
       public GetFriends(ListActivity list){
    
          mList = list;
       }
       [...]
       @Override
       protected void onPostExecute(JSONObject json) {
    
          //set Adapter to list
          mList.
       }  
    }