情况:我JSONObject
从服务器获取包含用户的播放列表。我想在一个中显示这个数据ListView
,我的测试用例中有 288 个音频文件。JSON 被正确解析,大小等于 288。我创建了一个帮助类并ArrayList
使用它的对象参数化。问题:我的条目中正好有 288 个项目,ListView
但正确的条目 Artist - Title 上升到索引 13(不知道为什么),然后它们被重复。所以我在列表中没有 288 个不同的条目,但我只有 14 个条目重复到列表末尾。
更新:我删除了 if 语句,它解决了问题,但 RAM 消耗增加了 3MB。有什么优化建议吗?
public class AudioList extends ListActivity {
private ListView lv;
private JSONObject usersPlaylist, singleJSONItem;
private JSONArray responseJSONArray;
private SharedPreferences prefs;
private PlaylistItem audioList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audio_list);
init();
ArrayList<PlaylistItem> playlist = new ArrayList<PlaylistItem>();
try {
usersPlaylist = Utils.retrieveJsonObjectFromUrl(new URL(
APP_CONSTANTS.REQUEST_AUDIO_LIST(prefs)), this);
responseJSONArray = usersPlaylist.getJSONArray("response");
for (int i = 0; i < responseJSONArray.length(); i++) {
singleJSONItem = responseJSONArray.getJSONObject(i);
audioList = new PlaylistItem(singleJSONItem);
playlist.add(audioList);
}
Toast.makeText(getApplicationContext(),
Integer.toString(playlist.size()), Toast.LENGTH_LONG)
.show(); //there are 288 entries, JSONArray parsed correctly
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
AudioListAdapter adapter = new AudioListAdapter(this,
R.layout.playlist_item, playlist);
lv.setAdapter(adapter);
}
private void init() {
lv = getListView();
lv.setTranscriptMode(0x00000000);
lv.setDividerHeight(1);
lv.setSmoothScrollbarEnabled(true);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
}
播放列表项类:
public class PlaylistItem {
private String artist, title;
private JSONObject obj;
public PlaylistItem(JSONObject obj) {
this.obj = obj;
}
public PlaylistItem(){
}
public String getArtist() {
try {
artist = obj.getString("artist");
} catch (JSONException e) {
e.printStackTrace();
}
return artist;
}
public String getTitle() {
try {
title = obj.getString("title");
} catch (JSONException e) {
e.printStackTrace();
}
return title;
}
}
适配器:
public class AudioListAdapter extends ArrayAdapter<PlaylistItem> {
private Context context;
private int layoutResourceId;
private PlaylistItem aud;
private ArrayList<PlaylistItem> data = null;
public AudioListAdapter(Context context, int layoutResourceId,
ArrayList<PlaylistItem> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = new ViewHolder();
aud = data.get(position);
//it's giving a CORRECT position if I hardcode the index, like data.get(99);
if (row == null) {
LayoutInflater inflater = ((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);
holder.tvArtist.setText(aud.getArtist());
holder.tvTitle.setText(aud.getTitle());
}
return row;
}
static class ViewHolder {
Button play;
ImageView imgSaved;
TextView tvArtist, tvTitle;
}
}