0

情况:我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;

}

    }
4

4 回答 4

2

在 getView 方法中,如果 View 是回收的,则不会再次填充 View。因此,一旦您滚动到屏幕外,它将开始显示旧的。您也没有设置/检索 ViewHolder。请尝试以下实现(未经测试)。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
   View row = convertView;
   ViewHolder holder = null;
   aud = data.get(position);
        //it's giving a CORRECT position if I hardcode the index, like data.get(99);
   if (row == null) {
       holder = new ViewHolder();
       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);          
       row.setTag(holder);
} else {
    holder = (ViewHolder)row.getTag();
}
holder.tvArtist.setText(aud.getArtist());
holder.tvTitle.setText(aud.getTitle());

return row;
}
于 2013-02-28T18:11:58.480 回答
1

如果要删除此行:

 View row = convertView;

并且每次都膨胀视图,那么这个问题就会消失。如此处所述,我也遇到过几次这种影响:

从屏幕上消失后,如何在 Listview 行中保存更改后的 ImageView 的状态?

实际上我不知道为什么会发生这种情况,因为建议使用 convertView。

更新:

试试这个:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
ViewHolder holder = new ViewHolder();
LayoutInflater inflater = (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;

}

于 2013-02-28T17:59:52.197 回答
1

我对您的 getView 方法进行了一些修改。替换它并检查它。

  @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = 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 = new ViewHolder();
                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);
                row.setTag(holder);
        }
    else
    {
                    holder = (ViewHolder ) row.getTag();

    }
                holder.tvArtist.setText(aud.getArtist());
                holder.tvTitle.setText(aud.getTitle());



            return row;
        }
于 2013-02-28T18:08:37.363 回答
1
      if (convertView == null)// if null

       {
        convertView = mInflater.inflate(R.layout.list, null);//inflate view
       } else 
       {
        holder = (ViewHolder) convertView.getTag();// set tag to holder.
       }

做出这些改变。

于 2013-02-28T18:14:18.603 回答