如何为列表视图中的每一行设置一个图标(图片)。列出通过 sdcard 中的某个文件夹访问的项目

How to set an icon(picture) for each row in listview. List view items access through some folder in sdcard

我是 android 开发的新手。我在 sdcard 中有一些 pdf 文件,我已经在 listView 中成功显示它们,我可以从 listview 本身打开它们。

现在我需要为每个列表项(pdf)设置一个图标(任何图片)。

请帮忙。

代码如下:

CustomListVIewAdapter.java

public class CustomListViewAdapter extends ArrayAdapter<RowItem> {

Context context;

public CustomListViewAdapter(Context context, int resourceId,
                             List<RowItem> items) {
    super(context, resourceId, items);
    this.context = context;
}

/*private view holder class*/
private class ViewHolder {
    ImageView imageView;
    TextView txtTitle;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    RowItem rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.custom_list, null);
        holder = new ViewHolder();
        holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
        holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    holder.txtTitle.setText(rowItem.getTitle());
    holder.imageView.setImageResource(rowItem.getImageId());

    return convertView;
}
}

Main.java

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_download_reports, container, false);

    ListView listView;
    List<RowItem> rowItems;
    final ArrayList<String> FilesInFolder = AccessFiles(Environment.getExternalStorageDirectory()
            + "/SOMEFOLDER");

    rowItems = new ArrayList<RowItem>();
    for (int i = 0; i < FilesInFolder.size(); i++) {
        RowItem item = new RowItem(images[i], FilesInFolder.get(i));
        rowItems.add(item);
    }

    listView = (ListView) rootView.findViewById(R.id.list_of_files);

    CustomListViewAdapter adapter = new CustomListViewAdapter(getActivity(), R.layout.custom_list, rowItems);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            String fileName = FilesInFolder.get(position);
            open_File(fileName);
        }
    });
    return rootView;
}

//Access folder from the path that is passed and get it in listview
public ArrayList<String> AccessFiles(String DirectoryPath) {
    ArrayList<String> Reports = new ArrayList<String>();
    File file = new File(DirectoryPath);

    file.mkdirs();
    File[] files = file.listFiles();
    if (files.length == 0)
        return null;
    else {
        for (int i = 0; i < files.length; i++)
            Reports.add(files[i].getName());
    }
    return Reports;
}

//Open file method to open file on listitem click 
public void open_File(String filename) {
    File filee = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/SOMEFOLDER", filename);

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(filee), "application/pdf");
    intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    Intent intent1 = Intent.createChooser(intent, "Open With");
    try {
        startActivity(intent1);
    } catch (ActivityNotFoundException e) {
        // Instruct the user to install a PDF reader here, or something
    }
}
}

RowItem.java

public class RowItem {
    private int imageId;
    private String title;

    public RowItem(int imageId, String title) {
        this.imageId = imageId;
        this.title = title;
    }
    public int getImageId() {
        return imageId;
    }
    public void setImageId(int imageId) {
        this.imageId = imageId;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    @Override
    public String toString() {
        return title;
    }
}

错误

日志

D/AndroidRuntime﹕ Shutting down VM
05-28 14:10:51.810    7888-7888/myprojectname
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: myprojectname, PID: 7888
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
        at projectname/main.onCreateView(main.java:47)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
        at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:456)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

您还没有在 code.Also 下面添加 getcount() method.Add 初始化项目。

public int getCount() {
        return items.size();
}

我终于做到了

我不必非得选择 CustomVIewAdapter。简单地说,ArrayAdpter 完成了这项工作。

ArrayAdapter 适配器 = new ArrayAdapter(getActivity(), R.layout.custom_list, R.id.title, FilesInFolder); // FilesInFolder 是我的文件存放在sdcard的路径