Android - Populating Listview With Data From Jsonarray
Situation: I'm getting a JSONObject containing user's playlist from the server. I want to display this data in a ListView, there are 288 audio files in my test case. The JSON is pa
Solution 1:
In getView method you are not populating the View again if it's a recycled one. Therefore once you scroll outside the screen it will start displaying the old ones. You are also not setting/retrieving the ViewHolder. Please try the following implementation (not tested).
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
Viewrow= convertView;
ViewHolderholder=null;
aud = data.get(position);
//it's giving a CORRECT position if I hardcode the index, like data.get(99);if (row == null) {
holder = newViewHolder();
LayoutInflaterinflater= ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder.play = (Button) row.findViewById(R.id.btn_list_play);
holder.imgSaved = (ImageView) row
.findViewById(R.id.img_list_audio_saved);
holder.tvArtist = (TextView) row
.findViewById(R.id.tvListItemArtist);
holder.tvTitle = (TextView) row.findViewById(R.id.tvListItemSong);
row.setTag(holder);
} else {
holder = (ViewHolder)row.getTag();
}
holder.tvArtist.setText(aud.getArtist());
holder.tvTitle.setText(aud.getTitle());
return row;
}
Solution 2:
I have done some modifications in your getView method.Replace it and check it.
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
Viewrow= convertView;
ViewHolder holder;
aud = data.get(position);
//it's giving a CORRECT position if I hardcode the index, like data.get(99);if (row == null) {
holder = newViewHolder();
LayoutInflaterinflater= ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder.play = (Button) row.findViewById(R.id.btn_list_play);
holder.imgSaved = (ImageView) row.findViewById(R.id.img_list_audio_saved);
holder.tvArtist = (TextView) row .findViewById(R.id.tvListItemArtist);
holder.tvTitle = (TextView) row.findViewById(R.id.tvListItemSong);
row.setTag(holder);
}
else
{
holder = (ViewHolder ) row.getTag();
}
holder.tvArtist.setText(aud.getArtist());
holder.tvTitle.setText(aud.getTitle());
return row;
}
Solution 3:
if (convertView == null)// if null
{
convertView = mInflater.inflate(R.layout.list, null);//inflate view
} else
{
holder = (ViewHolder) convertView.getTag();// set tag to holder.
}
Make those changes.
Solution 4:
If you will remove this line:
Viewrow= convertView;
and inflate the view every time, then this problem will go away. I did as well encountered this affect a couple of times as described here:
How to save state of changed ImageView in Listview row, after it' has disappeared from screen?
actualy i dont know why this happens because using the convertView is recommended.
Update:
try this one:
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
View row;
ViewHolderholder=newViewHolder();
LayoutInflaterinflater= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layoutResourceId, parent, false);
holder.play = (Button) row.findViewById(R.id.btn_list_play);
holder.imgSaved = (ImageView) row.findViewById(R.id.img_list_audio_saved);
holder.tvArtist = (TextView) row.findViewById(R.id.tvListItemArtist);
holder.tvTitle = (TextView) row.findViewById(R.id.tvListItemSong);
holder.tvArtist.setText(aud.getArtist());
holder.tvTitle.setText(aud.getTitle());
return row;
}
Post a Comment for "Android - Populating Listview With Data From Jsonarray"