0

这个自定义列表视图让我很兴奋。我一直在寻找解决这个问题的方法,但是我找不到其他人以与我相同的方式设置图像和数据库。

我有一个 SQLite 驱动的应用程序,它可以拍照并将图片保存在名为 /Pictures 的外部存储文件夹中,并且仅将数据库中的文件名保存为字符串(即 20190815_062534.jpg)。

我想要一个自定义列表视图,它使用数据库中的文件名来调用 /Pictures 文件夹中的图像。

我之前创建了一个显示各个记录的不同活动,我使用下面的方法来检索该活动的图像...

private void getImage(String infotopassFile) {
        String photoPath = Environment.getExternalStorageDirectory() + "/Pictures/" + infotopassFile;
        File file = new File(photoPath);
        if(file.canRead()) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
            ImageView ivPicture_view = (ImageView) findViewById(R.id.ivPicture_view);
            ivPicture_view.setImageBitmap(bitmap);
        }
        else {
            Toast.makeText(ViewRecordActivity.this, "IMAGE FILE COULD NOT BE FOUND", Toast.LENGTH_LONG).show();

        }
    }

此外,我在 PDFwriter 中使用了上述代码的变体,以在报告中显示图像。这两个操作都工作得很好,所以我知道所有图像都存在并且都可以访问,而且我知道我调用图像的方法是可行的。

问题是,我不知道何时何地调用函数 getImage()(或它的一些变体)以让应用程序在正确的时间调用图像。当我尝试调用具有自定义列表视图的 Activity 时,我尝试放置的每个地方都会导致应用程序崩溃。

我意识到解决方案将比仅仅在一个位置随机粘贴几行代码更复杂,但我需要一些帮助来找到一个起点。

我有从数据库请求信息作为数组的活动。我有一个运行查询并返回数组的 SQLiteHelper。我有一个自定义列表适配器,它将数组设置为列表视图的各个部分。而且我有一门课来处理个人记录,老实说,我真的不明白它的作用,但它确实有效。

我将在哪些文件中开始尝试检索图像?

下面的代码是我上面引用的四个 java 文件。他们目前正在从数据库中提取文本并将其显示在列表视图中,并且可以正常工作。我只是不知道我需要从哪里开始引用外部位置的图片。

//**********SQLLiteHelper.java**********

public class SQLLiteHelper extends SQLiteOpenHelper {

    //This is the query for the custom list adapter
    public ArrayList<Records> getAllRecords(){
        SQLiteDatabase db = this.getWritableDatabase();
        ArrayList<Records> myList = new ArrayList<Records>();
        String query = "SELECT fld_ID, fldName , fldFile FROM TABLE_table ORDER BY fld_ID DESC";
        Cursor cursor = db.rawQuery(query, null);

        while (cursor.moveToNext())
        {
            String ID = cursor.getString(cursor.getColumnIndex("fld_ID"));
            String NAME = cursor.getString(cursor.getColumnIndex("fldName"));
            String FILE = cursor.getString(cursor.getColumnIndex("fldFile"));
            String PATH = Environment.getExternalStorageDirectory() + "/Pictures/" + FILE;

            myList.add(new Records(ID, NAME, FILE, PATH));
        }

        return myList;

    }
}


//********** ActivityCustomListView.java**********

public class ActivityCustomListView extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_list_view);

        //identify the custom list view container in activity layout file
        ListView mListView = (ListView)findViewById(R.id.customListViewContainer);

        //get array of data from database
        SQLLiteHelper db = new SQLLiteHelper(this);
        ArrayList<Records> recordsArray = db.getAllRecords();

        //call the custom adapter
        CustomListAdapter adapter = new CustomListAdapter(this, R.layout.custom_record,recordsArray);

        //set the listview to the adapter
        mListView.setAdapter(adapter);
    }
}


//********** CustomListAdapter.java**********

public class CustomListAdapter extends ArrayAdapter<Records> {
    private Context mContext;
    private int mResource;
    private int lastPosition = -1;

    private static class ViewHolder{
        TextView ID;
        TextView Name;
        TextView File;
        TextView Path;
    }

    public CustomListAdapter(Context context, int resource, ArrayList<Records>objects){
        super (context, resource, objects);
        mContext = context;
        mResource = resource;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        String ID = getItem(position).getID();
        String NAME = getItem(position).getName();
        String FILE = getItem(position).getFile();
        String PATH = getItem(position).getPath();

        Records records = new Records(ID,NAME,FILE,PATH);

        final View result;
        ViewHolder holder;

        if(convertView == null){
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(mResource, parent, false);
            holder = new ViewHolder();
            holder.ID = (TextView)convertView.findViewById(R.id.tvcID);
            holder.Name = (TextView)convertView.findViewById(R.id.tvcName);
            holder.File = (TextView)convertView.findViewById(R.id.tvcFile);
            holder.Path = (TextView)convertView.findViewById(R.id.tvcPath);

            result = convertView;

            convertView.setTag(holder);
        }
        else{
            holder=(ViewHolder)convertView.getTag();
            result = convertView;
        }

        holder.ID.setText(records.getID());
        holder.Name.setText(records.getName());
        holder.File.setText(records.getFile());
        holder.Path.setText(records.getPath());

        return convertView;
    }
}

    //********** Records.java**********

