我在SD card的特定目录中有一些音频文件。我需要在Android的列表视图中显示他们的名字,如果我想播放或删除该特定文件,我可以通过列表视图执行此操作吗?我该怎么做?
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);
}
}
此代码是一个简单的列表视图。我需要将他们的名字从特定目录提取到适配器类。我该怎么办?
如果我对该特定文件进行任何更改,这些更改应反映在列表视图中。
答案 0 :(得分:4)
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.
}
}
答案 1 :(得分:2)
你可以检查使用下面的代码在listView中显示ll音频文件,然后点击你播放的任何一个..
public class AudioListActivity extends Activity {
ListView musiclist;
Cursor musiccursor;
int music_column_index;
int count;
MediaPlayer mMediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.audiolist_activity);
init_phone_music_grid();
}
private void init_phone_music_grid() {
System.gc();
String[] proj = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,MediaStore.Video.Media.SIZE };
musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,proj, null, null, null);
count = musiccursor.getCount();
musiclist = (ListView) findViewById(R.id.PhoneMusicList);
musiclist.setAdapter(new MusicAdapter(getApplicationContext()));
musiclist.setOnItemClickListener(musicgridlistener);
mMediaPlayer = new MediaPlayer();
}
private OnItemClickListener musicgridlistener = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,long id) {
System.gc();
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musiccursor.moveToPosition(position);
String filename = musiccursor.getString(music_column_index);
try {
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
}
mMediaPlayer.setDataSource(filename);
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (Exception e) {
}
}
};
public class MusicAdapter extends BaseAdapter {
private Context mContext;
public MusicAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
TextView tv = new TextView(mContext.getApplicationContext());
String id = null;
if (convertView == null) {
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
musiccursor.moveToPosition(position);
id = musiccursor.getString(music_column_index);
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
musiccursor.moveToPosition(position);
id += " Size(KB):" + musiccursor.getString(music_column_index);
tv.setText(id);
} else
tv = (TextView) convertView;
return tv;
}
}
}
答案 2 :(得分:1)
在您的文件所在的目录中使用java.io.File
对象:
File dir = new File( "path to dir");
String[] fileNames = dir.list();
如果您只想要其中一些,您甚至可以使用过滤器:
String[] fileNames = dir.list( new FilenNameFilter() {
@Override
public void accept(File dir, String name) {
return name.startWith( "Foo");
} //met
});
答案 3 :(得分:1)
我假设你可以正确地在列表中显示这些文件。
接下来,创建菜单,显示打开和删除:
public final static int MENU_OPEN = 0;
public final static int MENU_DELETE = 1;
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.add(0, MENU_OPEN, 0, "Open");
menu.add(0, MENU_DELETE, 1, "Delete");
}
然后,听一下onContextItemSelected进行菜单按:
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
String filename = <Your File List>.get(info.position);
switch(item.getItemId()) {
case MENU_OPEN:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(filename);
intent.setDataAndType(Uri.fromFile(file), "<Your MIME Type>");
startActivity(intent);
break;
case MENU_DELETE:
File file = new File(filename);
file.delete();
break;
}
}
最后,要实时监控目录,您应该实现FileObserver。
http://developer.android.com/reference/android/os/FileObserver.html