OnPostExecute项目到ListView

时间:2015-09-03 19:31:20

标签: android listview android-asynctask

这是OnPostExecute,它返回Listitem [name1,name2,name3]:

NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Template"];
NSArray *templateArray = [managedObjectContext executeFetchRequest:fetchRequest error:nil];
NSManagedObject *firstTemplate = [templateArray firstObject];
self.nameStringString = [firstTemplate valueForKey:@"name"];
NSLog(@"%@ viewDidLoad String", self.nameStringString);
self.testLabel.text = self.nameStringString;

我想要它在列表视图中,但我被困,因为我不能在OnPostExecute中使用ListAdapter。

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


    }

我试图把项目放到SharedPref但似乎我不知道如何正确地转发它并且conter到可以由Array / List Adapter使用的变量。 这是问题:)

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

1 个答案:

答案 0 :(得分: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.
   }  
}