ユーザーが所有しているすべてのページのデータ(名前、カテゴリ、ファン数)をフェッチして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が正常にデータを取得するが、私はロックとたら、自分の携帯電話のロックを解除した場合にのみ、私はコードと間違っているものを見当もつかないリストビューに結果を示しています。
これはうまくいきました.......しかし、あなたがここで何をしたのか説明してください。 –
この行は、行の追加ループの後に追加されました。 adapter.notifyDataSetChanged();アダプターを変更した後、notifyDataSetChangeを呼び出すと、このメソッドはリストのビューを更新します –