public class Records {

    private String ID;
    private String NAME;
    private String FILE;
    private String PATH;

    public Records(String id, String name, String file, String path) {
        this.ID = id;
        this.NAME = name;
        this.FILE = file;
        this.PATH = path;
    }

    public String getID(){
        return ID;}

    public String getName(){
         return NAME;}

    public String getFile(){
          return FILE;}

    public String getPath(){
           return PATH;}

}
4

1 回答 1

0

我想到了!!!!耶我!!!

我发布更新的代码是因为其他人可能正在寻找类似的解决方案。我让我的自定义列表与保存在外部存储器中的图片一起工作的方法是一直回到原始 SQLite 查询并让创建的数组添加一个新的 Bitmap 元素。然后通过其他操作携带该元素。这可能不是正确的方法,但它确实有效。

//**********SQLLiteHelper.java**********

public class SQLLiteHelper extends SQLiteOpenHelper {

//This is for the custom list adapter
    public ArrayList<Records> getAllRecords(){
        SQLiteDatabase db = this.getWritableDatabase();
        ArrayList<Records> myList = new ArrayList<Records>();
        String query = "SELECT fld_ID, fldName , fldFile FROM TABLE_table ORDER BY fld_ID DESC";
        Cursor cursor = db.rawQuery(query, null);

        while (cursor.moveToNext())
        {
            String ID = cursor.getString(cursor.getColumnIndex("fld_ID"));
            String NAME = cursor.getString(cursor.getColumnIndex("fldName"));
            String FILE = cursor.getString(cursor.getColumnIndex("fldFile"));
            String PATH = Environment.getExternalStorageDirectory() + "/Pictures/" + FILE;

                File photoFILEasFILE = new File(PATH);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                Bitmap photoAsBitmap = BitmapFactory.decodeFile(String.valueOf(photoFILEasFILE), options);

            myList.add(new Records(ID, NAME, FILE, PATH, photoAsBitmap));
        }

        return myList;

    }



//********** ActivityCustomListView.java**********

public class ActivityCustomListView extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_list_view);

        //identify the custom list view container in activity layout file
        ListView mListView = (ListView)findViewById(R.id.customListViewContainer);

        //get array of data from database
        SQLLiteHelper db = new SQLLiteHelper(this);
        ArrayList<Records> recordsArray = db.getAllRecords();

        //call the custom adapter
        CustomListAdapter adapter = new CustomListAdapter(this, R.layout.custom_record,recordsArray);

        //set the listview to the adapter
        mListView.setAdapter(adapter);

    }
}



//********** CustomListAdapter.java**********

public class CustomListAdapter extends ArrayAdapter<Records>  {

    private Context mContext;
    private int mResource;

    private static class ViewHolder{
        TextView ID;
        TextView Name;
        TextView File;
        TextView Path;
        ImageView Photo;
    }

    public CustomListAdapter(Context context, int resource, ArrayList<Records>objects){
        super (context, resource, objects);
        mContext = context;
        mResource = resource;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        String ID = getItem(position).getID();
        String NAME = getItem(position).getName();
        String FILE = getItem(position).getFile();
        String PATH = getItem(position).getPath();
        Bitmap photoAsBitmap = null;
        try {
            photoAsBitmap = getItem(position).getPicture();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BadElementException e) {
            e.printStackTrace();
        }

        Records records = new Records(ID,NAME,FILE,PATH,photoAsBitmap);

        final View result;
        ViewHolder holder;

        if(convertView == null){
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(mResource, parent, false);
            holder = new ViewHolder();
            holder.ID = (TextView)convertView.findViewById(R.id.tvcID);
            holder.Name = (TextView)convertView.findViewById(R.id.tvcName);
            holder.File = (TextView)convertView.findViewById(R.id.tvcFile);
            holder.Path = (TextView)convertView.findViewById(R.id.tvcPath);
            holder.Photo = (ImageView)convertView.findViewById(R.id.ivcPicture);

            result = convertView;

            convertView.setTag(holder);
        }
        else{
            holder=(ViewHolder)convertView.getTag();
            result = convertView;
        }

        holder.ID.setText(records.getID());
        holder.Name.setText(records.getName());
        holder.File.setText(records.getFile());
        holder.Path.setText(records.getPath());
        try {
            holder.Photo.setImageBitmap(records.getPicture());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BadElementException e) {
            e.printStackTrace();
        }
        return convertView;
    }
}


//********** Records.java**********

public class Records {

    private String ID;
    private String NAME;
    private String FILE;
    private String PATH;
    private Bitmap photoAsBitmap;

    public Records(String id, String name, String file, String path, Bitmap photoFILEasBitmap) {
        this.ID = id;
        this.NAME = name;
        this.FILE = file;
        this.PATH = path;
        this.photoAsBitmap = photoFILEasBitmap;
    }

    public String getID(){return ID;}

    public String getName(){return NAME;}

    public String getFile(){return FILE;}

    public String getPath(){return PATH;}

    public Bitmap getPicture() throws IOException, BadElementException {

        File photoFILEasFILE = new File(PATH);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        photoAsBitmap = BitmapFactory.decodeFile(String.valueOf(photoFILEasFILE), options);

        return photoAsBitmap;

    }

}
于 2019-08-21T04:29:41.283 回答