【问题标题】:Android: Populate listview with audio files and open new activityAndroid:使用音频文件填充列表视图并打开新活动
【发布时间】:2015-06-09 17:30:36
【问题描述】:

我正在创建一个录音应用程序,我已经设法用我选择的目录中的音频文件填充一个列表视图,但我还想获取它们的长度、类型等。 同样在单击它们时,我希望它打开一个新活动,其中包含有关该音频文件的信息以及播放它的能力。 附上截图显示我在说什么。

这是我用来填充列表视图的代码:

ListView listView = (ListView) findViewById(R.id.song_list);    
myList = new ArrayList<String>();

    File directory = Environment.getExternalStorageDirectory();
    file = new File( directory + "/Audio" );
    File list[] = file.listFiles();

    for( int i=0; i< list.length; i++)
    {
            myList.add( list[i].getName() );
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, myList);
    listView.setAdapter(adapter); //Set all the file in the list.

任何帮助将不胜感激,谢谢! http://i62.tinypic.com/rb9zja.png

【问题讨论】:

  • 要在单个列表项中填充多个属性,您必须使用自定义列表视图。 here
  • 谢谢,但我仍然不知道如何从文件中收集时间、日期和大小,因为它只是按名称列出文件。

标签: android listview audio android-activity populate


【解决方案1】:

必须有一个自定义适配器。

如有任何错误,请提前致歉;我直接在答案中输入了这个,并没有先用 IDE 来证明它。

ListView listView = (ListView) findViewById(R.id.song_list);    
myList = new ArrayList<File>();    // note, list of Files, not Strings

File directory = Environment.getExternalStorageDirectory();
file = new File( directory + "/Audio" );
File list[] = file.listFiles();

// you could also use Arrays.asList() instead of a loop
for( int i=0; i< list.length; i++)
{
    myList.add( list[i] );   // add the whole File instance to the list
}
MyFileArrayAdapter adapter = new MyFileArrayAdapter(this,
        R.layout.my_file_List_item, myList);
listView.setAdapter(adapter); //Set all the file in the list.

listView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent intent = new Intent();
        intent.putExtra("file", (Serializable) parent.getAdapter().getItem(position));
        // in your target activity, get the file like this:
        // File file = (File) getIntent().getSerializableExtra("file");
        startActivity(intent, FileDetailActivity.class);
    }
});

public static class MyFileArrayAdapter extends ArrayAdapter<File> {

    private int mLayoutId;

    public MyFileArrayAdapter(Context context, int layoutId, List<File> files) {
        super(context, layoutId, files);
        mLayoutId = layoutId;
    }

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

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) getContext()
                    .getSystemService(LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(mLayoutId, parent, false);
        }

        File file = (File) getItem(position);
        int fileSize = file.length();
        Date fileDateTime = new Date(file.lastModified());

        // set up your list item views here and fill in the size and date/time
    }
}

【讨论】:

  • 我觉得我的大脑昨天被炸了,花了一天时间基本了解它,现在我已经开始工作了,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多