小编典典

如何将方法设置为数组

java

我正在尝试显示媒体存储的多个列,例如艺术家,专辑,标题等。我能够正确获取arraylist中的一个列,并且可以正常工作,但是当我添加另一个列时,所有列都在同一列中数组列表,所以我创建了一个类,并在该类方法中添加了列。但是我无法在custome适配器中声明它,这是我的代码。

活动主体

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

定制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>

如何将方法设置为数组。如何获取任何示例的方法都将有所帮助


阅读 292

收藏
2020-11-30

共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()会给您一个a
View来填充列表项,一个Cursor已经位于具有该项目数据的记录的位置。这是从光标获取列数据并在列表项视图中设置子视图的简单操作。

2020-11-30