我想从移动SD卡的文件夹中显示一些图像。我写了一个适配器来显示图像和文件名。
很遗憾,该应用未显示列表视图。代码有什么问题,请帮帮我。
这是列表视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
这是我要显示在列表视图中的行
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/textView"
android:layout_width="307dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="73dp"
android:layout_marginTop="25dp"
android:text="TextView" />
</RelativeLayout>
我的MainActivity代码
public class MainActivity extends AppCompatActivity{
final String str = Environment.getExternalStorageDirectory().getAbsolutePath()+"/WhatsApp/Media/.Statuses";
File f = new File(str);
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File[] files = listFiles();
String[] fileNames = new String[files.length];
String[] filePaths = new String[files.length];
for(int i=0;i<files.length;i++){
fileNames[i]=files[i].getName();
filePaths[i]=files[i].getAbsolutePath();
}
System.out.println("files::: " + files.length);
System.out.println("fileNamesfileNames::: " + fileNames.length);
System.out.println("filePathsfilePaths::: " + filePaths.length);
listView = (ListView) findViewById(R.id.listview);
MyAdapter adapter = new MyAdapter(this,fileNames,filePaths);
listView.setAdapter(adapter);
}
public File[] listFiles() {
try {
return f.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".jpg");
}
});
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
class MyAdapter extends BaseAdapter{
Context context;
String[] rFiles;
String[] rPaths;
private static LayoutInflater inflater = null;
MyAdapter(Context ctx,String[] fileNames,String[] filePaths){
context = ctx;
rFiles = fileNames;
rPaths = filePaths;
inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
View vi = view;
if(view==null){
vi = inflater.inflate(R.layout.row,null);
}
ImageView images = vi.findViewById(R.id.imageView);
TextView textView = vi.findViewById(R.id.textView);
Bitmap bmp = BitmapFactory.decodeFile(rPaths[position]);
images.setImageBitmap(bmp);
textView.setText(rFiles[position]);
System.out.println(vi);
return vi;
}
}
文件正在读取,但是..它不显示列表...请帮我解决这个问题。
这里有错误-
@Override
public int getCount() {
return 0;
}
您应该像这样返回列表的大小-
@Override
public int getCount() {
return rFiles.size();
}
还有-
@Override
public Object getItem(int position) {
return rFiles[position];
}
检查是否添加用户权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
也请按Log.e("files",files.length);
检查打印,如果您的文件数组为null。