Search code examples
androidfacebook-graph-apiandroid-arrayadapter

ArrayAdapter showing the output on the screen only after i lock and unlock my phone


i am trying to fetch some data (name,category and fan_count) of all the pages owned by the user on facebook and then displaying the result in a ListView using ArrayAdapter.

i made my custom array adapter

public class PageAdapter extends ArrayAdapter<Page> {
public PageAdapter(Context context, ArrayList<Page> pages) {
    super(context, 0, pages);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.list_item_layout, parent, false);
    }
    final Page currentPage = getItem(position);
    TextView nameText = (TextView) listItemView.findViewById(R.id.name);
    TextView categoryText = (TextView) listItemView.findViewById(R.id.category);
    TextView fanCountText = (TextView) listItemView.findViewById(R.id.fan_count);

    nameText.setText(currentPage.getmName());
    categoryText.setText(currentPage.getmCategory());
    fanCountText.setText(currentPage.getmFanCount());

    return listItemView;

}

here is my page class

public class Page {
private String mName;
private String mCategory;
private String mFanCount;

public Page(String name, String category, String fanCount){
    mName = name;
    mCategory = category;
    mFanCount = fanCount;
}

public String getmName(){return mName;}
public String getmCategory(){return mCategory;}
public String getmFanCount(){return mFanCount;}}

i already logged into facebook and called an intent which opens PageActivity class where the ArrayAdapter should fetch data and should show the result through a ListView.

public class PageActivity extends AppCompatActivity {
private String category, fan_count, name;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_page);

    final ArrayList<Page> pages = new ArrayList<>();

    Bundle param = new Bundle();
    param.putString("fields", "category,fan_count,name");
    new GraphRequest(
            AccessToken.getCurrentAccessToken(),
            "/me/accounts",
            param,
            HttpMethod.GET,
            new GraphRequest.Callback() {
                public void onCompleted(GraphResponse response) {
                    try {
                        JSONObject root = new JSONObject(response.getRawResponse());
                        JSONArray data = root.getJSONArray("data");

                        for(int i=0 ; i<data.length() ; i++){
                            JSONObject dataRoot = data.getJSONObject(i);
                            name = dataRoot.getString("name");
                            category = dataRoot.getString("category");
                            fan_count = dataRoot.getString("fan_count");

                            pages.add(new Page(name, category, fan_count));
                        }

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

    ListView pageListView = (ListView) findViewById(R.id.list);
    final PageAdapter adapter = new PageAdapter(this, pages);
    pageListView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
}}

now the ArrayAdapter is fetching the data successfully but shows the result in the listview only if i lock and unlock my phone once, i have no clue what is wrong with the code.


Solution

  • you should call notifyDataSetChange method of adapter after adding all rows. so your code should change like this:

    new GraphRequest(
            AccessToken.getCurrentAccessToken(),
            "/me/accounts",
            param,
            HttpMethod.GET,
            new GraphRequest.Callback() {
                public void onCompleted(GraphResponse response) {
                    try {
                        JSONObject root = new JSONObject(response.getRawResponse());
                        JSONArray data = root.getJSONArray("data");
    
                        for(int i=0 ; i<data.length() ; i++){
                            JSONObject dataRoot = data.getJSONObject(i);
                            name = dataRoot.getString("name");
                            category = dataRoot.getString("category");
                            fan_count = dataRoot.getString("fan_count");
    
                            pages.add(new Page(name, category, fan_count));
    
                        }
                        //**********this line added!!!!!*************
                        adapter.notifyDataSetChanged();
    
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
    ).executeAsync();