【问题标题】:I need a custom listview with image and textview from database using cursor我需要一个自定义列表视图,其中包含使用光标从数据库中获取的图像和文本视图
【发布时间】:2013-11-10 08:29:56
【问题描述】:

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

    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;
        }
    });

}

【问题讨论】:

    标签: android cursor android-sqlite custom-lists


    【解决方案1】:

    在此示例中,我认为图像的路径位于外部存储(sdcard)中的MyAppFolder 文件夹中。我有 2 个字段,Item_NameItem_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;
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多