0

I am trying to Populate a list from MySQL lite database to a list view. I found some tutorials that a CursorAdaptor is good so that I could update list based on id.

But i tried the tutorials while no data is listed in listview.

I add and Update contents to database as:

 {
if(count > 0){
            String earlierQty = mydb.getDataFromList("qty",item.getId(),tabid);
            BigDecimal updatedQty = new BigDecimal(earlierQty).setScale(2).add(new BigDecimal(item.getQty()).setScale(2));
            BigDecimal updatedAmount = setCurrentAmount(item.getId(),updatedQty,tabid);
            total  = setTotalAmount(updatedAmount,new BigDecimal(item.getTax()).setScale(2));
            taxamount = setTaxAmount(updatedAmount,new BigDecimal(item.getTax()).setScale(2));
            mydb.updateARowByProductId(item.getTitle(), "pos", String.valueOf(updatedAmount),String.valueOf(updatedQty),new BigDecimal(item.getUnitPrice()).setScale(2).toString(),item.getDiscount(),String.valueOf(taxamount),item.getQuantityin(),String.valueOf(total),tabid,item.getId());
        }
        else{
            Log.e("PRICE ",item.getUnitPrice());
            total = new BigDecimal(item.getUnitPrice()).setScale(2).multiply(new BigDecimal(item.getTax()).setScale(2).divide(new BigDecimal(100)));
            total = total.add(new BigDecimal(item.getUnitPrice()).setScale(2)).setScale(2);
            taxamount = setTaxAmount(new BigDecimal(item.getUnitPrice()),new BigDecimal(item.getTax()).setScale(2));
            Log.e("taxamount",taxamount.toString());
            mydb.addContact(new ListItem(item.getTitle(), "pos", new BigDecimal(item.getUnitPrice()).setScale(2).toString(),new BigDecimal(item.getQty()).setScale(2).toString(),new BigDecimal(item.getUnitPrice()).setScale(2).toString(),item.getDiscount(),tabid,item.getId(),String.valueOf(taxamount),item.getQuantityin(),total.toString()));
        }
     showList(tabid);
        }

My ShowList function is trying to populate the values to list:

public void showList(String tab_id){
            Cursor listdata = mydb.getAllCursorTabItems(tab_id);
            try {
                mListAdapter = new ListviewCursorAdapter(getActivity(), listdata);
                mListview.setAdapter(mListAdapter);
            } catch (Exception e) {

            } finally {
                //mListAdapter.changeCursor(listdata);
                //scrollMyListViewToBottom();
                BigDecimal subtotalValue = new BigDecimal(mydb.getProductListAmountSUM(tab_id)).setScale(2);
                BigDecimal discountvalue = new BigDecimal(mydb.getProductListDiscountSUM(tab_id)).setScale(2);
                BigDecimal taxvalue = new BigDecimal(mydb.getProductListAmountTAX(tab_id)).setScale(2);
                BigDecimal totalvalue = new BigDecimal(mydb.getProductListTotalSUM(tab_id)).setScale(2);
                totalvalue = totalvalue.subtract(discountvalue).setScale(2);
               subtotal.setText(subtotalValue+" "+getString(R.string.rs));
               discount.setText(discountvalue+" "+getString(R.string.rs));
               tax.setText(taxvalue.toString()+" "+getString(R.string.rs));
               total.setText(totalvalue.toString()+" "+getString(R.string.rs));
            }
        }

my Adapter Class is:

    public class ListviewCursorAdapter extends CursorAdapter {
        public ListviewCursorAdapter(Context context, Cursor cursor) {
            super(context, cursor, 0);
        }

        // The newView method is used to inflate a new view and return it,
        // you don't bind any data to the view at this point.
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            return LayoutInflater.from(context).inflate(R.layout.list_row, parent, false);
        }

        // The bindView method is used to bind all data to a given view
        // such as setting the text on a TextView.
        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            // Find fields to populate in inflated template
            TextView name = (TextView) view.findViewById(R.id.name);
            TextView type = (TextView) view.findViewById(R.id.type);
            TextView amount = (TextView) view.findViewById(R.id.amount);
            // Extract properties from cursor


    String text_name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
        String text_type = cursor.getString(cursor.getColumnIndexOrThrow("type"));
        String text_amount = cursor.getString(cursor.getColumnIndexOrThrow("amount"));
        String text_qty = cursor.getString(cursor.getColumnIndexOrThrow("quantity"));
        String text_unit = cursor.getString(cursor.getColumnIndexOrThrow("unit"));
        String text_price = cursor.getString(cursor.getColumnIndexOrThrow("price"));
        String text_discount = cursor.getString(cursor.getColumnIndexOrThrow("discount"));
        String discription = text_qty + " "+text_unit+ " at " +text_price +"/"+text_unit+" with discount "+text_discount;
        name.setText(text_name);
        type.setText(discription);
        amount.setText(text_amount);
    }
}

UPDATE

I catched inside the showlist:

ERROR SHOWING IN CATCH:

E/ERROR: java.lang.IllegalArgumentException: column '_id' does not exist

Karthik CP
  • 1,150
  • 13
  • 24

0 Answers0