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。当使用[return this.stores.size();]并且应用程序没有打开时,getItemCount()会抛出nullpointer异常,当我将此行更改为[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();
}
当我使用List视图时,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();
}}
答案 0 :(得分:1)
当你在onPostExecute中获得代码时,你应该在List Adapter
中传递这些数据@Override
protected void onPostExecute(List<Stores> data) {
mAdapter = new StoreListAdapter(this, R.layout.list_item_layout, data);
listView.setAdapter(mAdapter);
super.onPostExecute(data);
}
其他方式,我认为更好,如果您正在制作自定义列表适配器使getter和setter或方法如addAll更新列表适配器中的数据。
修改后
创建ArrayList<Stores> stores
然后
@Override
protected void onPostExecute(List<Stores> data) {
mAdapter.setStores(data);
mAdapter.notifyDataSetChanged();
}
同样在shopList类private ArrayList<Stores> stores= = new ArrayList<>();
中可以避免空例外
答案 1 :(得分:0)
您已经以编程方式回答了自己的问题:
你的解决方案1:
@Override
protected void onPostExecute(List<Stores> data) {
mAdapter.notifyDataSetChanged();
listView.setAdapter(mAdapter);
super.onPostExecute(data);
}
你的解决方案2:
@Override
protected void onPostExecute(List<Quakes> data) {
mAdapter.clear();
if (data != null && !data.isEmpty()) {
mAdapter.addAll(data);
}
}
在解决方案1中,您再次将适配器添加到listview,但是将列表数据更新到适配器。所以这就是你在这里犯的错误,在解决方案2中得到了解决。
结论:onPostExecute没有问题。问题是将更新的数据传递给适配器并使notifyDataDetChange将更新的数据应用于listview。
因此,通过创建您使用mAdapter.addAll(data / new data /)的方法,然后执行notifyDataSetChange()以使用listview刷新新更新的数据,将数据添加到适配器。
@Override
protected void onPostExecute(List<Quakes> data) {
if (data != null && !data.isEmpty()) {
mAdapter.addAll(data);
mAdapter.notifyDataSetChange();
}
}