P.Vamsi P.Vamsi - 15 days ago 4
Android Question

What is the wrong with this onPostexecute method in Asynctask?

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);
}
}


}


it doesn't display the list, it just displays an empty list. I am using recyclerview.adapter. There is also this problem that getItemCount() throws nullpointer exception when [return this.stores.size();] is used and the app doesn't open, when i change this line to [return this.stores == null ? 0 : stores.size();] it opens but with empty list.

public int getItemCount() {

Log.e(LOG_TAG, "stores size");
// return this.stores.size();
return this.stores == null ? 0 : stores.size();
}


when i use List view the postexecute method body is this, and it works.

protected void onPostExecute(List<Quakes> data) {
mAdapter.clear();
if (data != null && !data.isEmpty()) {
mAdapter.addAll(data);
}
}


how to correctly execute postexecute method in Asynctask that is related to recyclerview.adapter?
The JSON parsing is error free, only i am unable to load it into the adapter.

This is the adapter

public class StoreListAdapter extends 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();
}}

Answer

When you get code in onPostExecute, you should pass this data in 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);
    }

Other way,i think better, if you are making custom list adapter make getter and setter or method like addAll to update data in list adapter.

After edited Create getter and setter of ArrayList<Stores> stores and then

@Override
        protected void onPostExecute(List<Stores> data) {
            mAdapter.setStores(data);
            mAdapter.notifyDataSetChanged();
        }

Also in shopList class private ArrayList<Stores> stores= = new ArrayList<>(); may avoid null exception