0
到的ListView我有一個SQLite數據庫連接到我的申請,我也跟着教程和教程顯示了在SimpleCursorAdapter數據庫,這確實全班這個適配器和我的按鈕不適合在這個類。 因此,我要減少這種適配器以配合我的按鈕。 或者還有其他更好的解決方案嗎? 對我來說,SimpleCursorAdapter混合所有我的照片的按鈕,他的適配器, 我不知道,但我認爲,SCadapter使得整個視圖變成一個列表視圖,我不希望出現這種情況。 我有四個班。 將對DBAdapter, Produkter(查看) 車是SimpleCursorAdapter ViewBinder 和公共汽車是位圖獲得,類變化SimpleCursorAdapter在XML
enter code here
public class DBhelper {
public static final String KEY_ID = BaseColumns._ID;
public static final String KEY_NAME = "name";
public static final String KEY_KCAL = "kcal";
public static final String KEY_VC = "vitaminc";
public static final String KEY_IMG = "image";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_NAME = "FruitDB";
private static final int DATABASE_VERSION = 1;
public static final String FRUITS_TABLE = "fruits";
private static final String CREATE_FRUITS_TABLE = "create table "+FRUITS_TABLE+" ("
+KEY_ID+" integer primary key autoincrement, "
+KEY_IMG+" blob not null, "
+KEY_NAME+" text not null unique, "
+KEY_KCAL+" integer not null, "
+KEY_VC+" integer not null);";
private final Context mCtx;
private boolean opened = false;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_FRUITS_TABLE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+FRUITS_TABLE);
onCreate(db);
}
}
public void Reset() {
openDB();
mDbHelper.onUpgrade(this.mDb, 1, 1);
closeDB();
}
public DBhelper(Context ctx) {
mCtx = ctx;
mDbHelper = new DatabaseHelper(mCtx);
}
private SQLiteDatabase openDB() {
if(!opened)
mDb = mDbHelper.getWritableDatabase();
opened = true;
return mDb;
}
public SQLiteDatabase getHandle() { return openDB(); }
private void closeDB() {
if(opened)
mDbHelper.close();
opened = false;
}
public void createFruitEntry(Bus fruit) {
openDB();
ByteArrayOutputStream out = new ByteArrayOutputStream();
fruit.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, out);
ContentValues cv = new ContentValues();
cv.put(KEY_IMG, out.toByteArray());
cv.put(KEY_NAME, fruit.getName());
cv.put(KEY_KCAL, fruit.getKcal());
cv.put(KEY_VC, fruit.getVitaminC());
mDb.insert(FRUITS_TABLE, null, cv);
closeDB();
}
} enter code here
public class produkter extends ListActivity {
private DBhelper mDB;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDB = new DBhelper(this);
mDB.Reset();
Bitmap img = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
mDB.createFruitEntry(new Bus(img, "Banane", 92, 10));
mDB.createFruitEntry(new Bus(img, "Kiwi", 56, 71));
mDB.createFruitEntry(new Bus(img, "Pfirsich", 41, 10));
mDB.createFruitEntry(new Bus(img, "Zitrone", 40, 51));
String[] columns = {mDB.KEY_ID, mDB.KEY_IMG, mDB.KEY_NAME, mDB.KEY_KCAL,
mDB.KEY_VC};
String table = mDB.FRUITS_TABLE;
Cursor c = mDB.getHandle().query(table, columns, null, null, null, null,
null);
startManagingCursor(c);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.produkterx,
c,
new String[] {mDB.KEY_IMG, mDB.KEY_NAME, mDB.KEY_KCAL, mDB.KEY_VC},
new int[] {R.id.img, R.id.txt, R.id.rating});
adapter.setViewBinder(new Car());
setListAdapter(adapter);
enter code here
public class Car implements SimpleCursorAdapter.ViewBinder {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if(view instanceof ImageView) {
ImageView iv = (ImageView) view;
byte[] img = cursor.getBlob(columnIndex);
iv.setImageBitmap(BitmapFactory.decodeByteArray(img, 0,
img.length));
return true;
}
if(view instanceof RatingBar) {
RatingBar rb = (RatingBar) view;
int kcal = cursor.getInt(cursor.getColumnIndex(DBhelper.KEY_KCAL));
int vitc = cursor.getInt(cursor.getColumnIndex(DBhelper.KEY_VC));
rb.setRating((float) (kcal * ((float) vitc/1000.0)));
return true;
}
return false;
}
}
enter code here
public class Bus {
private Bitmap bmp;
private String name;
private int kcal;
private int vitaminc;
public Bus(Bitmap b, String n, int k, int v) {
bmp = b;
name = n;
kcal = k;
vitaminc = v;
}
public Bitmap getBitmap() { return bmp; }
public String getName() { return name; }
public int getKcal() { return kcal; }
public int getVitaminC() { return vitaminc; }
}
enter code here` XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/Blue"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1" >
<Button
android:id="@+id/btnE"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="E" />
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginTop="-7dp"
android:contentDescription="@drawable/header2"
android:src="@drawable/header2" />
<Button
android:id="@+id/btnkontakt"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="k" />
<Button
android:id="@+id/btnprodukter"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/btnE"
android:text="p" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
<Button
android:id="@+id/btnSok"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/btnprodukter"
android:text="s" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="-7dp"
android:contentDescription="@drawable/sok"
android:src="@drawable/produkter" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="1" >
<ImageView
android:id = "@+id/img"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignParentLeft = "true"
>
</ImageView>
<TextView
android:id = "@+id/txt"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_centerVertical = "true"
>
</TextView>
<RatingBar
style="?android:attr/ratingBarStyleSmall"
android:id = "@+id/rating"
android:numStars = "5"
android:stepSize = "0.5"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignParentRight = "true"
android:layout_centerVertical = "true"
android:layout_marginRight = "5px">
</RatingBar>
</LinearLayout>
</LinearLayout>
> Blockquote
This XML is all in a simpelCA that i dont want!!