如何将方法设置为数组

时间:2016-07-27 13:19:31

标签: java android arraylist methods android-mediaplayer

我正在尝试显示多个媒体商店列,如艺术家,专辑,标题等。我能够正确地获得arraylist中的一个列,但它正在工作但是当我添加其他列时,我得到了所有列在同一个数组列表中,所以我创建了一个类并在该类方法中添加了列。但我无法在客户端适配器中声明它 这是我的代码。

活动主要

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

方法类

 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>

如何将方法设置为数组。我可以获得任何示例的方法将有用

1 个答案:

答案 0 :(得分:1)

如何将标题和艺术家都列入您的列表:

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

如果要从数据库获取数据并且拥有Cursor,那么您应该创建一个CursorAdapter子类。您无需从数据中创建自己的列表。

您需要做的就是覆盖bindView()bindView()会向您提供View以填写列表项以及已定位在包含此项目数据的记录中的Cursor。从光标获取列数据并在列表项视图中设置子视图是一个简单的操作。