Skip to content Skip to sidebar Skip to footer

Show Data In My Android App(saving Works Just Fine Now)

hey, now i can save data in my db, but it doesn't show up in the database activity itself. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(

Solution 1:

Please provide more information. As in the error log. The error might be in the statement itself.Add the details from the logcat first.It will clarify.

Solution 2:

I think the insert query is missing word 'values'. Try printing that query string on console executing the same through console on db. That may help you identify error.

Solution 3:

I have two things to point out:

What is i in your SQL query? Is it an int? If so that might be the problem because the way you are using it, it should be a String.

Secondly, why don't you use the insert convenience function of SQLiteDatabase?

ContentValuescv=newContentValues();
cv.put ( "Name", Name.getText().toString() );
cv.put ( "Comment", Comment.getText().toString() );
cv.put ( "BookingDetails", BookingDetails.getText().toString() );
cv.put ( "Project_kind", i );
cv.put ( "Editable", Editable.getText().toString() );
myDB.insert ( Projectshome.MY_DB_TABLE, null, cv );

This takes all the complexity of building the SQL query out of inserting rows - it will ensure that the query is correctly built, all you have to do is build the ContentValues map properly.

Solution 4:

OK, so here's how I do it:

row.xml (Used for each row in the list view):

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical"android:padding="4px"
    ><LinearLayoutandroid:id="@+id/LinearLayout01"android:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="wrap_content"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center_vertical"android:textStyle="bold"android:singleLine="true"android:ellipsize="end"android:id="@+id/itemDescription"/><TextViewandroid:text="@+id/TextView01"android:id="@+id/Category"android:layout_width="wrap_content"android:layout_height="wrap_content"></TextView></LinearLayout><LinearLayoutandroid:id="@+id/LinearLayout01"android:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="wrap_content"><TextViewandroid:layout_width="fill_parent"android:text="@+id/TextView01"android:gravity="center_vertical"android:layout_weight="1"android:singleLine="true"android:ellipsize="end"android:id="@+id/itemManufacturer"android:layout_height="wrap_content"></TextView><TextViewandroid:text="@+id/TextView01"android:id="@+id/EAN"android:layout_width="wrap_content"android:layout_height="wrap_content"></TextView></LinearLayout></LinearLayout>

I extend CursorAdapter to create or re-use existing ListActivity rows (supplied by the ListActivity) defined in row.xml. bindView is used if Android wants you to re-use an existing row resource, newView is used if it wants you to create a new one:

classBarcodeAdapterextendsCursorAdapter {
    BarcodeAdapter(Cursor c) {
        super(kitchenListInventoryActivity.this, c);
    }

    @OverridepublicvoidbindView(View row, Context ctxt,
                                             Cursor c) {
        BarcodeHolder holder=(BarcodeHolder)row.getTag();
        holder.populateFrom(c, helper);
    }

    @Overridepublic View newView(Context ctxt, Cursor c,
                                             ViewGroup parent) {
        LayoutInflater inflater=getLayoutInflater();
        Viewrow=null;
        row = inflater.inflate(R.layout.row, parent, false);
        BarcodeHolder holder=newBarcodeHolder(row);

        row.setTag(holder);

        return(row);
    }
}

BarcodeHolder is just a convenience class to hold a Java representation of the row, which I associate with the ListActivity item with setTag above. Not necessarily the most elegant way, but it simplifies things.:

staticclassBarcodeHolder {
        private TextView description=null;
        private TextView manufacturer=null;
        privateTextViewEAN=null;
        privateTextViewcategory=null;
        private View row=null;

        BarcodeHolder(View row) {
            this.row=row;

            description=(TextView)row.findViewById(R.id.itemDescription);
            manufacturer=(TextView)row.findViewById(R.id.itemManufacturer);
            EAN=(TextView)row.findViewById(R.id.EAN);
            category = ( TextView )row.findViewById(R.id.Category);
        }

        voidpopulateFrom(Cursor c, BarcodeHelper helper) {
            CursorinvCursor= helper.getByInventoryEAN(helper.getBarcode(c));
            CursordbCursor= helper.getByEAN(helper.getBarcode(c));
            dbCursor.moveToFirst();
            invCursor.moveToFirst();
            intqty= helper.getQuantity ( invCursor );
            Stringdesc= helper.getDescription(dbCursor) + " (" + String.valueOf( qty ) + ")" ;
            description.setText( desc );
            manufacturer.setText(helper.getManufacturer(dbCursor));
            category.setText(helper.getCategory(dbCursor));
        }
    }

To bring this all together, I call initList in my onCreate of the ListActivity:

privatevoidinitList () {
    if ( model != null ) {
        stopManagingCursor(model);
        model.close();
    }

    model = helper.getAllInventory();
    startManagingCursor(model);
    adapter = new BarcodeAdapter(model);
    setListAdapter(adapter);
}

model is a Cursor, and adapter is an instance of BarcodeAdapter, which I've shown above. I tell the ListActivity to use this adapter so that I can correctly populate the rows. getAllInventory is a member of BarcodeHelper which overrides SQLiteOpenHelper:

publicCursorgetAllInventory() {
    String sql = "SELECT _id, EAN, quantity FROM Inventory ORDER BY EAN";
    return(getReadableDatabase().rawQuery(sql, null));
}

The helper object just contains a collection of convenience functions.

All of this is based on CommonsWare's excellent Android tutorial book which I cannot recommend enough, but there should be enough here to get you going.

Post a Comment for "Show Data In My Android App(saving Works Just Fine Now)"