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外部ストレージ (sdcard) 内のフォルダーにあるとします。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 に答える