列表视图中的重复项 - Android

时间:2016-05-29 15:34:29

标签: android listview baseadapter

我在listviews中获取重复值,但我确信在我使用的列表中没有重复项。

以下是代码,

public class MainActivity extends Activity {

    ArrayList<String> listOfSongs = new ArrayList<String>();
    ListView liststructure;
    final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
    String MUSIC_STRING = MediaStore.Audio.Media.IS_MUSIC + "!=0";
    String[] STAR = {"*"};
    Uri uri=null;
    String orderColumns = MediaStore.Audio.AudioColumns.TITLE + " COLLATE LOCALISED ASC";
   private final int REQUEST_GRANTED_BY_USER = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        liststructure = (ListView) findViewById(R.id.listView);
        SongsList list = new SongsList(MainActivity.this, listOfSongs);
        liststructure.setAdapter(list);

        checkPermissions();
        Toast.makeText(getApplicationContext(), "Total number of Items are:" + liststructure.getAdapter().getCount() , Toast.LENGTH_LONG).show();

    }

    private void checkPermissions() {

        if (Build.VERSION.SDK_INT >= 23) {
            int permissionCheck = ContextCompat.checkSelfPermission(MainActivity.this,
                    Manifest.permission.READ_EXTERNAL_STORAGE);
            if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_GRANTED_BY_USER);
            }else{
                getInfoFromDevice();
            }
        }else{
            getInfoFromDevice();
        }
    }
    void getInfoFromDevice() {
        uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Cursor cursor = getContentResolver().query(uri, STAR, MUSIC_STRING, null, null);

        if (cursor != null)

        {
            if (cursor.moveToFirst()) {
                do {
                    String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                    String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                    String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
                    String albumID = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                    listOfSongs.add(name);
                } while (cursor.moveToNext());
            }

        Log.d("COUNT","COUNT is " + listOfSongs.size());
        }
        listOfSongs.size();
    }
}

SongList.java

public class SongsList extends BaseAdapter {

    ArrayList<String> songs;
    Context c;

    SongsList(Context c, ArrayList<String> songs)
    {
        this.c=c;
        this.songs = songs;
    }


    @Override
    public int getCount() {
        return songs.size();
    }

    @Override
    public Object getItem(int i) {
        return songs.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        TextView tv;
        ViewHolder vh;
        LayoutInflater inflater = (LayoutInflater)
                c.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if(view ==null)
        {
           view = inflater.inflate(R.layout.green,viewGroup,false);
           vh  = new ViewHolder(view);
            view.setTag(vh);
            vh.tv.setText(songs.get(i));
        }
        else{
            vh = (ViewHolder) view.getTag();
        }
        return view;
    }
}

我也尝试过以下问题,

Duplicated entries in ListView

结果:我已经实现了这一点。

Android ListView Duplicates entries

结果:这不起作用。

All elements of An ArrayList change when a new one is added?

结果:没有循环和对象创建代码。

ListView Duplicates Android

结果:已经实现了这一点。

有没有人能告诉我为什么列表视图会显示重复项?

2 个答案:

答案 0 :(得分:0)

您需要在视图已存在时设置文本视图内容:

} else { 
    vh = (ViewHolder) view.getTag();
    vh.tv.setText(songs.get(i));
}

答案 1 :(得分:0)

我认为解决方案实际上是您链接的第一个答案。

尝试将代码更改为:

if(view ==null)
    {
       view = inflater.inflate(R.layout.green,viewGroup,false);
        vh = new ViewHolder(view);
        view.setTag(vh);
        vh.tv.setText(songs.get(i));
    }
    else{
        vh = (ViewHolder) view.getTag();
        vh.tv.setText(songs.get(i));
    }

在else语句中,您正在重用已创建的视图,但仍需要更新该视图中的数据。