【发布时间】:2016-11-22 11:43:26
【问题描述】:
public class ShopsList extends AppCompatActivity {
private RecyclerView listView;
private StoreListAdapter mAdapter;
private ArrayList<Stores> stores;
public static final String LOG_TAG = ShopsList.class.getName();
private String sampleURL = "http://104.199.230.125/stores/1.json/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shops_list);
StoresAsyncTask task = new StoresAsyncTask();
task.execute(sampleURL);
mAdapter = new StoreListAdapter(this, R.layout.list_item_layout, stores);
listView = (RecyclerView) findViewById(R.id.store_list);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
listView.setLayoutManager(layoutManager);
listView.setAdapter(mAdapter);
private class StoresAsyncTask extends AsyncTask<String, Void, List<Stores>> {
@Override
protected List<Stores> doInBackground(String... URLs) {
if (URLs.length < 1 || URLs[0] == null) {
Log.e("QueryUtils", "URL is is null");
return null;
}
Log.e("QueryUtils", "URL is not null" + URLs[0]);
return QueryHandler.fetchStoreData(URLs[0]);
}
@Override
protected void onPostExecute(List<Stores> data) {
mAdapter.notifyDataSetChanged();
listView.setAdapter(mAdapter);
super.onPostExecute(data);
}
}
}
它不显示列表,它只显示一个空列表。我正在使用 recyclerview.adapter。还有一个问题是 getItemCount() 在使用 [return this.stores.size();] 并且应用程序没有打开时抛出空指针异常,当我将此行更改为 [return this.stores == null 时? 0 : stores.size();] 它打开但列表为空。
public int getItemCount() {
Log.e(LOG_TAG, "stores size");
// return this.stores.size();
return this.stores == null ? 0 : stores.size();
}
当我使用列表视图时,postexecute 方法体是这样的,它可以工作。
protected void onPostExecute(List<Quakes> data) {
mAdapter.clear();
if (data != null && !data.isEmpty()) {
mAdapter.addAll(data);
}
}
如何正确执行与recyclerview.adapter相关的Asynctask中的postexecute方法? JSON解析没有错误,只是我无法将它加载到适配器中。
这是适配器
公共类 StoreListAdapter 扩展 RecyclerView.Adapter {
private ArrayList<Stores> stores = new ArrayList<>();
private int itemResource;
private Context context;
public StoreListAdapter(Context context, int itemResource, ArrayList<Stores> stores) {
this.stores = stores;
this.itemResource = itemResource;
this.context = context;
}
@Override
public storeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.list_item_layout, parent, false);
return new storeViewHolder(this.context, view);
}
@Override
public void onBindViewHolder(storeViewHolder holder, int position) {
Stores stores = this.stores.get(position);
holder.bindStoreData(stores);
}
@Override
public int getItemCount() {
Log.e(LOG_TAG, "stores size");
return this.stores.size();
}}
【问题讨论】:
-
我想你忘了放 mAdapter.notifydatasetchanged();
-
请看第一个代码onPostExecute方法,是不是mAdapter.notifydatasetchanged();去?注意:最后一个代码 onPostExecute 方法工作正常,与列表视图相关。上述与 recyclerview 相关的 onPostExecute 方法代码不起作用。请在那里提出任何更改建议。
-
@P.Vamsi 尝试评论 listView.setAdapter(mAdapter);在异步任务之前
-
问题在于为适配器设置新数据并使用 notifyDataSetChange 进行刷新,检查 readyandroid 给出的答案
-
我试过了,但还是一样,我想我的适配器有错误。请检查适配器是否正确,它在返回 this.stores.size() 时抛出空指针异常;当我用 return this.stores == null 替换它时? 0 : 商店.size();或返回 0;列表为空,当我用 return 1 替换时,它会在 bindviewholders [Stores stores = this.stores.get(position);] 行抛出空指针异常
标签: android android-asynctask android-recyclerview