Gionne Lapuz
Gionne Lapuz

Reputation: 548

Data not being inserted in Table on Button Press

When I tap the button for inserting the data it says it is successful, but when I check my listview there is no data. But If I add again, then only the data is inserted.

Why is the data only inserted on the second time?

Thanks in advance! :D

This is my Database Helper class:

 public static final String DB_NAME = "CartDB";
public static final String TABLE_NAME = "Orders";
public static final String COLUMN_ID = "id";
public static final String NAME ="name";
public static final String SIZE ="size";
public static final String QUANTITY ="quantity";

private static final int DB_VERSION = 1;

public cartDatabaseHelper(Context context)
{
    super(context,DB_NAME,null,DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

    String sql = "CREATE TABLE " + TABLE_NAME
            + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + NAME + " VARCHAR, "
            + SIZE + " VARCHAR, "
            + QUANTITY + " VARCHAR);";

              db.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String sql = "DROP TABLE IF EXIST Orders";
    db.execSQL(sql);
    onCreate(db);
}

public boolean addPerson(String name, String size, String quantity){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();

    contentValues.put(NAME,name);
    contentValues.put(SIZE,size);
    contentValues.put(QUANTITY,quantity);

    long result = db.insert(TABLE_NAME,null ,contentValues);
    if(result == -1)
        return false;
    else
        return true;
}


public Cursor getListContents(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
    return data;
}

And this is my MainActivity class:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_alcohol_list);

    db = new cartDatabaseHelper(this);

  GridAlcoholAdapter adapter = new GridAlcoholAdapter(alcoholType.this, images, names);
    gridView.setAdapter(adapter);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                final int position, long id) {

   btnAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    String name = names.get(position);
                    String size = textSize.getText().toString().trim();
                    String quantityNumber = textQuantityNumber.getText().toString().trim();
                    String bottleCase = textBottleCase.getText().toString().trim();

                    String bottleCaseQuantity = textQuantity.getText().toString().trim();

                    textQuantity.setText(quantityNumber + " " + bottleCase);

                    db.addPerson(name,size,bottleCaseQuantity);

                    dialog.dismiss();
                }
            });



     @Override
       public boolean onOptionsItemSelected(MenuItem item) {
    // Take appropriate action for each action item click
    switch (item.getItemId()) {

        case R.id.action_cart:

            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.cartdialog);
            dialog.setTitle("YOUR CART");

            listView = (ListView) dialog.findViewById(R.id.listView);
            final ListCartAdapter adapter = new ListCartAdapter(alcoholType.this, orderName, orderSize, orderQuantity);

            listView.setAdapter(adapter);

            Cursor data = db.getListContents();
            data.moveToFirst();
            while (data.moveToNext()) {
                orderName.add(data.getString(1));
                orderSize.add(data.getString(2));
                orderQuantity.add(data.getString(3));
            }
            data.close();

            dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    orderName.clear();
                    orderSize.clear();
                    orderQuantity.clear();
                }
            });

            dialog.show();

            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

This is my Adapter Class:

public class ListCartAdapter extends BaseAdapter {


private Context context;

private ArrayList<String> orderName;
private ArrayList<String> orderSize;
private ArrayList<String> orderQuantity;

public ListCartAdapter(Context context, ArrayList<String> orderName, ArrayList<String> orderSize, ArrayList<String> orderQuantity){
  // public ListCartAdapter(Context context, ArrayList<String> orderName){
    this.context = context;
    this.orderName = orderName;
    this.orderSize = orderSize;
    this.orderQuantity = orderQuantity;
}

@Override
public int getCount() {
    return orderName.size();
}

@Override
public Object getItem(int position) {
    return orderName.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View listView;
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    listView = inflater.inflate(R.layout.cart_list_item, null);

    TextView name = (TextView) listView.findViewById(R.id.textOrderName);
    TextView size = (TextView) listView.findViewById(R.id.textOrderSize);
    TextView quantity = (TextView) listView.findViewById(R.id.textOrderQuantity);

    name.setText(orderName.get(position));
    size.setText(orderSize.get(position));
    quantity.setText(orderQuantity.get(position));

    return listView;
}

Upvotes: 0

Views: 36

Answers (4)

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21766

Why is the data only inserted on the second time?

The problem is in your while loop. When there is only one order then your while loop body will not be executed because you have used data.moveToNext() as condition. If your order count more than one, only then it will enter into the while loop.

ERROR:

data.moveToFirst();
while (data.moveToNext()) {
    orderName.add(data.getString(1));
    orderSize.add(data.getString(2));
    orderQuantity.add(data.getString(3));
}

SOLUTION:

if(data.moveToFirst())
{
    do
    {
        orderName.add(data.getString(1));
        orderSize.add(data.getString(2));
        orderQuantity.add(data.getString(3));

    } while(data.moveToNext());
}

Hope this will help~

Upvotes: 1

Gionne Lapuz
Gionne Lapuz

Reputation: 548

Well guys I fixed the problem

I made a do-while in retrieving the data and it works!

   do{
           orderName.add(data.getString(1));
           orderSize.add(data.getString(2));
           orderQuantity.add(data.getString(3));
                } while (data.moveToNext());

thanks again to anyone who wanted and helped :D

Upvotes: 0

Gustavo Conde
Gustavo Conde

Reputation: 977

The problem is that the adapter doesn't know that you have added an element to the database.

After:

db.addPerson(name,size,bottleCaseQuantity);

you should make

adapter.notifyDataSetChanged()

Upvotes: 1

Ali Ahsan
Ali Ahsan

Reputation: 470

this is happening because you are adding data to orderName,orderSize and orderQuantity after setting adapter to listView. and you are not even calling

adapter.notifyDataSetChanged();

to let the adapter know that dataSet has changed

Upvotes: 1

Related Questions