android - Reading and Writing to a SQLITE3 database from a ListView populated by a CursorAdapter -
i made android app online program part of. app supposed track inventory. app uses cursoradapter populate listview 3 inventory details - productname, product price , quantity. part of assignment add button on each list item when clicked quantity decreased 1 , new value stored in database. problem having each listitem populated cursoradapter, code not in activity. know how read , write database activity not cursoradapter. assuming cursoradapter need put code because place there logic textviews shown in each list item. tried has failed. unfortunately can't show have tried because @ point decided delete , start scratch.
here each list item should like.
here code cursoradapter:
public class inventorycursoradapter extends cursoradapter { private uri mcurrentinventoryuri; /** * constructs new {@link inventorycursoradapter}. * * @param context context * @param c cursor data. */ public inventorycursoradapter(context context, cursor c) { super(context, c, 0 /* flags */); } /** * makes new blank list item view. no data set (or bound) views yet. * * @param context app context * @param cursor cursor data. cursor * moved correct position. * @param parent parent new view attached * @return newly created list item view. */ @override public view newview(context context, cursor cursor, viewgroup parent) { // inflate list item view using layout specified in list_item.xml return layoutinflater.from(context).inflate(r.layout.list_item, parent, false); } @override public void bindview(view view, context context, cursor cursor) { // find individual views want modify in list item layout textview nametextview = (textview) view.findviewbyid(r.id.name); final textview quantitytextview = (textview) view.findviewbyid(r.id.quantity); textview pricetextview = (textview) view.findviewbyid(r.id.price); // find columns of product attributes we're interested in int namecolumnindex = cursor.getcolumnindex(inventoryentry.column_product_name); int quantitycolumnindex = cursor.getcolumnindex(inventoryentry.column_product_quantity); int pricecolumnindex = cursor.getcolumnindex(inventoryentry.column_product_price); // read product attributes cursor current product string productname = cursor.getstring(namecolumnindex); //string productname = "productname"; int productquantity = cursor.getint(quantitycolumnindex); int productprice = cursor.getint(pricecolumnindex); if (textutils.isempty(productname)) { productname = context.getstring(r.string.unknown_product); } // update textviews attributes current product nametextview.settext(productname); quantitytextview.settext(integer.tostring(productquantity)); pricetextview.settext(" $" + integer.tostring(productprice)); }
}
you can see can use cursor , display data listview, don't know how update quantity, , save database. when tried adding button view binded listview, other touch functionalities had set click on list doesn't work (touching on list item opens detailed screen more product info).
i tried including functionality main activity since button isn't located in activity when called getviewbyid ended being null , app crashed. included enough code snippet, not sure else have been relevant. in addition posted have 2 other activity (main activity , activity edit details of inventory), have database helper files reading , writing database.
to make easier on reading this, there 2 major obstacles facing. 1). place button. using layout called cursoradapter best place? 2). put code update database? using cursoradapter provided challenges me.
thanks in advance..
Comments
Post a Comment