ArrayAdapter 仅在我锁定和解锁 phone 后才在屏幕上显示输出
ArrayAdapter showing the output on the screen only after i lock and unlock my phone
我正在尝试获取用户在 Facebook 上拥有的所有页面的一些数据(名称、类别和 fan_count),然后使用 ArrayAdapter 在 ListView 中显示结果。
我制作了自定义阵列适配器
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;
}
这是我的页面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;}}
我已经登录到 facebook 并调用了打开 PageActivity class 的意图,其中 ArrayAdapter 应该获取数据并应该通过 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 正在成功获取数据,但在列表视图中显示结果 仅当我锁定和解锁我的 phone 一次,我不知道是什么代码错误。
我相信这是因为您在后台线程上调用 pages.add...
。
我不记得 Facebook 是否这样做了。
作为简单测试,将您的代码更改为:
new Handler().post(new Runnable() {
@Override
public void run() {
pages.add(new Page(name, category, fan_count));
}
});
如果它有效,那么你就知道这是一个线程问题,你必须从那里开始
您应该在添加所有行后调用适配器的 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();
在请求有机会完成之前,您正在适配器中显示数据。
我假设即使通过生命周期方法也保存了页面对象(不确定它的定义位置),因此新适配器的创建会考虑新数据。您可以在请求完成后尝试重新创建适配器。
我正在尝试获取用户在 Facebook 上拥有的所有页面的一些数据(名称、类别和 fan_count),然后使用 ArrayAdapter 在 ListView 中显示结果。
我制作了自定义阵列适配器
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;
}
这是我的页面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;}}
我已经登录到 facebook 并调用了打开 PageActivity class 的意图,其中 ArrayAdapter 应该获取数据并应该通过 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 正在成功获取数据,但在列表视图中显示结果 仅当我锁定和解锁我的 phone 一次,我不知道是什么代码错误。
我相信这是因为您在后台线程上调用 pages.add...
。
我不记得 Facebook 是否这样做了。
作为简单测试,将您的代码更改为:
new Handler().post(new Runnable() {
@Override
public void run() {
pages.add(new Page(name, category, fan_count));
}
});
如果它有效,那么你就知道这是一个线程问题,你必须从那里开始
您应该在添加所有行后调用适配器的 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();
在请求有机会完成之前,您正在适配器中显示数据。
我假设即使通过生命周期方法也保存了页面对象(不确定它的定义位置),因此新适配器的创建会考虑新数据。您可以在请求完成后尝试重新创建适配器。