解决方案 »
- webview网页中软键盘回车键“Go”和“Next”布局的设置
- 在BroadcastReceiver中能否监听系统时间?
- 不小心把R.java删除了,怎么样再自动生成出来
- 把Edittext放到WindowManager中,长按Edittext不出现响应的选择框
- 切换acivity时,如何保存各个activity的状态
- android开发地图求助
- 求各路大神指点得不到网上数据,报volley错误的问题
- android开发中的一个……小问题,跪求各位看看是什么情况
- 安卓项目集成MOB的短信验证码,有些号码没有收到验证码。
- 想知道这个通讯录是怎么弄的
- 如何一直显示菜单
- Android 5.0 contacts notification 相关问题
http://download.csdn.net/detail/u012137924/8412899
这个是我前不久写个一个DEMO,关于ListView的,你下载看看,或许有帮助,下班了先,呵呵。
public boolean onItemLongClick(AdapterView<?> parent, View v,
int position, long arg3) {
// TODO Auto-generated method stub
tv = (TextView) v.findViewById(R.id.item_tv);
Toast.makeText(getApplication(), tv.getText().toString(), 1).show();
return true;
}
});
比较笨的方法就这样了。
按照你的描述, 你所说的菜单是指Menu还是指ListView的Item?
是Menu的话,直接用容器吧,ArrayList等都可以;
是Item的话,建议你还是下载我之前写的那个DEMO吧,那里面有详细的注释,那个DEMO是曾经给CSDN上的一个朋友写的关于如何在适配器里面跳转到不同的Activity,因为涉及到Item的子布局控件的点击,所以用OnItemClickListener是解决不了的,呵呵。
用Environment.getExternalStorageDirectory().getpath+"/MUSIC/"这个是获取你手机上的MUSIC目录的路径,哎,算了,明天早上去给你写段代码,你就清楚了。现在不太想动,呵呵。
private static final String TAG = "TestListSongName";
private ListView listViewSongName;
private List<String> list; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_song_name); initView();
} private void initView() {
listViewSongName = (ListView) this.findViewById(R.id.listSongName);
// 获取音乐文件夹路径
String path = Environment.getExternalStorageDirectory().getPath()
+ "/MUSIC/";
Log.e(TAG, "path == " + path); /**
* 新建一个文件夹把歌曲列入,然后For循环一次取出各文件的文件名并添加到list列表中,
* 当然这只是最简单的处理,没有考虑二级和更深级的文件夹.当然如果要做音乐播放器的话
* 这里需要优化,不过,只是获取MUSIC文件夹下的歌曲名的话,用这个足以。呵呵。
*/
// //简写
// File[] songNameFiles = new File(path).listFiles();
File file = new File(path);
File[] songNameFiles = file.listFiles();
list = new ArrayList<String>(); for (int i = 0; i < songNameFiles.length; i++) {
list.add(songNameFiles[i].getName());
} Log.e(TAG, "list {" + list + "}"); listViewSongName.setAdapter(new ListSongNameAdapter(this, list));
} // 一个通常写法的适配器。
public class ListSongNameAdapter extends BaseAdapter {
private Context context;
private List<String> list; public ListSongNameAdapter(Context context, List<String> list) {
this.context = context;
this.list = list;
} @Override
public int getCount() {
return list.size();
} @Override
public Object getItem(int position) {
return list.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewCache viewCache = null; if (convertView == null) {
viewCache = new ViewCache();
convertView = LayoutInflater.from(context).inflate(
R.layout.list_song_name_item, null);
viewCache.textSongName = (TextView) convertView
.findViewById(R.id.songName);
convertView.setTag(viewCache);
} else {
viewCache = (ViewCache) convertView.getTag();
} String songName = list.get(position).toString();
viewCache.textSongName.setText(songName); return convertView;
} class ViewCache {
TextView textSongName;
}
}
}list_song_name.xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".TestListSongName" > <ListView
android:id="@+id/listSongName"
android:layout_width="match_parent"
android:layout_height="match_parent" /></RelativeLayout>list_song_name_item.xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
android:id="@+id/songName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="25sp" /></RelativeLayout>