gpt4 book ai didi

android - 如何在 Android 的 ListView 中显示音频文件?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:06:31 27 4
gpt4 key购买 nike

我在 SD card 上的特定目录中有一些音频文件.我需要在 Android 的 ListView 中显示他们的名字,如果我想播放或删除那个特定的文件,我可以通过那个 ListView 来实现吗?我该怎么做?

public class MyPerformanceArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;

public MyPerformanceArrayAdapter(Context context, String[] values) {
super(context, R.layout.main, values);
this.context = context;
this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.main, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(values[position]);
// Change the icon for Windows and iPhone
String s = values[position];
if (s.startsWith("Windows7") || s.startsWith("iPhone")
|| s.startsWith("Solaris")) {
//imageView.setImageResource(R.drawable.no);
} else {
//imageView.setImageResource(R.drawable.ok);
}
return rowView;
}
}

public class SimpleListActivity extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
MyPerformanceArrayAdapter adapter = new MyPerformanceArrayAdapter(this, values);
setListAdapter(adapter);
}
}

这段代码是一个简单的 ListView 。我需要将它们的名称从特定目录获取到适配器类。我该怎么做?

如果我对该特定文件进行了任何更改,这些更改应该反射(reflect)在 ListView 中。

最佳答案

public class ReadAllFilesFromPathActivity extends Activity {
/** Called when the activity is first created. */
private List<String> myList;
File file;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView) findViewById(R.id.mylist);
myList = new ArrayList<String>();

File directory = Environment.getExternalStorageDirectory();
file = new File( directory + "/Test" );
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.
}
}

关于android - 如何在 Android 的 ListView 中显示音频文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9782168/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com