ListView上的Item长按弹出菜单或者对话框是常见的功能之一:
其实现如下:
extends ListActivity{
在方法:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.localfilebrowser); registerForContextMenu(this.getListView());增加方法:
[java]
- @Override
- public void onCreateContextMenu(ContextMenu menu, View view,
- ContextMenuInfo menuInfo) {
- AdapterView.AdapterContextMenuInfo info;
- try {
- info = (AdapterView.AdapterContextMenuInfo) menuInfo;
- } catch (ClassCastException e) {
- Log.e("test", "bad menuInfo", e);
- return;
- }
- currentPath = new File(paths.get(info.position));
- showDialog(UPDATE_FILEPATH);
- }
本文使用了弹出式窗口,如果是菜单的话,需要增加如下代码
[java]
- @Override
- public boolean onContextItemSelected(MenuItem item) {
- super.onContextItemSelected(item);
- AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item
- .getMenuInfo();
- // Remember current selection
- IconifiedTextListAdapter adapter = (IconifiedTextListAdapter) getListAdapter();
- if (adapter == null) {
- return false;
- }
- return true;
- }
showDialog(UPDATE_FILEPATH);对应于如下代码
[java]
- @Override
- protected Dialog onCreateDialog(int id) {
- switch (id) {
- case UPDATE_FILEPATH:
- LayoutInflater inflater = LayoutInflater.from(this);
- View view = inflater.inflate(R.layout.dialog_change_filepath, null);
- final TextView oldet = (TextView) view.findViewById(R.id.oldfilepath);
- final TextView newet = (TextView) view.findViewById(R.id.newfilepath);
- prefs = PreferenceManager.getDefaultSharedPreferences(this);
- oldet.setText(oldet.getText()+prefs.getString("CLB_DOWNLOAD_PATH", ""));
- if(currentPath!=null)
- newet.setText(newet.getText()+currentPath.getAbsolutePath());
- return new AlertDialog.Builder(this).setIcon(
- android.R.drawable.ic_dialog_alert).setTitle(
- R.string.dialog_filepath).setView(view)
- .setPositiveButton(android.R.string.ok,
- new OnClickListener() {
- public void onClick(DialogInterface dialog,
- int which) {
- Editor editor = prefs.edit();
- editor.putString("CLB_DOWNLOAD_PATH", currentPath.getAbsolutePath());
- editor.commit();
- ListOSDir.this.finish();
- }
- }).setNegativeButton(android.R.string.cancel,
- new OnClickListener() {
- public void onClick(DialogInterface dialog,
- int which) {
- // Cancel should not do anything.
- removeDialog(UPDATE_FILEPATH);
- }
- }).create();
- }
- return null;
- }
其中:removeDialog(UPDATE_FILEPATH); 是解决
因为create 一个dialog的开销很大,所以android会先查找有没有dialog实例存在,如果有,不create,prepared之后就show,弹出窗口一直都相同的问题
整个程序是选择一个目录来设置下载需要存储的路径的,代码如下:
[java]
- package cn.cnic.cerc.moblie.clbclient.filemanager;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.app.ListActivity;
- import android.content.DialogInterface;
- import android.content.SharedPreferences;
- import android.content.DialogInterface.OnClickListener;
- import android.content.SharedPreferences.Editor;
- import android.os.Bundle;
- import android.os.Environment;
- import android.preference.PreferenceManager;
- import android.util.Log;
- import android.view.ContextMenu;
- import android.view.LayoutInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.Window;
- import android.view.ContextMenu.ContextMenuInfo;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.AdapterView.AdapterContextMenuInfo;
- import cn.cnic.cerc.moblie.clbclient.R;
- public class ListOSDir extends ListActivity {
- private static final int UPDATE_FILEPATH = 0;
- /** Called when the activity is first created. */
- private List<String> items = null;// 存放名称
- private List<String> paths = null;// 存放路径
- private String rootPath = "/";
- private TextView tv;
- private SharedPreferences prefs;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
- setContentView(R.layout.localfilebrowser);
- registerForContextMenu(this.getListView());
- tv = (TextView) this.findViewById(R.id.TextView);
- this.getFileDir(rootPath);// 获取rootPath目录下的文件.
- prefs = PreferenceManager.getDefaultSharedPreferences(this);
- if(prefs.getString("CLB_DOWNLOAD_PATH", "").equals(""))
- {
- File sdcardDir = Environment.getExternalStorageDirectory();
- String path = sdcardDir.getParent() + sdcardDir.getName();
- String fileDirectory = path + java.io.File.separator + "clb";
- Editor editor = prefs.edit();
- editor.putString("CLB_DOWNLOAD_PATH", fileDirectory);
- editor.commit();
- }
- }
- public void getFileDir(String filePath) {
- try {
- this.tv.setText("当前路径:" + filePath);// 设置当前所在路径
- items = new ArrayList<String>();
- paths = new ArrayList<String>();
- File f = new File(filePath);
- File[] files = f.listFiles();// 列出所有文件
- // 如果不是根目录,则列出返回根目录和上一目录选项
- if (!filePath.equals(rootPath)) {
- items.add("返回根目录");
- paths.add(rootPath);
- items.add("返回上一层目录");
- paths.add(f.getParent());
- }
- // 将所有文件存入list中
- if (files != null) {
- int count = files.length;// 文件个数
- for (int i = 0; i < count; i++) {
- File file = files[i];
- if (file.isDirectory()) {
- items.add(file.getName());
- paths.add(file.getPath());
- }
- }
- }
- ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, items);
- this.setListAdapter(adapter);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- super.onListItemClick(l, v, position, id);
- String path = paths.get(position);
- File file = new File(path);
- // 如果是文件夹就继续分解
- if (file.isDirectory()) {
- this.getFileDir(path);
- } else {
- new AlertDialog.Builder(this).setTitle("提示").setMessage(
- file.getName() + " 是一个文件!").setPositiveButton("OK",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- }
- }).show();
- }
- }
- private File currentPath;
- @Override
- public void onCreateContextMenu(ContextMenu menu, View view,
- ContextMenuInfo menuInfo) {
- AdapterView.AdapterContextMenuInfo info;
- try {
- info = (AdapterView.AdapterContextMenuInfo) menuInfo;
- } catch (ClassCastException e) {
- Log.e("test", "bad menuInfo", e);
- return;
- }
- currentPath = new File(paths.get(info.position));
- showDialog(UPDATE_FILEPATH);
- }
- @Override
- public boolean onContextItemSelected(MenuItem item) {
- super.onContextItemSelected(item);
- AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item
- .getMenuInfo();
- // Remember current selection
- IconifiedTextListAdapter adapter = (IconifiedTextListAdapter) getListAdapter();
- if (adapter == null) {
- return false;
- }
- return true;
- }
- @Override
- protected Dialog onCreateDialog(int id) {
- switch (id) {
- case UPDATE_FILEPATH:
- LayoutInflater inflater = LayoutInflater.from(this);
- View view = inflater.inflate(R.layout.dialog_change_filepath, null);
- final TextView oldet = (TextView) view.findViewById(R.id.oldfilepath);
- final TextView newet = (TextView) view.findViewById(R.id.newfilepath);
- prefs = PreferenceManager.getDefaultSharedPreferences(this);
- oldet.setText(oldet.getText()+prefs.getString("CLB_DOWNLOAD_PATH", ""));
- if(currentPath!=null)
- newet.setText(newet.getText()+currentPath.getAbsolutePath());
- return new AlertDialog.Builder(this).setIcon(
- android.R.drawable.ic_dialog_alert).setTitle(
- R.string.dialog_filepath).setView(view)
- .setPositiveButton(android.R.string.ok,
- new OnClickListener() {
- public void onClick(DialogInterface dialog,
- int which) {
- Editor editor = prefs.edit();
- editor.putString("CLB_DOWNLOAD_PATH", currentPath.getAbsolutePath());
- editor.commit();
- ListOSDir.this.finish();
- }
- }).setNegativeButton(android.R.string.cancel,
- new OnClickListener() {
- public void onClick(DialogInterface dialog,
- int which) {
- // Cancel should not do anything.
- removeDialog(UPDATE_FILEPATH);
- }
- }).create();
- }
- return null;
- }
- }
更多