2017-06-15 18 views
0

ユーザーが所有しているすべてのページのデータ(名前、カテゴリ、ファ​​ン数)をフェッチしてListViewに表示しようとしていますArrayAdapterを使用します。ArrayAdapter私の携帯電話をロックしてロックを解除した後にのみ画面に出力する

私はすでにFacebookのにログインし、ArrayAdapterがデータをフェッチし、すべきでなければならないPageActivityクラスを開く意図を呼ばれる私のページクラスは

ある
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;}} 

ここ

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; 

} 

私のカスタムアレイアダプタを作りました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(); 
}} 

今ArrayAdapterが正常にデータを取得するが、私はロックとたら、自分の携帯電話のロックを解除した場合にのみ、私はコードと間違っているものを見当もつかないリストビューに結果を示しています。

答えて

1

にそこからあなたをそれを取る必要があります必要がありますすべての行を追加した後、アダプタのnotifyDataSetChangeメソッドを呼び出します。 ので、あなたのコードは次のように変更する必要があります。リクエストが終了する機会を得る前に

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(); 
+0

これはうまくいきました.......しかし、あなたがここで何をしたのか説明してください。 –

+0

この行は、行の追加ループの後に追加されました。 adapter.notifyDataSetChanged();アダプターを変更した後、notifyDataSetChangeを呼び出すと、このメソッドはリストのビューを更新します –

0

これは、あなたがバックグラウンドスレッドでpages.add...と呼んでいるためだと思います。

Facebookがこれを行うかどうかはわかりません。簡単なテストとして

、これにコードを変更:

new Handler().post(new Runnable() { 
    @Override 
    public void run() { 
     pages.add(new Page(name, category, fan_count)); 
    } 
}); 

それが動作する場合は、あなたがそのスレッドの問題を知っている、とあなたは

0

あなたは、アダプタ内のデータを表示しています。

ページオブジェクト(定義されている場所がわからない)がライフサイクルメソッドで保存されていると仮定しているため、新しいアダプタを作成すると新しいデータが考慮されます。要求が完了したら、アダプターを再作成することができます。

関連する問題