0

I am getting duplicate values in listviews but I am sure that there are no duplicates in the list that I am using.

Here is the below code,

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;
    }
}

I also tried the below questions,

Duplicated entries in ListView

Result: I have already implemented this.

Android ListView Duplicates entries

Result: This did not work.

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

Result: No looping and object creation code.

ListView Duplicates Android

Result: Already implemented this.

Could any one tell me why is the listview displaying the duplicates ?

Community
  • 1
  • 1
oldcode
  • 1,669
  • 3
  • 22
  • 41

2 Answers2

0

You need to set the text view content when the view already exists:

} else { 
    vh = (ViewHolder) view.getTag();
    vh.tv.setText(songs.get(i));
}
Joni
  • 108,737
  • 14
  • 143
  • 193
0

I think the solution is actually in the first answer you linked to.

Try changing your code to something like:

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));
    }

In the else statement, you're reusing a view that's already been created, however you still need to update the data in that view.

Daniel Scott
  • 1,867
  • 3
  • 13
  • 18