Android Listview行重复项

时间:2013-12-17 15:23:33

标签: java android android-listview android-adapter

我有一个列表,其中我从共享首选项获得的特定数字(位置)应显示imageview(表示当前正在播放的歌曲)。但我得到了这个位置,但该项目也显示在其他行上。滚动列表时会出现问题。

*当我退出另一项活动时,这种情况正在发生,而在我的简历中我发现了这一点:

@Override
protected void onResume() {
    super.onResume();
    if(AlbumDetails.mediaPlayer!=null)
    adapter = new PlaylistAdapter(this, songs);
    list.setAdapter(adapter);
}

这是我的代码:

public class PlaylistAdapter extends BaseAdapter {

    private Activity activity;
    private static LayoutInflater inflater = null;
    private ArrayList<Songs> data;
    private DatabaseHelper db;
    private SharedPreferences prefs;
    int playpos;

    public PlaylistAdapter(Activity a, ArrayList<Songs> songs) {
        activity = a;
        data = songs;
        db = new DatabaseHelper(a);
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        prefs = activity.getSharedPreferences("com.darkovski.quran",
                Context.MODE_PRIVATE);
        playpos = prefs.getInt("posPlaying", -1);
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView,
            final ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.song_item, parent, false);

        ImageView download = (ImageView) vi
                .findViewById(R.id.playlist_item_download);
        db.openDB();
        if (db.isDownloaded(data.get(position).getNumber(), data.get(position)
                .getRecitorName(), data.get(position).getRecitorID()))
            download.setImageResource(R.drawable.download_yes);
        else {
            download.setImageResource(R.drawable.download_no);
            download.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    new DownloadFileFromURL(activity, data.get(position)
                            .getRecitorName(), data.get(position).getTitle(),
                            data.get(position).getLink(), data.get(position)
                                    .getNumber(), data.get(position)
                                    .getRecitorID()).execute();
                    if (!db.isDBOpen())
                        db.openDB();
                    db.addDownloaded(data.get(position).getNumber(),
                            data.get(position).getLink(), 0, data.get(position)
                                    .getRecitorID(), "", data.get(position)
                                    .getTitle());

                    Toast.makeText(activity,
                            "Downloading " + data.get(position).getTitle(),
                            Toast.LENGTH_SHORT).show();

                }
            });
        }
        db.closeDB();

        TextView number = (TextView) vi.findViewById(R.id.playlist_item_num);
        TextView title = (TextView) vi.findViewById(R.id.playlist_item_reciter);
        title.setText(data.get(position).getTitle());
        number.setText((position + 1) + "");
        ImageView eq = (ImageView) vi.findViewById(R.id.playlist_item_equlizer);
        //this is where i show the item
        if (playpos == position) {
            eq.setVisibility(View.VISIBLE);
        }

        return vi;
    }
}

这就是它的样子:

enter image description here

2 个答案:

答案 0 :(得分:7)

因为这句话

if (playpos == position) {
        eq.setVisibility(View.VISIBLE);
    }

listview会回收视图,因此如果您注意到第一行而最后一行显示相同的内容。那是因为第一行被回收到最后一行

你可以通过在if上添加一个else来解决这个问题,看起来像是

if (playpos == position) {
        eq.setVisibility(View.VISIBLE);
}else{
    eq.setVisibility(View.GONE);
}

答案 1 :(得分:0)

如果您在Adapter类中扩展BaseAdapter,那么此解决方案将起作用。 https://stackoverflow.com/a/28791031/4531507