计应113李明亮
设计一个简单的文件管理器
首先界面的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/t1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/btnUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="向上" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
界面item.xml代码
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/folder" />
<TextView
android:id="@+id/txtname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="28dp"
android:text="TextView" />
<TextView
android:id="@+id/txtsize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_alignLeft="@+id/txtname"
android:text="TextView" />
新建class fileadapter
public class FileAdapter extends BaseAdapter {
public Activity activity;
public List<File> list=new LinkedList<File>();
public String currPath;
public FileAdapter(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 arg0) {
// 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.imageView1);
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;
}
}
在主方法中写代码
ublic class FilelistTestActivity extends Activity implements OnClickListener, OnItemClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getObjFromID();
FileAdapter da =new FileAdapter(this);
lv.setAdapter(da);
lv.setOnItemClickListener(this);
da.scanFiles("/");
btup.setOnClickListener(this);
}
ListView lv;
TextView t1;
Button btup;
void getObjFromID()
{
t1=(TextView) findViewById(R.id.t1);
lv=(ListView) findViewById(R.id.listView);
btup=(Button) findViewById(R.id.btnUp);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
FileAdapter da=(FileAdapter) lv.getAdapter();
File f=da.list.get(position);
if(f.isDirectory())
{
t1.setText(f.getPath());
da.scanFiles(f.getPath());
}
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
FileAdapter da=(FileAdapter) lv.getAdapter();
if(da.currPath.equals("/")) return;
File f=new File(da.currPath);
t1.setText(f.getPath());
da.scanFiles(f.getParent());
}
本文章还在维护修改中。