0

我的应用程序有一个 SQLite 数据库连接,我遵循了一个教程,该教程在 SimpleCursorAdapter 中显示了数据库,这对这个适配器进行了整个类,我的按钮不适合这个类。因此,我会减少这个适配器以适合我的按钮。或者有没有其他更好的解决方案?对我来说,SimpleCursorAdapter 将我所有的图片混合为他的适配器的按钮,我不知道,但我认为 SAdapter 将整个视图变成了列表视图,我不想要那样。我有四节课。DBAdapter, Produkter (View) Car 是 SimpleCursorAdapter ViewBinder 而 Bus 是 Bitmap get, class

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!!
4

1 回答 1

0

我对这个问题有点困惑。您的简单光标适配器旨在将数据发送到列表视图以填充您的屏幕。

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

您需要匹配 String[]/int[]。如果您不想显示某些内容,请不要将其包含在此处。

于 2012-04-27T09:17:20.467 回答