mercredi 26 août 2015

how to over write item in Database instead of creating new item

I am working with sqlite database, and if i add an existing item in my database i want it to overwrite and just add 1 in its quantity i know this is possible but i don't know how. can someone help me with this please i will really appreciate it if someone do (Im new in programming) this is for my project*

this is my window for adding an item to data base and show it in listview

public class pop_up extends Activity {
    int qty;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pop_up);

        EditText quantity = (EditText)findViewById(R.id.quantity);

        quantity.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                try{
                    qty = 1;
                }catch(Exception e){

                }

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try{
                    qty = Integer.valueOf(s.toString()).intValue();
                }catch(Exception e){

                }
            }
        });

        Intent getpop_up = getIntent();

        final int price = getpop_up.getIntExtra("passedprice", 0);
        final String name = getpop_up.getStringExtra("passedname");



        Button add = (Button) findViewById(R.id.add);




        TextView menuprice = (TextView)findViewById(R.id.menuprice);
        String initialprice = String.valueOf(price);
        menuprice.setText(initialprice);

        TextView menuname = (TextView)findViewById(R.id.menuname);
        String currentname = String.valueOf(name);
        menuname.setText(currentname);


        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast toast;
                int tprice = ((price)*(qty));

                SQLiteDatabase db = openOrCreateDatabase("Orders.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
                MDB mdb = new MDB(getApplicationContext(), "Orders.db", null, 1);
                db = mdb.getWritableDatabase();
                ContentValues cv = new ContentValues();
                cv.put("Food", name);
                cv.put("Price", tprice);
                cv.put("Quantity", qty);
                db.insert("OrderedList", null, cv);

                toast=Toast.makeText(getApplicationContext(), "Added successfully", Toast.LENGTH_SHORT);

                finish();
                toast.show();
            }


        });

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        int width=dm.widthPixels;
        int height = dm.heightPixels;

        getWindow().setLayout((int) (width),(int) (height*.6));



    }
}

this is my listview adapter

public class ListView_Adapter extends ArrayAdapter<String> {
    private final Context context;
    private String[] column1;
    private double[] column2;
    private int[] column3;

    public ListView_Adapter(Context context, String[] column1, double[] column2, int[] column3) {
        super(context, R.layout.list_adapter, column1);
        this.context = context;
        this.column1 = column1;
        this.column2 = column2;
        this.column3 = column3;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_adapter, parent, false);
        }
        TextView t1 = (TextView) convertView.findViewById(R.id.t1);
        TextView t2 = (TextView) convertView.findViewById(R.id.t2);
        TextView t3 = (TextView) convertView.findViewById(R.id.t3);

        t1.setText(column1[position]);
        t2.setText("₱" + column2[position]);
        t3.setText("" + column3[position]);
        return convertView;
    }
} 

Aucun commentaire:

Enregistrer un commentaire