Android 我需要一个自定义的列表视图与图像和文本视图从数据库使用光标
我有一个使用数组适配器的listview,它是从数据库中检索到的,可以正常工作。 但我有存储在数据库中的图像路径,我希望它显示在自定义列表中,使用光标显示图像和文本视图。有人给我举个例子吗Android 我需要一个自定义的列表视图与图像和文本视图从数据库使用光标,android,cursor,android-sqlite,custom-lists,Android,Cursor,Android Sqlite,Custom Lists,我有一个使用数组适配器的listview,它是从数据库中检索到的,可以正常工作。 但我有存储在数据库中的图像路径,我希望它显示在自定义列表中,使用光标显示图像和文本视图。有人给我举个例子吗 public class SingleItem extends Activity { DatabaseHelper db; Cursor c; private CustomAdapter Adapter; protected Object mActionMode; String[] Phno,Des
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;
}
});
公共类SingleItem扩展活动{
数据库辅助数据库;
光标c;
专用自定义适配器;
保护对象模式;
字符串[]Phno,说明;
整数[]金额;
int j=0,位置;
私有ArrayList ArrayList=新ArrayList();
私人数据库费用;
列表视图细节;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.singleitem);
TextView项=(TextView)findViewById(R.id.textView1);
详细信息=(ListView)findViewById(R.id.listView1);
Intent i=getIntent();
字符串st=i.getStringExtra(“票据”);
项目1.setText(st);
试一试{
db=新的DatabaseHelper(此);
//打开数据库
费用=db.getWritableDatabase();
c=expenses.rawQuery(“从详细信息中选择*,其中名称为“+”'%“+st+“%”,null);
Phno=新字符串[c.getCount()];
Description=新字符串[c.getCount()];
金额=新整数[c.getCount()];
如果(c!=null){
if(c.moveToFirst()){
做{
Phno[j]=c.getString(c.getColumnIndex(“Phno”);
//字符串名称=c.getString(c.getColumnIndex(“名称”));
Description[j]=c.getString(c.getColumnIndex(“Description”);
金额[j]=c.getInt(c.getColumnIndex(“金额”);
字符串日期=c.getString(c.getColumnIndex(“日期”);
//字符串Image=c.getString(c.getColumnIndex(“Image”);
arrayList.add(“说明:“+Description[j]+”\n”+
金额:“+金额[j]+”\n+
“日期:”+日期);
j++;
}而(c.moveToNext());
}
}
}catch(sqlitese异常){
Log.e(getClass().getSimpleName(),
“无法创建或”+“打开数据库”);
}最后{
如果(费用!=null)
费用。关闭();
}
setAdapter(新的ArrayAdapter(这个,android.R.layout.simple_list_item_1,arrayList));
detail.setOnItemLongClickListener(新的OnItemLongClickListener(){
//当用户长时间单击someView时调用
公共布尔值长单击(AdapterView父项、视图、整型位置、长id){
pos=位置;
if(mActionMode!=null){
返回false;
}
//使用上面定义的ActionMode.Callback启动CAB
mActionMode=SingleItem.this.startActionMode(mActionModeCallback);
view.setSelected(true);
返回true;
}
});
在这个例子中,我认为图像的路径在外部存储(SD卡)中的<代码> MyAppFolder < /Cord>文件夹中。我有2个字段,<代码> iTimeNo.<代码>和<代码> ITEMIIGION 存储的文件名。< /P>
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;
}
}