Note that there are some explanatory texts on larger screens.

plurals
  1. POChange SimpleCursorAdapter to listview in XML
    primarykey
    data
    text
    <p>I have a SQLite Database connection to my application, I have followed a Tutorial and that tutorial show the database in a SimpleCursorAdapter, this does the whole class to this adapter and my buttons can not fit in this class. I would therefore reduce this adapter to to fit with my buttons. Or is there any other better solution? For me the SimpleCursorAdapter Mix all my pictures an button to his adapter, I dont know but i think that the SCadapter makes the whole view into a list view and i dont want that. I have four classes. DBAdapter, Produkter (View) Car is the SimpleCursorAdapter ViewBinder and Bus is the Bitmap get, class</p> <pre><code>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 &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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" &gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_weight="1" &gt; &lt;Button android:id="@+id/btnE" android:layout_width="70dp" android:layout_height="70dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:text="E" /&gt; &lt;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" /&gt; &lt;Button android:id="@+id/btnkontakt" android:layout_width="70dp" android:layout_height="70dp" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:text="k" /&gt; &lt;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" /&gt; &lt;ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" &gt; &lt;/ListView&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:layout_weight="1" &gt; &lt;ImageView android:id = "@+id/img" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignParentLeft = "true" &gt; &lt;/ImageView&gt; &lt;TextView android:id = "@+id/txt" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerVertical = "true" &gt; &lt;/TextView&gt; &lt;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"&gt; &lt;/RatingBar&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt; &gt; Blockquote This XML is all in a simpelCA that i dont want!! </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload