我想在编辑文本中赋值时,每次都将编辑文本值存储到我的数据库中。我知道如何更新我的表格。每次编辑文本值更改此处编辑文本指示我想要根据数量计算数量的食谱数量,因此我想在db中存储数量值。请帮我。如何将特定行值存储到db中我可以编写更新查询但是整个表更新发生在那里。 提前致谢
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class YourOrder extends HelperActivity {
private ListView list;
private DataBaseHandler db;
ArrayList<Contact> imageArry = new ArrayList<Contact>();
OrdersImageAdapter adapter;
public static TextView toatalamount;
EditText qtyView,quantityt;
int total;
double recipeamount=0;
public static Button payment;
public Integer id = null;
public String name = null;
public Integer price = null;
public int quantiy;
public String quan;
double ordercount = 0;
private Double orderTotal = 0.00;
static String ordertotalval;
public String setvaluefinal;
int dbcount = 0;
public double editcount=0;
int count = NewBreakfastItems.getVariable();
ImageHolder holder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourorders);
list = (ListView) findViewById(R.id.listView1);
toatalamount = (TextView) findViewById(R.id.rupees);
db = new DataBaseHandler(this);
payment = (Button) findViewById(R.id.buy);
// Reading all contacts from database
List<Contact> contacts = db.getAllContacts();
Log.d("", "------DATABASE CONTACTS-------" + db.getAllContacts());
for (Contact cn : contacts) {
String log = "ID:" + cn.getID() + " Name: " + cn.getName()
+ " ,Image: " + cn.getImage() + "Price :" + cn.getprice();
Log.d("Name: ", log);
Log.i("", "-----ID-------" + cn.getID());
Log.i("", "-----ID-------" + cn.getName());
Log.i("----recipe price---", "-----price----" + cn.getprice());
id = cn.getID();// Here i am getting the id no...
name = cn.getName();
price = cn.getprice();
ordercount = ordercount + price;
Log.e("---ordercount---", "----ordercount----" + ordercount);
toatalamount.setText(String.valueOf(ordercount));
/*
* count=count+price; Log.i("---count----", "-----count----"+count);
*/
// Writing Contacts to log
Log.e("Result:========== ", "=====LOG=====" + log);
imageArry.add(cn);
}
Log.d("", "-----ID-------" + id);
Log.d("", "-----ID-------" + name);
adapter = new OrdersImageAdapter(this, R.layout.orderscart, imageArry);
list.setAdapter(adapter);
dbcount = db.getContactsCount();
Log.e("----dbcount----", "-----dbcount---" + dbcount);
payment.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(),
Payment.class);
//intent.putExtra("count", count);
intent.putExtra("ordertotalval", ordertotalval);
intent.putExtra("name", name);
startActivity(intent);
}
});
footerBlock();
}
public class OrdersImageAdapter extends ArrayAdapter<Contact> {
// Contact contact;
Context context;
public int dltcount;
int layoutResourceId;
int adaptercount = 0;
public int extPrice;
ArrayList<Contact> data = new ArrayList<Contact>();
public OrdersImageAdapter(Context context, int layoutResourceId,
ArrayList<Contact> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
this.data = new ArrayList<Contact>();
this.data.addAll(data);
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
DecimalFormat df = new DecimalFormat("0.00##");
Contact product = data.get(position);
View row = convertView;
holder = null;
Contact lContact = (Contact) list.getItemAtPosition(position);
db = new DataBaseHandler(getContext());
if (row == null) {
LayoutInflater inflater = ((Activity) context)
.getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
EditText quantity = (EditText) row.findViewById(R.id.quantity);
//attach the TextWatcher listener to the EditText
quantity.addTextChangedListener(new MyTextWatcher(row));
holder = new ImageHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.txtTitle);
holder.imgIcon = (ImageView) row.findViewById(R.id.imgIcon);
holder.dlttxt = (TextView) row.findViewById(R.id.dlt);
holder.quantity = (EditText) row.findViewById(R.id.quantity);
holder.recipeprice=(TextView) row. findViewById(R.id.recipeprice);
row.setTag(holder);
} else {
holder = (ImageHolder) row.getTag();
}
quantityt = (EditText) row.findViewById(R.id.quantity);
quantityt.setTag(product);
if(product.getQuantity() != 0){
quantityt.setText(String.valueOf(product.getQuantity()));
}
else {
quantityt.setText("");
}
TextView ext = (TextView) row.findViewById(R.id.setprice);
if(product.getQuantity() != 0){
ext.setText("$" + df.format(product.getExt()));
}
else {
ext.setText("");
}
holder.recipeprice.setText(String.valueOf(lContact.getprice()+"X"));
/* ====DELETING RECIPE ITEMS==== */
holder.dlttxt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.e("---pos---", "----position----" + position);
Contact lContact = (Contact) list
.getItemAtPosition(position);
Log.e("---pos---", "----position----" + lContact);
Log.e("TAG", "" + lContact.getID());
db.deleteContact(lContact.getID());
// db.deleteContact(pos);
Log.e("", "======deleted=====" + lContact.getID());
Log.e("", "====NAME====" + lContact.getName());
quan = lContact.getName();
Log.e("---lcontact price---", "------lcntact price----"
+ lContact.getprice());
adaptercount = lContact.getprice();
Log.e("","quantity---"+quantityt.getText());
Toast.makeText(
getContext(), lContact.getName() + ":"
+ "Deleted Sucessfully", Toast.LENGTH_SHORT)
.show();
//setvaluefinal=qtyView.getText().toString();
//ordercount=Integer.parseInt(setvaluefinal);
Log.e("", "-----order count----"+ordercount);
//editcount=adaptercount * ordercount;
Log.e("", "----edit count----"+editcount);
//Log.e("", "----order total value----"+orderTotal);
//recipeamount=orderTotal-editcount;
//Log.e("", "------recipe amount-------"+recipeamount);
imageArry.remove(lContact);
adapter.notifyDataSetChanged();// updating adapter
count = db.getContactsCount();
Log.d("", "----updated count----" + db.getContactsCount());
HelperActivity.num.setText(String.valueOf(count));
//toatalamount.setText(String.valueOf(recipeamount));
// YourOrder.toatalamount.setText();
}
});
holder.quantity.setText("1");
Contact picture = data.get(position);
holder.txtTitle.setText(picture._name);
// convert byte to bitmap take from contact class
byte[] outImage = picture._image;
ByteArrayInputStream imageStream = new ByteArrayInputStream(
outImage);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
holder.imgIcon.setImageBitmap(theImage);
// db.deleteContact(null);
return row;
}
public class ImageHolder {
ImageView imgIcon;
TextView txtTitle;
TextView dlttxt;
EditText quantity;
TextView recipeprice;
}
private class MyTextWatcher implements TextWatcher{
private View view;
private MyTextWatcher(View view) {
this.view = view;
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//do nothing
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//do nothing
}
public void afterTextChanged(Editable s) {
DecimalFormat df = new DecimalFormat("0.00##");
String qtyString = s.toString().trim();
int quantity = qtyString.equals("") ? 0:Integer.valueOf(qtyString);
qtyView = (EditText) view.findViewById(R.id.quantity);
// here i am getting the quantity value i want to store thid value into db
Contact contact = (Contact) qtyView.getTag();
// quantityt.setText(qtyView.getText());
if(contact.getQuantity() != quantity){
int currPrice = contact.getExt();
extPrice = quantity * contact.getprice();
Double priceDiff = Double.valueOf(df.format(extPrice - currPrice));
contact.setQuantity(quantity);
contact.setExt(extPrice);
TextView setprice = (TextView) view.findViewById(R.id.setprice);
if(contact.getQuantity() != 0){
setprice.setText("$" + df.format(contact.getExt()));
}
else {
setprice.setText("");
}
if(contact.getQuantity() != 0){
qtyView.setText(String.valueOf(contact.getQuantity()));
}
else {
qtyView.setText("");
}
orderTotal += priceDiff;
toatalamount.setText(df.format(orderTotal));
ordertotalval=toatalamount.getText().toString();
Log.e("----total value----", "------total value-----"+ordertotalval);
}
}
}
}
}
答案 0 :(得分:0)
您可以使用sqlite中的update方法进行更新,
ContentValues values=new ContentValues();
values.put(update_column_name, value_of_update_column);
db.update(DATABASE_TABLE_NAME, values, condition_field_name+" =?", new String[] {condition_value_});
答案 1 :(得分:0)
但是要更新行,您的表格中有id
。在Where
条件中,使用该ID更新特定行。
试试这个
public SQLiteDatabase db;
String UpdateVAlue = editText.getText().toString(); // Getting edittext value
ContentValues updateValues = new ContentValues();
updateValues.put("ColomnFieldName", UpdateVAlue); // adding thevalues in Content view
db.update("TABLE_NAME", updateValues, WHERE,new String[] {
String.valueOf(ConditionValue) });