我有一个listview,我正在使用自定义游标适配器从sqlite生成数据。我在listview中有两个文本字段和一个Imageview。文本字段已正确填充,但imageview始终显示相同的图像
这是我的代码
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Food.db";
public static final String BREAKFAST_TABLE = "breakfast_table";
public static final String LUNCH_TABLE = "lunch_table";
public static final String DINNER_TABLE = "dinner_table";
public static final String SNACKS_TABLE = "snacks_table";
public static final String TOTAL_TABLE = "total_table";
public static final String RECEIVED_TABLE = "received_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "CAL";
public static final String COL_4 = "IMAGE";
public long result;
private SQLiteDatabase db;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 2);
db = getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + BREAKFAST_TABLE + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,CAL INTEGER)");
db.execSQL("create table " + LUNCH_TABLE + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,CAL INTEGER)");
db.execSQL("create table " + DINNER_TABLE + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,CAL INTEGER)");
db.execSQL("create table " + SNACKS_TABLE + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,CAL INTEGER)");
db.execSQL("create table " + TOTAL_TABLE + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,CAL INTEGER)");
db.execSQL("create table " + RECEIVED_TABLE + " (_id INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,CAL INTEGER,IMAGE BLOB)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + BREAKFAST_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + LUNCH_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + DINNER_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + SNACKS_TABLE);
this.onCreate(db);
}
public Integer deleteData(String name,String tableName){
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(tableName, "NAME = ?" ,new String[] {name});
}
public void insertData(Food food,String tableName) {
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, food.getName());
contentValues.put(COL_3, food.getCal());
contentValues.put(COL_4,food.getImage());
food.setId(db.insert(tableName, null, contentValues));
}
public void insertDataToTotal(Food food) {
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, food.getName());
contentValues.put(COL_3, food.getCal());
food.setId(db.insert(TOTAL_TABLE, null, contentValues));
}
public ArrayList<Food> getAllFood(String tableName) {
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<Food> listItems = new ArrayList<Food>();
Cursor cursor = db.rawQuery("SELECT * from " + tableName,
new String[] {});
if (cursor.moveToFirst()) {
do {
Food food = new Food(cursor.getString(cursor.getColumnIndex(COL_2)),cursor.getInt(cursor.getColumnIndex(COL_3)));
listItems.add(food);
} while (cursor.moveToNext());
}
cursor.close();
return listItems;
}
public Cursor getCursor(){
String selectQuery = "SELECT * FROM received_table ORDER BY name";
Cursor c = db.rawQuery(selectQuery, null);
return c;
}
public ArrayList<Food> getAllContacts() {
ArrayList<Food> foodList = new ArrayList<Food>();
String selectQuery = "SELECT * FROM received_table ORDER BY name";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Food food = new Food();
food.setName(cursor.getString(1));
food.setCal(cursor.getInt(2));
food.setImage(cursor.getBlob(3));
foodList.add(food);
} while (cursor.moveToNext());
}
db.close();
return foodList;}}
public class ReceivedItems extends AppCompatActivity {
ListView receivedMenu;
DatabaseHelper myDb;
Adapter itemAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recieved_items);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
myDb = new DatabaseHelper(this);
itemAdapter = new Adapter(this,myDb.getCursor());
receivedMenu = (ListView) findViewById(R.id.receivedMenu);
receivedMenu.setAdapter(itemAdapter);}}
class Adapter extends CursorAdapter {
private LayoutInflater mLayoutInflater;
private Context mContext;
public Adapter(Context context, Cursor c) {
super(context, c);
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = mLayoutInflater.inflate(R.layout.received_list, parent, false);
return v;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView txtName=(TextView)view.findViewById(R.id.receivedName);
TextView txtCal=(TextView)view.findViewById(R.id.recCal);
ImageView image = (ImageView)view.findViewById(R.id.recievedIcon);
txtName.setText(cursor.getString(1));
txtCal.setText(cursor.getString(2));
ByteArrayInputStream imageStream = new ByteArrayInputStream(cursor.getBlob(3));
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
image.setImageBitmap(theImage);}}
编辑:here is a sample from my received table
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:textAlignment="viewEnd">
<ImageView
android:id="@+id/recievedIcon"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/receivedName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#33CC33"
android:paddingBottom="5dp"
android:layout_weight="0.5" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#33CC33"
android:textAlignment="viewEnd"
android:id="@+id/recCal"
android:paddingBottom="5dp"
android:layout_weight="0.5" />