0

I am trying to display multiple column of media store like artist,album,title etc .I am able to get one of the column in arraylist properly and it is working but when I add the other column I am getting all the column in the same array list so I made a class and adding the column in that class method .But i am not able to declare it in the custome adapter here is my code.

Activity main

    private ArrayList < MediaFileInfo > audioList = new ArrayList < > ();

    private void External() {
        try {
            String[] proj = {
                MediaStore.Audio.Media._ID,
                    MediaStore.Audio.Media.TITLE,
                    MediaStore.Audio.Media.ARTIST,
                    MediaStore.Audio.Media.ALBUM
            }; // Can include more data for more details and check it.

            String selection = MediaStore.Audio.Media.DURATION + ">=90000";

            String[] selectionArgs = null;

            String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";

            Cursor audioCursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, selection, selectionArgs, sortOrder);

            if (audioCursor != null) {
                if (audioCursor.moveToFirst()) {
                    do {
                        int audioTitle = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
                        int audioartist = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
                        int audioalbum = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM);
                        MediaFileInfo info = new MediaFileInfo();
                        info.setTitle(audioCursor.getString(audioTitle));
                        info.setAlbum(audioCursor.getString(audioalbum));
                        info.setArtist(audioCursor.getString(audioartist));
                        audioList.add(info);
                    } while (audioCursor.moveToNext());
                }
            }
            audioCursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Method class

 public class MediaFileInfo {
     private String title, artist, album;

     public String getTitle() {
         return title;
     }

     public void setTitle(String title) {
         this.title = title;
     }

     public String getArtist() {
         return artist;
     }

     public void setArtist(String artist) {
         this.artist = artist;
     }

     public String getAlbum() {
         return album;
     }

     public void setAlbum(String album) {
         this.album = album;
     }
 }

CustomeAdapter

public class CustomeAdapter extends ArrayAdapter {
    private ArrayList < MediaFileInfo > audioList = new ArrayList < > ();

    public CustomeAdapter(Context context, int resource) {
        super(context, resource);
    }

    public CustomeAdapter(MainActivity context, ArrayList < MediaFileInfo > audioList) {
        super(context, R.layout.custome_list, audioList);
    }

    @
    Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater imthebest = LayoutInflater.from(getContext());@
        SuppressLint("ViewHolder") View custome = imthebest.inflate(R.layout.custome_list, parent, false);
        MediaFileInfo item = audioList.get(0);
        String item1 = (String) getItem(position);
        TextView text1 = (TextView) custome.findViewById(R.id.textView);
        text1.setText(item1);


        return custome;
    }
}

custome xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Song name"
    android:id="@+id/textView" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Artist"
    android:id="@+id/textView2" />
</LinearLayout>

how to set the methods as an array .how i can get the method any example will be helpful

Neelay Srivastava
  • 1,041
  • 3
  • 15
  • 46

1 Answers1

1

How to get both title and artist into your list:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.custome_list, parent, false);
        }
        MediaFileInfo item = (MediaFileInfo) getItem(position);
        TextView text1 = (TextView) convertView.findViewById(R.id.textView);
        text1.setText(item.getTitle());
        TextView text2 = (TextView) convertView.findViewById(R.id.textView2);
        text2.setText(item.getArtist());


        return convertView;
    }

If you are getting data from the database and have a Cursor then you should just create a CursorAdapter subclass. You don't need to do the work of creating your own list from the data.

All you need to do is override bindView(). bindView() will hand you a View to fill in for the list item and a Cursor that is already positioned at the record that has the data for this item. It's a simple operation to get the column data from the cursor and set the child views in the list item view.

kris larson
  • 30,387
  • 5
  • 62
  • 74
  • I have to use the data in different fragment also – Neelay Srivastava Jul 27 '16 at 18:23
  • I would just re-run the query in the new fragment, but of course you can do it this way. Please post your list item XML because I think you are very close. – kris larson Jul 27 '16 at 18:30
  • see the updated code hope u can help me now .I just want to know how to set the method as an array any example will be helpful – Neelay Srivastava Jul 28 '16 at 08:20
  • kris what is the difference b/w LayoutInflater inflater=LayoutInflater.from(getContext()); View custome=inflater.inflate(R.layout.custome_list,parent,false); – Neelay Srivastava Jul 28 '16 at 13:07
  • and what u have used – Neelay Srivastava Jul 28 '16 at 13:08
  • First, I had some errors in my code I had to fix. I just combined your two statements into one statement, there's no real difference there. However, you need to understand something about the `convertView` parameter. If it is null when passed in, you need to inflate the list item view. If it is *not* null, then it is a recycled view that you can use again, so you don't have to inflate. – kris larson Jul 28 '16 at 13:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118548/discussion-between-neelay-srivastava-and-kris-larson). – Neelay Srivastava Jul 28 '16 at 13:34