0

我有使用从数据库中检索的数组适配器的文本列表视图。它工作正常。但是我将图像路径存储在数据库中,我希望它使用光标显示在带有图像和文本视图的自定义列表中。任何人都建议我举个例子

    public class SingleItem extends Activity {
DatabaseHelper db;
Cursor c;

private CustomAdapter Adapter;
protected Object mActionMode;
String[] Phno,Description;
int[] Amount;
int j=0,pos;
private ArrayList<String> arrayList = new ArrayList<String>();
private SQLiteDatabase expenses;
ListView detail;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.singleitem);
    TextView item=(TextView) findViewById(R.id.textView1);
    detail=(ListView) findViewById(R.id.listView1);
    Intent i=getIntent();
    String st=i.getStringExtra("Bill");
    item.setText(st);
    try {
        db = new DatabaseHelper(this);
        //Open the database
        expenses = db.getWritableDatabase();
        c = expenses.rawQuery("SELECT * FROM Details where Name like"+"'%"+st+"%'", null);
        Phno=new String[c.getCount()];
        Description=new String[c.getCount()];
        Amount=new int[c.getCount()];

        if (c != null ) {
            if  (c.moveToFirst()) {
                do {

                    Phno[j] = c.getString(c.getColumnIndex("Phno"));
                    //String Name = c.getString(c.getColumnIndex("Name"));
                    Description[j] = c.getString(c.getColumnIndex("Description"));
                    Amount[j] = c.getInt(c.getColumnIndex("Amount"));
                    String Date = c.getString(c.getColumnIndex("Date"));
                    //String Image=c.getString(c.getColumnIndex("Image"));

                    arrayList.add("Description    :"+Description[j]+"\n"+
                                "Amount       : " +Amount[j] +"\n"+
                            "Date    :"+Date                                    );
                    j++;
                }while (c.moveToNext());
            } 
        }           
    } catch (SQLiteException se ) {
        Log.e(getClass().getSimpleName(),
                "Could not create or " +"Open the database");
    } finally {
        if (expenses != null) 

            expenses.close();
    }


    detail.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arrayList));


    detail.setOnItemLongClickListener(new OnItemLongClickListener() {

        // Called when the user long-clicks on someView
        public boolean onItemLongClick (AdapterView parent, View view, int position, long id) {
            pos=position;
            if (mActionMode != null) {

                return false;
            }

            // Start the CAB using the ActionMode.Callback defined above
            mActionMode = SingleItem.this.startActionMode(mActionModeCallback);

            view.setSelected(true);
            return true;
        }
    });

}

4

1 回答 1

0

在此示例中,我认为图像的路径MyAppFolder位于外部存储(SD 卡)的文件夹中。我有 2 个字段,Item_Name以及Item_Image图像的存储文件名。

public class CustomImageAdapter extends BaseAdapter {
/** The parent context */
private Context myContext;
private Cursor cursor;
/** Simple Constructor saving the 'parent' context. and cursor */
public CustomImageAdapter(Context c, Cursor cursor) {
    this.myContext = c;
    this.cursor = cursor;

}

// inherited abstract methods - must be implemented
// Returns count of images, and individual IDs
public int getCount() {
    return cursor.getCount();
}

public Object getItem(int position) {
    return cursor.getPosition();
}

public long getItemId(int position) {
    cursor.moveToPosition(position);
    return cursor.getLong(0);
}
// Returns a new ImageView to be displayed,
public View getView(int position, View convertView, 
        ViewGroup parent) {

    cursor.moveToPosition(position);
    LayoutInflater inflater = (LayoutInflater) myContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.image_row, parent, false);
    TextView tvItem = (TextView) rowView.findViewById(R.id.tvItem);
    tvItem.setText(cursor.getString(cursor.getColumnIndex("Item_Title")));

    ImageView ivItem_Image = (ImageView) rowView.findViewById(R.id.ivItem_Image);
    // You can specify path of image file here
    String path = "\\MyAppFolder";
    File imgFile=new File(Environment.getExternalStorageDirectory() + path ,cursor.getString(cursor.getColumnIndex("Item_Imag")));
    if(imgFile.exists()){
        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
        ivItem_Image.setImageBitmap(myBitmap);
    }
    ivItem_Image.setScaleType(ScaleType.FIT_XY);
    return rowView;
}
}
于 2013-11-10T09:42:14.230 回答