如何将方法设置为数组 [英] How to set method as array

查看:80
本文介绍了如何将方法设置为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

活动主体

    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

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>

如何将方法设置为数组.如何获取任何示例的方法将很有帮助

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

推荐答案

如何将标题和艺术家都添加到您的列表中:

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


如果要从数据库中获取数据并使用Cursor,则应只创建一个CursorAdapter子类.您无需执行根据数据创建自己的列表的工作.


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.

您需要做的就是覆盖bindView(). bindView()将为您提供View来填充列表项,而Cursor则已经放置在具有该项目数据的记录处.这是从光标获取列数据并在列表项视图中设置子视图的简单操作.

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.

这篇关于如何将方法设置为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