• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
灬丿妖妖灬
博客园    首页    新随笔    联系   管理    订阅  订阅

android 列表的编辑

计应111陈佳

创建列表

第一个class

public class FileListViewActivity extends Activity implements OnItemClickListener, OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getObjFromID();
       
        FileListAdapter da=new FileListAdapter(this);
        lv.setAdapter(da);
        lv.setOnItemClickListener(this);
        da.scanFiles("/");
       
        btnUp.setOnClickListener(this);
    }
    ListView lv;
    TextView t1;
    Button btnUp;
   
    void getObjFromID()
    {
        t1=(TextView) findViewById(R.id.t1);
        lv=(ListView) findViewById(R.id.listView1);
     btnUp=(Button) findViewById(R.id.btnUp);
    }
   
 @Override
 public void onItemClick(AdapterView<?> parent, View view,
   int position,long id) {
  FileListAdapter da= (FileListAdapter) lv.getAdapter();
  File f= da.list.get(position);
  if(f.isDirectory())
  {
   t1.setText(f.getPath());
   da.scanFiles(f.getPath());
  }
 }

 @Override
 public void onClick(View v) {
 
  FileListAdapter da= (FileListAdapter) lv.getAdapter();
  if(da.currPath.equals("/")) return;
  File f=new File(da.currPath);
  t1.setText(f.getParent());
  da.scanFiles(f.getParent());
 
 }
}

第二个class

public class FileListAdapter extends BaseAdapter {

 public Activity activity; 

 public List<File> list=new LinkedList<File>();
 public String currPath;
 
 public FileListAdapter(Activity activity) {
  this.activity = activity;
 }

 public void scanFiles(String path)
 {
  list.clear();
  File dir=new File(path);
  File[] subFiles=dir.listFiles();
  if(subFiles!=null)
   for(File f:subFiles)
    list.add(f);
  this.notifyDataSetChanged();
  currPath=path;
 }
 
 @Override
 public int getCount() {
  // TODO Auto-generated method stub
  return list.size();
 }

 @Override
 public Object getItem(int position) {
  // TODO Auto-generated method stub
  return null;
 }

 @Override
 public long getItemId(int position) {
  // TODO Auto-generated method stub
  return position;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
 
  View v=View.inflate(activity, R.layout.item, null);
  TextView txtName= (TextView) v.findViewById(R.id.txtName);
  ImageView imgIcon=(ImageView) v.findViewById(R.id.imgIcon);
  File f=list.get(position);
  txtName.setText(f.getName());
  Bitmap bmp_folder=BitmapFactory.decodeResource(
    activity.getResources(),R.drawable.folder);
  Bitmap bmp_file=BitmapFactory.decodeResource(
    activity.getResources(),R.drawable.file);
  if(f.isDirectory())
   imgIcon.setImageBitmap(bmp_folder);
  else
   imgIcon.setImageBitmap(bmp_file);
  return v;
 }

}

第三个class

public class FileBean implements Comparator<Object>{

 private File file; 
 private Bitmap icon;
 private String name;
 private String path;
 private String size;
 private String type;

 public File getFile() {
  return file;
 }

 public void setFile(File file) {
  this.file = file;
 }

 public Bitmap getIcon() {
  return icon;
 }

 public void setIcon(Bitmap icon) {
  this.icon = icon;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getPath() {
  return path;
 }

 public void setPath(String path) {
  this.path = path;
 }

 public String getFileSize() {
  return size;
 }

 public void setFileSize(String fileSize) {
  this.size = fileSize;
 }

 public String getFileStyle() {
  return type;
 }

 public void setFileStyle(String style) {
  this.type = style;
 }

 @Override
 public int compare(Object object1, Object object2) {
  FileBean file1 = (FileBean)object1;
  FileBean file2 = (FileBean)object2;
  return file1.getName().compareToIgnoreCase(file2.getName());
 }
}

运行结果:

总结:列表显示总的来说就是几个不同的类,通过相继承的关系来实现目录之间的转换。

posted @ 2013-06-19 09:54  灬丿妖妖灬  阅读(346)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3