简体   繁体   English

Asynctask中的基本适配器不起作用

[英]Base Adapter in Asynctask not working

I am working with facebook app for my project and I'm getting a json from the graph API. 我正在为我的项目使用facebook应用程序,并且从graph API获取json。 I have a custom listView with hashMap, but when I run, the list wont populate but there isn't any errors. 我有一个带有hashMap的自定义listView,但是当我运行时,该列表不会填充,但没有任何错误。 please help me. 请帮我。

here are the codes: 这是代码:

public class PageFeedHome extends Fragment {

ArrayList<HashMap<String, String>> feedList;
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_MESSAGE = "message";
private String feedMessage;
ListView listView;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.feed_home_activity,
            container, false);

    listView = (ListView) view.findViewById(R.id.feed_lv);
    feedList = new ArrayList<HashMap<String, String>>();

    new LoadPosts().execute();

    BaseAdapter adapter = new SimpleAdapter(getActivity(), feedList,
    R.layout.feed_item_view, new String[] { TAG_MESSAGE, TAG_NAME,
    TAG_ID }, new int[] { R.id.message, R.id.author,
    R.id.id_tv });

     listView.setAdapter(adapter);
     adapter.notifyDataSetChanged();

    return view;
}

private class LoadPosts extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        Session.openActiveSession(getActivity(), true,
                new Session.StatusCallback() {

                    // callback when session changes state
                    @Override
                    public void call(Session session, SessionState state,
                            Exception exception) {
                        if (session.isOpened()) {

                            new Request(session, "/163340583656/feed",
                                    null, HttpMethod.GET,
                                    new Request.Callback() {
                                        public void onCompleted(
                                                Response response) {
                                            /* handle the result */
                                            Log.i("PostFeedResponse", response.toString());
                                            try {
                                                GraphObject graphObj = response
                                                        .getGraphObject();
                                                JSONObject json = graphObj
                                                        .getInnerJSONObject();
                                                JSONArray jArray = json
                                                        .getJSONArray("data");
                                                for (int i = 0; i < jArray.length(); i++) {
                                                    JSONObject currObj = jArray.getJSONObject(i);
                                                    final String feedId = currObj.getString("id");
                                                    if (currObj.has("message")) {
                                                        feedMessage = currObj.getString("message");
                                                    } else if (currObj.has("story")) {
                                                        feedMessage = currObj.getString("story");
                                                    } else {
                                                        feedMessage = "Posted a something";
                                                    }
                                                    JSONObject fromObj = currObj.getJSONObject("from");
                                                    String from = fromObj.getString("name");

                                                    HashMap<String, String> feed = new HashMap<String, String>();
                                                    feed.put(TAG_ID, feedId);
                                                    feed.put(TAG_MESSAGE, feedMessage);
                                                    feed.put(TAG_NAME, from);

                                                    feedList.add(feed);
                                                }
                                            } catch (JSONException e) {
                                                // TODO Auto-generated catch block
                                                e.printStackTrace();
                                            }
                                        }
                                    }).executeAsync();

                        }
                    }
                });

        return null;
    }

}

}

Move this code : 移动此代码:

BaseAdapter adapter = new SimpleAdapter(getActivity(), feedList,
R.layout.feed_item_view, new String[] { TAG_MESSAGE, TAG_NAME,
TAG_ID }, new int[] { R.id.message, R.id.author,
R.id.id_tv }); //initialize adapter

 listView.setAdapter(adapter); //set adapter
 adapter.notifyDataSetChanged();

To the AsyncTask's onPostExecute to make sure the feedList is already inserted from the onBackground . AsyncTask's onPostExecute以确保feedList已经从插入onBackground

Or if you prefer to initialize your adapter and setting the adapter to the listview in the onCreate, just move adapter.notifyDataSetChanged(); 或者,如果您希望初始化adapter并将adapter设置为onCreate中的listview ,则只需移动adapter.notifyDataSetChanged(); to the onPostExecute . onPostExecute This is recommended if you call the AsyncTask multiple times, because theres no need to initialize and set the adapter multiple times. 如果调用此建议AsyncTask多次,因为世界上没有需要初始化,并多次设置适配器。 (see my comment in the code) (请参阅我在代码中的注释)

ArrayList<HashMap<String, String>> feedList;
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_MESSAGE = "message";
private String feedMessage;
ListView listView;
BaseAdapter adapter;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.feed_home_activity,
        container, false);

listView = (ListView) view.findViewById(R.id.feed_lv);
feedList = new ArrayList<HashMap<String, String>>();
BaseAdapter adapter = new SimpleAdapter(getActivity(), feedList,
R.layout.feed_item_view, new String[] { TAG_MESSAGE, TAG_NAME,
TAG_ID }, new int[] { R.id.message, R.id.author,
R.id.id_tv });

 listView.setAdapter(adapter);

 new LoadPosts().execute();




 return view;
 }

  private class LoadPosts extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
    // TODO Auto-generated method stub

    Session.openActiveSession(getActivity(), true,
            new Session.StatusCallback() {

                // callback when session changes state
                @Override
                public void call(Session session, SessionState state,
                        Exception exception) {
                    if (session.isOpened()) {

                        new Request(session, "/163340583656/feed",
                                null, HttpMethod.GET,
                                new Request.Callback() {
                                    public void onCompleted(
                                            Response response) {
                                        /* handle the result */
                                        Log.i("PostFeedResponse", response.toString());
                                        try {
                                            GraphObject graphObj = response
                                                    .getGraphObject();
                                            JSONObject json = graphObj
                                                    .getInnerJSONObject();
                                            JSONArray jArray = json
                                                    .getJSONArray("data");
                                            for (int i = 0; i < jArray.length(); i++) {
                                                JSONObject currObj = jArray.getJSONObject(i);
                                                final String feedId = currObj.getString("id");
                                                if (currObj.has("message")) {
                                                    feedMessage = currObj.getString("message");
                                                } else if (currObj.has("story")) {
                                                    feedMessage = currObj.getString("story");
                                                } else {
                                                    feedMessage = "Posted a something";
                                                }
                                                JSONObject fromObj = currObj.getJSONObject("from");
                                                String from = fromObj.getString("name");

                                                HashMap<String, String> feed = new HashMap<String, String>();
                                                feed.put(TAG_ID, feedId);
                                                feed.put(TAG_MESSAGE, feedMessage);
                                                feed.put(TAG_NAME, from);

                                                feedList.add(feed);
                                            }
                                        } catch (JSONException e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }
                                    }
                                }).executeAsync();

                    }
                }
            });

     return null;

   }
   @Override
    protected void onPostExecute(Void result) 
    {
        super.onPostExecute(result);
       Toast.makeText(getApplicationContext(), ""+feedList.size(), Toast.LENGTH_LONG).show();
        adapter.notifyDataSetChanged();
    }

  }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM