我正在撰写Android
文件选择器程序,在ListView
中添加详细信息时,流程无法正常运行。在文件Uploadlist.java
中,我将目录路径发送到另一个扩展filedetails
的类ArrayAdapter
。但是代码流没有进入方法getView()
。有些人可以说出原因,并说明为什么代码没有进入getView()
。我使用了以下代码。
UploadList.Java
public class UploadList extends ListActivity {
private List<String> item=null;
private List<String> path=null;
private String root;
private TextView mypath;
String pathname,filename;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mypath=(TextView)findViewById(R.id.path);
image=(ImageView)findViewById(R.id.imageView1);
root=Environment.getExternalStorageDirectory().getPath();
filedetails details=new filedetails(this, root);
setListAdapter(details);
}
/**
* @param dirPath
*/
public void getDir(String dirPath){
mypath.setText("Location is:"+dirPath);
pathname=dirPath;
filedetails details=new filedetails(this, dirPath);
setListAdapter(details);
}
protected void onListItemClick(ListView l,View v,int position,long id){
File file=new File(path.get(position));
if(file.isDirectory()){
if(file.canRead())
getDir(path.get(position));
}
else
{
filename=file.getName();
pathname=pathname+"/"+filename;
Intent back=new Intent(this,Upload.class);
Bundle filen=new Bundle();
filen.putString("filepath",pathname);
back.putExtras(filen);
startActivity(back);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_upload_list, menu);
return true;
}
}
filedetails.java
public class filedetails extends ArrayAdapter<String> {
Context context;
File[]values;
String dirPath;
String root=Environment.getExternalStorageDirectory().getPath();
String filename;
private List<String> item;
private List<String> path;
public filedetails(Context context,String dirPath)
{
super(context,R.layout.row1);
this.context=context;
this.dirPath=dirPath;
}
static class ViewHolder {
public TextView text;
public ImageView image;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.row1, parent, false);
ViewHolder viewholder=new ViewHolder();
viewholder.text=(TextView)rowView.findViewById(R.id.textView1);
viewholder.image=(ImageView)rowView.findViewById(R.id.imageView1);
rowView.setTag(viewholder);
item=new ArrayList<String>();
path=new ArrayList<String>();
File file=new File(dirPath);
File[]filelist=file.listFiles();
ViewHolder holder=(ViewHolder)rowView.getTag();
if(!dirPath.equals(root)){
item.add(root);
path.add(root);
item.add("../");
path.add(file.getParent());
}
for(int i=0;i<filelist.length;i++){
File file1=values[i];
if(file1.canRead())
{
if(file1.isDirectory()){
holder.image.setImageResource(R.drawable.folder);
holder.text.setText(file1.getName());
}
}
else{
holder.image.setImageResource(R.drawable.file);
holder.text.setText(file1.getName());
}
}
return rowView;
}
}
答案 0 :(得分:1)
更改
File[]values;
到
//File[]values;
然后改变
File file1=values[i];
到
File file1=filelist[i];