Reputation: 1092
I am creating a listview inside a Recyclerview. I am populating Recyclerview with data which is fetched online. The RecyclerView is populating properly. I want to populate the listview at the run time when a button will be clicked. I am defining onClickListener for the button and on button click I am getting the data and populating an adapter which will be set to the listview. But the data is not reflecting in the listview.
onClickListener for the Button
holder.arrow_up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new LoadClothes().execute(order.getOrderId());
}
});
Asynctask for Loading details for listview
private class LoadClothes extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... strings) {
String string = "";
try {
Request request = new Request.Builder()
.url(ORDERCLOTHES)
.addHeader("token", BaseApplication.getToken())
.addHeader("orderId", strings[0])
.build();
Response response = client.newCall(request).execute();
string = response.body().string();
Logger.d(string);
} catch (IOException e) {
e.printStackTrace();
}
return string;
}
@Override
protected void onPostExecute(String string) {
super.onPostExecute(string);
Logger.d(string);
Gson gson = new Gson();
Type type = new TypeToken<List<OrderedCloth>>() {
}.getType();
cloths = gson.fromJson(string, type);
viewHolder.listView.setVisibility(ListView.VISIBLE);
CustomListOrderAdapter adapter = new CustomListOrderAdapter(context, cloths);
viewHolder.listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
Upvotes: 1
Views: 7198
Reputation: 113
You don't have to do that. Just make a recycler view and inside it make a LinearLayout with orientation "vertical" instead of a listView. Now in the OnBindViewHolder of the recyclerView just run a loop till list.size() and keep inflating the view in linearLayout with addView(). Before the for loop just removeAllView(). This will work and won't make the scrolling a bad experience.
Upvotes: 5
Reputation: 10537
Are you sure you want to do that? That seems like a design pattern problem. If you are going to show additional data fetched with an HTTP request, a better way to do it could be another activity with the details.
However, I think the problem is the "viewHolder", where are you getting that variable?
A simple way to solve it seems like passing the ListView to the AsyncTask:
private class LoadClothes extends AsyncTask<String, Void, String> {
private ListView listView;
public LoadCloathes(ListView listView) {
this.listView = listView;
}
//...
@Override
protected void onPostExecute(String string) {
super.onPostExecute(string);
//mostly the same but
//make sure somehow listview is still available
listView.setAdapter...
}
}
Then run the AsyncTask:
new LoadClothes(listView).execute(order.getOrderId());
As you can see an AsyncTask can take params in the constructor, the arguments in the execute()
method are to support the AsyncTask cycle, but is still a Java class.
Upvotes: 0