gpt4 book ai didi

java - android文件选择器不工作

转载 作者:行者123 更新时间:2023-11-30 03:53:43 28 4
gpt4 key购买 nike

我正在编写一个 Android 文件选择器程序,当在 ListView 中添加详细信息时,流程无法完美运行。在文件 Uploadlist.java 中,我将目录路径发送到另一个扩展 ArrayAdapter 的类 filedetails。但是代码流没有进入方法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;

}
}

最佳答案

改变

File[]values;  

//File[]values;

然后改变

File file1=values[i];

File file1=filelist[i];

关于java - android文件选择器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13679040/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com