Cannot Update Listview Adapter In The Volley Request Listener In Android
Solution 1:
Root cause is -
privatefinalArrayList<Podcast> values
your values
in the adapter are not updating with the new objects from the response. You should check that.
add the required public
method addAll()
to your own ArrayAdapter
implementation, you don't have the visibility on private member in android.widget.ArrayAdapter
so need use existing public API ArrayAdapter.add()
to add every single element within a loop. But remember, each add()
will result notifyDataSetChanged()
which might lead to crash depending on the number of addition.
So the sum up is you need to implement your own addAll()
in your implementation. Or you can come up with your own method. There are plenty of ways to do that.
Update
You need to override -
@Override
publicintgetCount () {
return values.size();
}
Solution 2:
Please fill arrayList before send PodcastListAdapter class and use adapter below code.
if (adapter != null)
adapter.notifyDataSetChanged();
else {
adapter = new PodcastListAdapter(getActivity(),new ArrayList<Podcast>());
listView.setAdapter(adapter);
}
Solution 3:
In the ArrayAdapter
constructor why are you passing -1 ? Pass R.layout.podcast_list_row
in super constructor. And use the same in getView
.
super(context,R.layout.podcast_list_row,values);
Edit:
Pass an instance of ArrayList<Podcast>
say podCastList in adapter constructor adapter
in onCreateView
and in onResponse
add new values to that instance podCastList.addAll(items)
then call notifyDataSetChanged
publicclassPodcastListFragmentextendsFragment {
privateView view;
privateSwipeRefreshLayout refreshLayout;
privateListView listView;
private int channelId;
privateArrayAdapter adapter;
privateArrayList<Podcast>podcastList = newArrayList<Podcast>()
@Nullable@OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.podcast_list_fragment,container,false);
adapter = newPodcastListAdapter(getActivity(),podcastList);
listView = (ListView)view.findViewById(R.id.listview_podcast_list);
listView.setAdapter(adapter);
TextView textView = (TextView)view.findViewById(R.id.list_title);
textView.setText(getArguments().getString("title"));
this.channelId = getArguments().getInt("channel");
updateListItems();
return view;
}
privatevoidupdateListItems()
{
String url = getResources().getString(R.string.api_endpoint)+"podcast";
if(channelId>0)
{
url = url + "?channel="+String.valueOf(channelId);
}
JsonObjectRequest jsonObjectRequest = newJsonObjectRequest(url, null, newResponse.Listener<JSONObject>() {
@OverridepublicvoidonResponse(JSONObject response) {
try{
String status = response.getString("status");
if(status.equals(getResources().getString(R.string.success)))
{
JSONArray items = response.getJSONArray("items");
ArrayList<Podcast> podcasts = newArrayList<Podcast>();
if(items.length()>0)
{
for(int i=0;i<items.length();i++)
{
Podcast podcast = Podcast.fromJsonObject(items.getJSONObject(i));
podcasts.add(podcast);
}
}
podcastList.addAll(podcasts);
adapter.notifyDataSetChanged();
}
}
catch (JSONException e)
{
Toast.makeText(getActivity().getBaseContext(),"Error encountered in transferring data from server",Toast.LENGTH_SHORT).show();
}
}
}, newResponse.ErrorListener() {
@OverridepublicvoidonErrorResponse(VolleyError error) {
Toast.makeText(getActivity().getBaseContext(),"Error encountered in transferring data from server",Toast.LENGTH_SHORT).show();
}
});
MySingleton.getInstance(getActivity()).addToRequestQueue(jsonObjectRequest);
}
}
Solution 4:
I found the error. I should have posted the fragment xml layout file as well. The problem was I put the list view layout inside swipe refresh layout. That is the problem.
This is my old xml layout file
<android.support.v4.widget.SwipeRefreshLayoutandroid:id="@+id/podcast_list_refresh_layout"android:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:id="@+id/list_title"android:layout_width="wrap_content"android:layout_height="wrap_content" /><ListViewandroid:id="@+id/listview_podcast_list"android:layout_width="match_parent"android:layout_height="match_parent"></ListView></android.support.v4.widget.SwipeRefreshLayout>
This is how I fixed it removing the SwipeRefreshLayout
<TextViewandroid:id="@+id/list_title"android:layout_width="wrap_content"android:layout_height="wrap_content" /><ListViewandroid:id="@+id/listview_podcast_list"android:layout_width="match_parent"android:layout_height="match_parent"></ListView>
Post a Comment for "Cannot Update Listview Adapter In The Volley Request Listener In Android"