列表的设计
计应111 马兵
下面是一个设计列表的程序,将Layout转换成View,通过LayoutInflater完成
使用Layout的原因:Layout便于在可视化界面中设计。
其中代码如下:
1.
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());
}
}
2.
public class FileListAdapter extends BaseAdapter {
public Activity activity; //创建View时必须要提供Context
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;
}
}
3.
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());
}
}
运行效果如下:
图1
这里使用的代码较多,这里使用了三个不同的类,他们之间是继承的关系;当用户点击进入一个文件夹后,再点击向上按钮,程序会自动进入上一级界面。
心得体会:从学习这门课程以来,在这途中遇到了许多许多的问题,大的,小的.....有时候几乎崩溃了,因为找到错误但是修改了很多次,还是不能运行,所以要学习这门课程我们还需要有耐心有毅力,勤奋。遇到不会的时候去请教别人,自己也多动动手,不会还可以去百度,贴吧 博客中查找...总之就是不要偷懒。
虽然我大多数都是直接在博客中粘贴的代码进行修改,但是我也从中学会了许多知识,也是有收获的,但是还是从其中学到了很多。而且还锻炼了自己的动手能力。虽然写的这些都是基础但是不要小看这些基础哦...0.0
浙公网安备 33010602011771号