按钮上的数据未插入按下按钮

时间:2017-05-05 17:08:30

标签: android sqlite

当我点击按钮插入数据时,它表示成功,但是当我检查列表视图时没有数据。但如果我再次添加,则只插入数据。

为什么数据仅在第二次插入?

提前致谢! :d

这是我的数据库助手类:

 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;
}

这是我的MainActivity类:

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);
    }
}

这是我的适配器类:

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;
}

4 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为在将适配器设置为listView之后,您正在向orderName,orderSize和orderQuantity添加数据。而你甚至没有打电话

adapter.notifyDataSetChanged();

让适配器知道dataSet已更改

答案 1 :(得分:1)

问题是适配器不知道您已向数据库添加了元素。

后:

db.addPerson(name,size,bottleCaseQuantity);

你应该

adapter.notifyDataSetChanged()

答案 2 :(得分:1)

为什么数据仅在第二次插入?

问题出在你的while循环中。当只有one订单时,您的while循环body将不会被执行,因为您已使用data.moveToNext()作为条件。如果您的订单数量为more than one,则只会进入while循环。

错误:

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());
}

希望这会有所帮助〜

答案 3 :(得分:0)

好吧,我解决了这个问题

我在检索数据方面做了一些工作,但它确实有效!

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

再次感谢任何想要和帮助的人:D