如何在android中从存储创建自定义音频列表视图?

os8fio9y  于 2021-06-27  发布在  Java
关注(0)|答案(2)|浏览(297)

我会创建一个自定义的音频列表视图来显示存储中的所有音频文件。

public class AudioToSendActivity extends AppCompatActivity {

    private ArrayList<String> arrayList;
    private ListView listView;
    private ArrayAdapter<String> adapter;
    private static final int PERMISSION_REQUEST = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audio_send);
        listView = findViewById(R.id.list_view_audio);

        if(ContextCompat.checkSelfPermission(AudioToSendActivity.this,
                Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            if(ActivityCompat.shouldShowRequestPermissionRationale(AudioToSendActivity.this,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {
                ActivityCompat.requestPermissions(AudioToSendActivity.this,
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST);
            }
            else {
                ActivityCompat.requestPermissions(AudioToSendActivity.this,
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST);
            }
        }
        else {
            upload();
        }

    }

    private void upload() {

        listView = findViewById(R.id.list_view_audio);
        arrayList = new ArrayList<>();
        getAudio();
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // open music player to play desired song
            }
        });

    }

    public void getAudio() {
        ContentResolver contentResolver = getContentResolver();
        Uri audioUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
        Cursor audioCursor = contentResolver.query(audioUri, null, null, null, null);

        if(audioCursor != null && audioCursor.moveToFirst()) {
            int audioTitle = audioCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
            int audioDate = audioCursor.getColumnIndex(MediaStore.Audio.Media.DATE_ADDED);

            do {
                String currentTitle = audioCursor.getString(audioTitle);
                String currentDate = audioCursor.getString(audioDate);

                if(currentDate != null) {
                    currentDate = formatter.format(audioDate);
                }
                arrayList.add(currentTitle + "\n" + currentDate);
            }
            while (audioCursor.moveToNext());
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch(requestCode) {
            case PERMISSION_REQUEST: {
                if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    if(ContextCompat.checkSelfPermission(AudioToSendActivity.this,
                            Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                        Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();

                        upload();
                    }
                }
                else {
                    Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
                    finish();
                }
                return;
            }
        }
    }
}

这是我的单个\u列表\u item.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/list_image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/list_play_btn" />

    <TextView
        android:id="@+id/list_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:fontFamily="@font/open_sans_semibold"
        android:text="Filename"
        android:textColor="@color/colorDarkBlue"
        android:textSize="16sp"
        app:layout_constraintStart_toEndOf="@+id/list_image_view"
        app:layout_constraintTop_toTopOf="@+id/list_image_view" />

    <TextView
        android:id="@+id/list_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:fontFamily="@font/open_sans"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="@+id/list_image_view"
        app:layout_constraintStart_toEndOf="@+id/list_image_view" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是我的音频类,但我不知道如何自定义listview。我已经为这个类创建了布局(activity\u audio\u to\u send.xml)和单行布局(single\u list\u item.xml)。如何使用单行布局?有人能帮我吗?提前感谢大家!

acruukt9

acruukt91#

在你的活动中

private void upload() {
        listView = findViewById(R.id.list_view_audio);
        arrayList = new ArrayList<>();
        getAudio();
        adapter = new AudioAdapter<String>(this, arrayList);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // open music player to play desired song
            }
        });
    }

音频适配器.java

public class AudioAdapter extends ArrayAdapter<String> {

    private Context mContext;
    private List<String> audioList;

    public AudioAdapter(@NonNull Context context, ArrayList<String> list) {
        super(context, 0 , list);
        mContext = context;
        audioList= list;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View listItem = convertView;
        if (listItem == null)
            listItem = LayoutInflater.from(mContext).inflate(R.layout.single_list_item, parent, false);

        String audio = audioList.get(position);
        String[] items = audio.split("\n");
        String title = "", date = "";
        if (items.length > 1) {
           title = items[0];
        }

        if (items.length > 2) {
           date = items[1];
        }

        TextView titleView = (TextView) listItem.findViewById(R.id.list_title);
        titleView.setText(title);

        TextView dateView = (TextView) listItem.findViewById(R.id.list_date);
        dateView.setText(date);

        return listItem;
    }
}
rqcrx0a6

rqcrx0a62#

创建 CustomAdapter 扩展 ArrayAdapter 并尝试在构造函数中不传递任何布局引用,即调用 super(context,0,datalist) 现在覆盖 getView 方法将所需视图充气, R.layout.single_list_item ,并绑定数据
查看本教程以获取示例

相关问题