Android data storage and retrieving - android

I developed an android application in which a users provides 6 pieces of information in 6 separate text boxes (information such as first name, last name, middle name, mobile number email id etc)
When the user presses a Save button, I need to store this information and be able to recall this information when the Recall button is pressed.
What is the next step to accomplish this
My code so far is below:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DatePicker date = (DatePicker) findViewById (R.id.datePicker1);
final TextView tv1 = (TextView) findViewById (R.id.textView88);
final TextView tv2 = (TextView) findViewById (R.id.textView89);
final TextView tv3 = (TextView) findViewById (R.id.textView90);
final TextView tv4 = (TextView) findViewById (R.id.textView91);
final TextView tv5 = (TextView) findViewById (R.id.textView92);
final TextView tv6 = (TextView) findViewById (R.id.textView93);
final TextView tv7 = (TextView) findViewById (R.id.textView94);
date.init(date.getYear(), date.getMonth(), date.getDayOfMonth(),new OnDateChangedListener()
{
#Override
public void onDateChanged(DatePicker arg0, int arg1, int arg2, int arg3)
{
// TODO Auto-generated method stub
String date=Integer.toString(arg3);
String month=Integer.toString(arg2);
String year=Integer.toString(arg1);
tv1.setText(date+ month+1 +year);
tv2.setText(month+1 + date);
tv3.setText(year);
tv4.setText(month+1);
tv5.setText(date);
tv6.setText(date + year);
}
});}
public void sum(View v)
{
long sum1=0;
long sum2=0;
long sum3=0;
long sum4=0;
long sum5=0;
long sum6=0;
long sum7=0;
long sum8=0;
long sum=0;
long sum11=0;
long sum12=0;
long sum13=0;
long sum14=0;
long sum100=0;
EditText et1 = (EditText) findViewById (R.id.editText1);
EditText et2 = (EditText) findViewById (R.id.editText2);
EditText et3 = (EditText) findViewById (R.id.editText3);
EditText et4 = (EditText) findViewById (R.id.editText4);
EditText et5 = (EditText) findViewById (R.id.editText5);
EditText et6 = (EditText) findViewById (R.id.editText6);
final DatePicker date = (DatePicker) findViewById (R.id.datePicker1);
final TextView tv1 = (TextView) findViewById (R.id.textView88);
final TextView tv2 = (TextView) findViewById (R.id.textView89);
final TextView tv3 = (TextView) findViewById (R.id.textView90);
final TextView tv4 = (TextView) findViewById (R.id.textView91);
final TextView tv5 = (TextView) findViewById (R.id.textView92);
final TextView tv6 = (TextView) findViewById (R.id.textView93);
final TextView tv7 = (TextView) findViewById (R.id.textView94);
sum1=getSum(et1.getText().toString() + et2.getText().toString() + et3.getText().toString() + et4.getText().toString());
sum2=getSum1(et1.getText().toString() + et2.getText().toString() + et3.getText().toString() + et4.getText().toString());
sum3=getSum2(tv1.getText().toString());
/*sum4=getSum3(tv2.getText().toString());*/
sum5=getSum4(tv3.getText().toString());
sum6=getSum5(tv4.getText().toString());
sum8=getSum7(tv5.getText().toString());
String yearf = tv3.getText().toString();
int yearb = Integer.valueOf(yearf);
int val = 2013 - yearb;
long val1 = sum3;
/*int val2 = Integer.valueOf(val1);*/
long val3 = 36 - val1;
int val4 = 9;
long val5 = val3 + val4;
long val6 = val5 + val4;
long val7 = val6 + 50;
sum = getSum3(tv2.getText().toString()) + getSum8(tv6.getText().toString());
sum100 = getSum4(tv3.getText().toString()) + getSum5(tv4.getText().toString());
if (val <= val3)
{
sum4=getSum3(tv2.getText().toString());
}
else if ((val <= val5) && (val > val3))
{
sum4=getSum8(tv6.getText().toString());
}
else if ((val<=val6) && (val > val5) && (val > val3))
{
/*sum = getSum3(tv2.getText().toString()) + getSum8(tv6.getText().toString());*/
sum4=getSum9(Integer.toString((int) sum));
}
else
{
sum4=getSum9(Integer.toString((int) sum100));
}
if (val <= val3)
{
if (sum8>sum6)
{
sum7 = (sum8 - sum6);
}
else
{
sum7 = (sum6 - sum8);
}
}
else if ((val <= val5) && (val > val3))
{
if(sum5>sum8)
{
sum7=(sum5 - sum8);
}
else
{
sum7 = (sum8 - sum5);
}
}
else if ((val<=val6) && (val > val5) && (val > val3))
{
sum11 = (sum5 - sum8);
sum12 = (sum8 - sum5);
sum13 = (sum6 - sum8);
sum14 = (sum8 - sum6);
if ((sum11 > sum12) && (sum13 > sum14))
{
if (sum11 > sum13)
{
sum7 = sum11 - sum13;
}
else
{
sum7 = sum13 - sum11;
}
}
else if ((sum12 > sum11) && (sum13 > sum14))
{
if (sum12 > sum13)
{
sum7 = sum12 - sum13;
}
else
{
sum7 = sum13 - sum12;
}
}
else if ((sum11 > sum12) && (sum14 > sum13))
{
if (sum11 > sum14)
{
sum7 = sum11 - sum14;
}
else
{
sum7 = sum14 - sum11;
}
}
else if ((sum12 > sum11) && (sum14 > sum13))
{
if (sum12 > sum14)
{
sum7 = sum12 - sum14;
}
else
{
sum7 = sum14 - sum12;
}
}
}
else
{
if (sum5>sum6)
{
sum7 = (sum5 - sum6);
}
else
{
sum7 = (sum6 - sum5);
}
}
String edtxt1 = et1.getText().toString();
String edtxt4 = et4.getText().toString();
String edtxt5 = et5.getText().toString();
String edtxt6 = et6.getText().toString();
if(edtxt1.equals(""))
{
Toast.makeText(getApplicationContext(),"First Name should not be left blank, Please enter your First Name ", Toast.LENGTH_LONG).show();
}
else if(edtxt4.equals(""))
{
Toast.makeText(getApplicationContext(),"Last Name should not be left blank, Please enter your Last Name ", Toast.LENGTH_LONG).show();
}
else if(edtxt5.equals(""))
{
Toast.makeText(getApplicationContext(),"Mobile Number should not be left blank, Please enter your Mobile Number ", Toast.LENGTH_LONG).show();
}
else if(edtxt6.equals(""))
{
Toast.makeText(getApplicationContext(),"E-Mail id should not be left blank, Please enter your E-Mail id ", Toast.LENGTH_LONG).show();
}
else{
Intent i = new Intent(this, result.class);
i.putExtra("name", sum1 + "");
i.putExtra("name1", sum2 + "");
i.putExtra("name2", sum3 + "");
i.putExtra("name3", sum4 + "");
i.putExtra("name4", sum7 + "");
startActivity(i);
}
public long getSum(String text)
{
// TODO Auto-generated method stub
long sum1 = 0;
char[] name = new char[text.length()];
name = text.toCharArray();
for(int i=0; i<text.length(); i++)
{
sum1 += value( name[i] );
}
while (sum1>9)
{
sum1 = findDigitSum(sum1);
}
return sum1;
}
public long getSum1(String text)
{
// TODO Auto-generated method stub
long sum2 = 0;
char[] name1 = new char[text.length()];
name1 = text.toCharArray();
for(int i=0; i<text.length(); i++)
{
sum2 += value1( name1[i] );
}
while (sum2>9)
{
sum2 = findDigitSum(sum2);
}
return sum2;
}

db = openOrCreateDatabase("MyDBI", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS TABLE1 ();");
You can create database using this code
db.execSQL("INSERT INTO TABLE1 VALUES('"+fname"');
And values can be inserted to this database using this code.
A tablelayout can be created and the saved details can be displayed in this tablelayout. Give clickable option and clicking all the details can be transferred using intents to the previous activity.

Related

Unable to calculate proper value from each row in ListView

// i have two button for increasing and decreasing the quantity, after increasing or decreasing the value i want to calculate price from each row but it is not giving correct value.Also the quantity getting changed while scrolling .Mostly the above said problem occurs whenever the size of listview is more than screen size.
public class Baseddapter extends BaseAdapter
{
#Override
public int getCount()
{
return prodetailarray.size();
}
#Override
public Object getItem(int i)
{
// Log.d("item_value",""+prodetailarray.get(i).getPrice());
return null;
}
#Override
public long getItemId(int i)
{
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
listrowposition = position;
final ViewHolder holder;
if (convertView == null)
{
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.singlerowbill_detail, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.txtproname_prodetail);
holder.sellprice = (TextView) convertView.findViewById(R.id.txtprice_prodetail);
holder.proid = (TextView) convertView.findViewById(R.id.txtproid_prodetail);
holder.qty = (TextView) convertView.findViewById(R.id.txtquntty_prodetail);
holder.imgplus = (ImageView) convertView.findViewById(R.id.imgplus_prodetail);
holder.imgminus = (ImageView) convertView.findViewById(R.id.imgminus_prodetail);
holder.singlerowprice = (TextView) convertView.findViewById(R.id.txtsinglerow_price);
holder.baseprice = (TextView) convertView.findViewById(R.id.txt_base_price_prodetail);
holder.taxid = (TextView) convertView.findViewById(R.id.txt_taxid_prodetail);
holder.taxname = (TextView) convertView.findViewById(R.id.txt_taxname_prodetail);
holder.taxval = (TextView) convertView.findViewById(R.id.txt_taxval_prodetail);
// holder.singlerowprice.addTextChangedListener(new MytextWatcher(convertView));
holder.delete = (ImageView) convertView.findViewById(R.id.txtdeleterow);
holder.txtkg = (TextView) convertView.findViewById(R.id.txtkg);
holder.whole_qntty = (TextView) convertView.findViewById(R.id.txt_whole_qntty_prodetail);
holder.delete.setTag(position);
holder.circularImageView = (CircularImageView) convertView.findViewById(R.id.productImage_billdetail);
holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.linearkg);
holder.name.setTypeface(light);
holder.singlerowprice.setTypeface(light);
holder.baseprice.setTypeface(light);
holder.qty.setTypeface(light);
//holder.qty.setTag(position);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
base_price = prodetailarray.get(listrowposition).getBaseprice();
Log.d("baseprice==", "***" + base_price);
holder.baseprice.setText(prodetailarray.get(listrowposition).getBaseprice());
//this means if qnty contain only number means add as unit otherwise add as kg
if(prodetailarray.get(listrowposition).getQntity().matches("[0-9]+"))
{
holder.qty.setText(prodetailarray.get(position).getQntity());
//holder.linearLayout.setVisibility(View.VISIBLE);
holder.imgplus.setVisibility(View.VISIBLE);
holder.imgminus.setVisibility(View.VISIBLE);
// holder.txtkg.setVisibility(View.GONE);
Log.d("qntty_kg??",""+prodetailarray.get(listrowposition).getQntity());
}
else
{
// holder.linearLayout.setVisibility(View.GONE);
holder.imgplus.setVisibility(View.GONE);
holder.imgminus.setVisibility(View.GONE);
// holder.txtkg.setVisibility(View.VISIBLE);
holder.qty.setText(""+prodetailarray.get(listrowposition).getQntity());
Log.d("qntty_kg==",""+prodetailarray.get(listrowposition).getQntity());
// Toast.makeText(Bill_details.this,""+prodetailarray.get(position).getQntity(),Toast.LENGTH_LONG).show();
}
holder.name.setText(prodetailarray.get(listrowposition).getProductname());
holder.sellprice.setText(getResources().getString(R.string.Rs)+" "+prodetailarray.get(position).getPrice());
holder.taxid.setText(prodetailarray.get(listrowposition).getTaxid());
holder.taxname.setText(prodetailarray.get(listrowposition).getTaxname());
holder.taxval.setText(prodetailarray.get(listrowposition).getTaxvalue());
holder.whole_qntty.setText(prodetailarray.get(listrowposition).getWhole_qntty());
holder.proid.setText(prodetailarray.get(listrowposition).getProducuid());
final String str=prodetailarray.get(listrowposition).getQntity();
Double q = Double.parseDouble(str.replaceAll("KG", ""));
holder.singlerowprice.setText(getResources().getString(R.string.Rs)+" " + Double.parseDouble(prodetailarray.get(listrowposition).getPrice()) * q);
drawable = mDrawableBuilder.build(String.valueOf(holder.name.getText().toString().trim().toUpperCase().charAt(0)),
mColorGenerator.getColor((holder.name.getText().toString().trim().charAt(0))));
holder.circularImageView.setImageDrawable(drawable);
holder.delete.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
total_item = totalquantity-1;
totalquantity=total_item;
Integer index = (Integer) view.getTag();
Log.d("indexval==","delete"+index.intValue());
prodetailarray.remove(index.intValue());
adapter.notifyDataSetChanged();
listView.invalidateViews();
Log.d("listCount==",""+listView.getCount());
// totalprice=0;
Double add = 0.0;
Double pr = 0.0;
// listView.invalidateViews();
if (listView.getCount() == 0)
{
totalitem.setText("0 Items");
totalamount.setText( getResources().getString(R.string.Rs)+" 0");
editor.remove("count");
editor.remove("price");
editor.commit();
}
Log.d("liistvisiblepos==",""+(listView.getLastVisiblePosition()-listView.getFirstVisiblePosition())) ;
for (int i = 0; i < listView.getCount(); i++)
{
// this if is for after delete item it will take first item so we dont want like this
if(index.intValue() != i)
{
/* View v = listView.getChildAt(i);
TextView tvquan = (TextView) v.findViewById(R.id.txtquntty_prodetail);
TextView tvprice = (TextView) v.findViewById(R.id.txtsinglerow_price);
*/
String str = holder.qty.getText().toString().trim();
Double p = Double.parseDouble(str.replaceAll(getResources().getString(R.string.Rs), ""));
pr = pr + p;
totalitem.setText("" + total_item + " Items");
totalamount.setText(getResources().getString(R.string.Rs) + " " + pr);
// Log.d("listitemcount==", "tvquan==" + tvquan.getText().toString() + "add==" + add + "price" + pr + "p==" + p);
}
}
}
});
holder.imgplus.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
View v1=view;
PLUSMINUS = 100;
if(holder.qty.getText().toString().matches("[0-9]+"))
{
plus_qnty = Integer.parseInt(holder.qty.getText().toString());
Log.d("plus_qnty==",""+plus_qnty);
//this is for check the quantity that useer enter is not more than qntty in stock
Log.d("whole_qnt_plus==",""+holder.whole_qntty.getText().toString());
whole_qnt= Integer.parseInt(holder.whole_qntty.getText().toString());
Log.d("whole_qnt_plus==",""+whole_qnt);
plus_qnty = plus_qnty + 1;
if(plus_qnty>whole_qnt)
{
Toast.makeText(getApplicationContext(), getResources().getString(R.string.outofstock), Toast.LENGTH_LONG).show();
}
else
{
prodetailarray.get(listrowposition).setQntity(String.valueOf(plus_qnty));
holder.qty.setText("" + plus_qnty);
//remove all the text except price (integer value)
String str=holder.sellprice.getText().toString().trim();
Double sell_price= Double.parseDouble(str.replaceAll(getResources().getString(R.string.Rs), ""));
holder.singlerowprice.setText(getResources().getString(R.string.Rs)+" " + Double.parseDouble(holder.qty.getText().toString()) * sell_price);
prodetailarray.get(listrowposition).setQntity(String.valueOf(plus_qnty));
prodetailarray.get(listrowposition).setBaseprice("" + Double.parseDouble(base_price) * plus_qnty);
}
/*for calculate value after change if any change*/
Double add = 0.0;
Double pr = 0.0;
for (int i = 0;i < listView.getCount(); i++)
{
Log.d("listviewcount==",""+listView.getCount());
View v = listView.getAdapter().getView(i,null,null);
TextView tvquan = (TextView) v.findViewById(R.id.txtquntty_prodetail);
TextView tvprice = (TextView) v.findViewById(R.id.txtsinglerow_price);
// String str_qun=tvquan.getText().toString().trim();
String str_qun=tvquan.getText().toString().trim();
Log.d("listviewcount==",""+str_qun);
Double c = Double.parseDouble(str_qun.replaceAll("KG", ""));
add = c + add;
String str = tvprice.getText().toString().trim();
// String str = holder.singlerowprice.getText().toString().trim();
Log.d("str_qun==",""+str_qun+str);
Double p = Double.parseDouble(str.replaceAll(getResources().getString(R.string.Rs), ""));
pr = pr + p;
// Log.d("listitemcount==", "tvquan==" + tvquan.getText().toString() + "add==" + add + "price" + pr);
totalamount.setText(getResources().getString(R.string.Rs) +" "+ pr);
}
}
else {
}
}
});
holder.imgminus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PLUSMINUS = 200;
if(holder.qty.getText().toString().matches("[0-9]+"))
{
minus_qnty = Integer.parseInt(holder.qty.getText().toString());
if (minus_qnty == 1)
{
// holder.qty.setText(""+);
}
else
{
minus_qnty = minus_qnty - 1;
holder.qty.setText("" + minus_qnty);
String str=holder.sellprice.getText().toString();
Double sell_price= Double.parseDouble(str.replaceAll(getResources().getString(R.string.Rs), ""));
holder.singlerowprice.setText(getResources().getString(R.string.Rs)+" " + Double.parseDouble(holder.qty.getText().toString()) * sell_price);
prodetailarray.get(listrowposition).setQntity(String.valueOf(minus_qnty));
prodetailarray.get(listrowposition).setBaseprice("" + Double.parseDouble(base_price) * minus_qnty);
}
/*for calculate value after change if any change*/
Double add = 0.0;
Double pr = 0.0;
for (int i = 0; i <listView.getCount(); i++)
{
View v = listView.getAdapter().getView(i,null,null);
TextView tvquan = (TextView) v.findViewById(R.id.txtquntty_prodetail);
TextView tvprice = (TextView) v.findViewById(R.id.txtsinglerow_price);
String str_qun=tvquan.getText().toString().trim();
Double c = Double.parseDouble(str_qun.replaceAll("KG", ""));
add = c + add;
String str = tvprice.getText().toString().trim();
Double p = Double.parseDouble(str.replaceAll(getResources().getString(R.string.Rs), ""));
pr = pr + p;
// Log.d("listitemcount==", "tvquan==" + tvquan.getText().toString() + "add==" + add + "price" + pr);
totalamount.setText(getResources().getString(R.string.Rs) +" "+ pr);
}
}
else
{
}
}
});
if(!prodetailarray.get(listrowposition).getQntity().matches("[0-9]+"))
{
holder.qty.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
final Integer index = (Integer) view.getTag();
Log.d("indexval==","index==="+index);
final Dialog dialog_item = new Dialog(Bill_details.this);
dialog_item.setContentView(R.layout.add_item_dialog);
dialog_item.getWindow().setLayout(AppBarLayout.LayoutParams.FILL_PARENT, AppBarLayout.LayoutParams.WRAP_CONTENT);
final EditText edt_enter_qntty = (EditText) dialog_item.findViewById(R.id.txtqntty_item_dialog);
TextView ok = (TextView) dialog_item.findViewById(R.id.txtok_item_dialog);
ok.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
if(edt_enter_qntty.getText().toString().equals(""))
{
}
else
{
Double dialog_qntty= Double.parseDouble(edt_enter_qntty.getText().toString());
// PLUSMINUS = 100;
kg_qntty = dialog_qntty;
//this is for checking entered qunnt is not more than avail qntty
String whole_qnt=holder.whole_qntty.getText().toString();
String whole_qnt_replce= (whole_qnt.replaceAll("KG", ""));
whole_qnt_kg= Double.parseDouble(whole_qnt_replce);
Log.d("whole_qnt_kg==",""+whole_qnt_kg);
if(kg_qntty>whole_qnt_kg)
{
Toast.makeText(getApplicationContext(), getResources().getString(R.string.outofstock), Toast.LENGTH_LONG).show();
}
else
{
prodetailarray.get(listrowposition).setQntity(String.valueOf(kg_qntty));
holder.qty.setText("" + kg_qntty+"KG");
//remove all the text except price (integer value)
String str_qnty=holder.qty.getText().toString();
Double qty= Double.parseDouble(str_qnty.replaceAll("KG", ""));
String str=holder.sellprice.getText().toString();
Double sell_price= Double.parseDouble(str.replaceAll(getResources().getString(R.string.Rs), ""));
holder.singlerowprice.setText(getResources().getString(R.string.Rs)+" " + qty * sell_price);
prodetailarray.get(listrowposition).setQntity(String.valueOf(kg_qntty));
prodetailarray.get(listrowposition).setBaseprice("" + Double.parseDouble(base_price) * kg_qntty);
}
/*for calculate value after change if any change*/
Double add = 0.0;
Double pr = 0.0;
for (int i = 0;i <listView.getCount(); i++)
{
View v = listView.getAdapter().getView(i,null,null);
TextView tvquan = (TextView) v.findViewById(R.id.txtquntty_prodetail);
TextView tvprice = (TextView) v.findViewById(R.id.txtsinglerow_price);
String str_quan=tvquan.getText().toString().trim();
Log.d("strqn",str_quan);
String c = (str_quan.replaceAll("KG", ""));
Log.d("strqn", "" + c);
add = Double.parseDouble(c) + add;
String str_pri=tvprice.getText().toString().trim();
Double p= Double.parseDouble(str_pri.replaceAll(getResources().getString(R.string.Rs), ""));
pr = pr + p;
Log.d("total_qntty", "tvquan==" + tvquan.getText().toString() + "add==" + add + "price" + pr);
// totalitem.setText("" + add+" Items");
totalamount.setText(getResources().getString(R.string.Rs) +" "+ pr);
}
}
dialog_item.dismiss();
}
});
dialog_item.show();
}
});
}
return convertView;
}
#Override
public int getViewTypeCount() {
return getCount();
}
#Override
public int getItemViewType(int position) {
return position;
}
}
Never update an item view in a list from the event handler (controller).
It looks like you are correctly updating the model (prodetailarray), but in an onClick or any event handler, all you want to do is update the model and then call notifyDataSetChanged(). This will inform your list view that the model has changed, and the list view will regenerate item views based on the new model data.
If that's not sufficient to solve your problem, you'll need to post your entire adapter code so we can identify the problem.
//see below getview method which I updated .
public View getView(final int position, View convertView, ViewGroup parent)
{
listrowposition = position;
final ViewHolder holder;
if (convertView == null)
{
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.singlerowbill_detail, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.txtproname_prodetail);
holder.sellprice = (TextView) convertView.findViewById(R.id.txtprice_prodetail);
holder.proid = (TextView) convertView.findViewById(R.id.txtproid_prodetail);
holder.qty = (TextView) convertView.findViewById(R.id.txtquntty_prodetail);
holder.imgplus = (ImageView) convertView.findViewById(R.id.imgplus_prodetail);
holder.imgminus = (ImageView) convertView.findViewById(R.id.imgminus_prodetail);
holder.singlerowprice = (TextView) convertView.findViewById(R.id.txtsinglerow_price);
holder.baseprice = (TextView) convertView.findViewById(R.id.txt_base_price_prodetail);
holder.taxid = (TextView) convertView.findViewById(R.id.txt_taxid_prodetail);
holder.taxname = (TextView) convertView.findViewById(R.id.txt_taxname_prodetail);
holder.taxval = (TextView) convertView.findViewById(R.id.txt_taxval_prodetail);
// holder.singlerowprice.addTextChangedListener(new MytextWatcher(convertView));
holder.delete = (ImageView) convertView.findViewById(R.id.txtdeleterow);
holder.txtkg = (TextView) convertView.findViewById(R.id.txtkg);
holder.whole_qntty = (TextView) convertView.findViewById(R.id.txt_whole_qntty_prodetail);
holder.circularImageView = (CircularImageView) convertView.findViewById(R.id.productImage_billdetail);
holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.linearkg);
holder.name.setTypeface(light);
holder.singlerowprice.setTypeface(light);
holder.baseprice.setTypeface(light);
holder.qty.setTypeface(light);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
base_price = prodetailarray.get(listrowposition).getBaseprice();
holder.qty.setTag(listrowposition);
holder.delete.setTag(listrowposition);
holder.imgplus.setTag(listrowposition);
holder.imgminus.setTag(listrowposition);
Log.d("baseprice==", "***" + base_price);
holder.baseprice.setText(prodetailarray.get(listrowposition).getBaseprice());
//this means if qnty contain only number means add as unit otherwise add as kg
if(prodetailarray.get(listrowposition).getQntity().matches("[0-9]+"))
{
holder.qty.setText(prodetailarray.get(listrowposition).getQntity());
//
holder.imgplus.setVisibility(View.VISIBLE);
holder.imgminus.setVisibility(View.VISIBLE);
//
Log.d("qntty_kg??",""+prodetailarray.get(listrowposition).getQntity());
}
else
{
holder.imgplus.setVisibility(View.GONE);
holder.imgminus.setVisibility(View.GONE);
//
holder.qty.setText(""+prodetailarray.get(listrowposition).getQntity());
Log.d("qntty_kg==",""+prodetailarray.get(listrowposition).getQntity());
}
holder.name.setText(prodetailarray.get(listrowposition).getProductname());
holder.sellprice.setText(getResources().getString(R.string.Rs)+" "+prodetailarray.get(listrowposition).getPrice());
holder.taxid.setText(prodetailarray.get(listrowposition).getTaxid());
holder.taxname.setText(prodetailarray.get(listrowposition).getTaxname());
holder.taxval.setText(prodetailarray.get(listrowposition).getTaxvalue());
holder.whole_qntty.setText(prodetailarray.get(listrowposition).getWhole_qntty());
holder.proid.setText(prodetailarray.get(listrowposition).getProducuid());
final String str=prodetailarray.get(listrowposition).getQntity();
Double q = Double.parseDouble(str.replaceAll("KG", ""));
holder.singlerowprice.setText(getResources().getString(R.string.Rs)+" " + Double.parseDouble(prodetailarray.get(listrowposition).getPrice()) * q);
drawable = mDrawableBuilder.build(String.valueOf(holder.name.getText().toString().trim().toUpperCase().charAt(0)),
mColorGenerator.getColor((holder.name.getText().toString().trim().charAt(0))));
holder.circularImageView.setImageDrawable(drawable);
holder.delete.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
total_item = totalquantity-1;
totalquantity=total_item;
Integer index = (Integer) view.getTag();
Log.d("indexval==","delete"+index.intValue());
prodetailarray.remove(index.intValue());
adapter.notifyDataSetChanged();
listView.invalidateViews();
Log.d("listCount==",""+listView.getCount());
Double add = 0.0;
Double pr = 0.0;
if (listView.getCount() == 0)
{
totalitem.setText("0 Items");
totalamount.setText( getResources().getString(R.string.Rs)+" 0");
editor.remove("count");
editor.remove("price");
editor.commit();
}
for (int i = 0; i < listView.getCount(); i++)
{
// this if is for after delete item it will take first item so we dont want like this
View v = listView.getAdapter().getView(i,null,listView);
if(v==null)
{
return;
}
TextView tvquan = (TextView) v.findViewById(R.id.txtquntty_prodetail);
TextView tvprice = (TextView) v.findViewById(R.id.txtsinglerow_price);
String str_qun=tvquan.getText().toString().trim();
Double c = Double.parseDouble(str_qun.replaceAll("KG", ""));
add = c + add;
String str = tvprice.getText().toString().trim();
Double p = Double.parseDouble(str.replaceAll(getResources().getString(R.string.Rs), ""));
pr = pr + p;
totalitem.setText("" + total_item + " Items");
totalamount.setText(getResources().getString(R.string.Rs) + " " + pr);
Log.d("listitemcount==", "tvquan==" + tvquan.getText().toString() + "add==" + add + "price" + pr + "p==" + p);
}
}
});
holder.imgplus.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
int in=(Integer) view.getTag();
PLUSMINUS = 100;
if(holder.qty.getText().toString().matches("[0-9]+"))
{
plus_qnty = Integer.parseInt(holder.qty.getText().toString());
Log.d("plus_qnty==",""+plus_qnty);
//this is for check the quantity that useer enter is not more than qntty in stock
Log.d("whole_qnt_plus==",""+holder.whole_qntty.getText().toString());
whole_qnt= Integer.parseInt(holder.whole_qntty.getText().toString());
Log.d("whole_qnt_plus==",""+whole_qnt);
plus_qnty = plus_qnty + 1;
if(plus_qnty>whole_qnt)
{
Toast.makeText(getApplicationContext(), getResources().getString(R.string.outofstock), Toast.LENGTH_LONG).show();
}
else
{
for(int i=0;i<prodetailarray.size();i++){
Log.d("proarr==","getQntity=="+""+prodetailarray.get(i).getQntity()+"baseprice=="+prodetailarray.get(i).getBaseprice());
}
holder.qty.setText("" + plus_qnty);
//remove all the text except price (integer value)
String str=holder.sellprice.getText().toString().trim();
Double sell_price= Double.parseDouble(str.replaceAll(getResources().getString(R.string.Rs), ""));
holder.singlerowprice.setText(getResources().getString(R.string.Rs)+" " + Double.parseDouble(holder.qty.getText().toString()) * sell_price);
Bills_Activity.productlistarray.set(position,);
prodetailarray.get(in).setQntity(String.valueOf(plus_qnty));
prodetailarray.get(in).setBaseprice("" + Double.parseDouble(base_price) * plus_qnty);
for(int i=0;i<prodetailarray.size();i++)
{
Log.d("proarr=="," after_getQntity=="+""+prodetailarray.get(i).getQntity()+"baseprice=="+prodetailarray.get(i).getBaseprice());
}
}
/*for calculate value after change if any change*/
Double add = 0.0;
Double pr = 0.0;
for (int i = 0;i <listView.getCount(); i++)
{
View v = listView.getAdapter().getView(i,null,listView);
if(v==null)
{
return;
}
TextView tvquan = (TextView) v.findViewById(R.id.txtquntty_prodetail);
TextView tvprice = (TextView) v.findViewById(R.id.txtsinglerow_price);
String str_qun=tvquan.getText().toString().trim();
Double c = Double.parseDouble(str_qun.replaceAll("KG", ""));
add = c + add;
String str = tvprice.getText().toString().trim();
Double p = Double.parseDouble(str.replaceAll(getResources().getString(R.string.Rs), ""));
pr = pr + p;
Log.d("listitemcount==", "tvquan==" + tvquan.getText().toString() + "add==" + add + "price" + pr);
totalamount.setText(getResources().getString(R.string.Rs) +" "+ pr);
}
}
else
{
}
}
});
holder.imgminus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PLUSMINUS = 200;
int in=(Integer) view.getTag();
if(holder.qty.getText().toString().matches("[0-9]+"))
{
minus_qnty = Integer.parseInt(holder.qty.getText().toString());
if (minus_qnty == 1)
{
}
else
{
minus_qnty = minus_qnty - 1;
holder.qty.setText("" + minus_qnty);
String str=holder.sellprice.getText().toString();
Double sell_price= Double.parseDouble(str.replaceAll(getResources().getString(R.string.Rs), ""));
holder.singlerowprice.setText(getResources().getString(R.string.Rs)+" " + Double.parseDouble(holder.qty.getText().toString()) * sell_price);
prodetailarray.get(in).setQntity(String.valueOf(minus_qnty));
prodetailarray.get(in).setBaseprice("" + Double.parseDouble(base_price) * minus_qnty);
}
/*for calculate value after change if any change*/
Double add = 0.0;
Double pr = 0.0;
for (int i = 0; i < listView.getCount(); i++)
{
View v = listView.getAdapter().getView(i,null,listView);
if(v==null)
{
return;
}
TextView tvquan = (TextView) v.findViewById(R.id.txtquntty_prodetail);
TextView tvprice = (TextView) v.findViewById(R.id.txtsinglerow_price);
String str_qun=tvquan.getText().toString().trim();
Double c = Double.parseDouble(str_qun.replaceAll("KG", ""));
add = c + add;
String str = tvprice.getText().toString().trim();
Double p = Double.parseDouble(str.replaceAll(getResources().getString(R.string.Rs), ""));
pr = pr + p;
Log.d("listitemcount==", "tvquan==" + tvquan.getText().toString() + "add==" + add + "price" + pr);
totalamount.setText(getResources().getString(R.string.Rs) +" "+ pr);
// totalamount.setText(""+ pr);
}
}
else
{
}
}
});
if(!prodetailarray.get(listrowposition).getQntity().matches("[0-9]+"))
{
holder.qty.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
index = (Integer) view.getTag();
Log.d("indexval==","index==="+index);
final Dialog dialog_item = new Dialog(Bill_details.this);
dialog_item.setContentView(R.layout.add_item_dialog);
dialog_item.getWindow().setLayout(AppBarLayout.LayoutParams.FILL_PARENT, AppBarLayout.LayoutParams.WRAP_CONTENT);
edt_enter_qntty = (EditText) dialog_item.findViewById(R.id.txtqntty_item_dialog);
TextView ok = (TextView) dialog_item.findViewById(R.id.txtok_item_dialog);
ok.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
if(edt_enter_qntty.getText().toString().equals(""))
{
}
else
{
Double dialog_qntty= Double.parseDouble(edt_enter_qntty.getText().toString());
// PLUSMINUS = 100;
kg_qntty = dialog_qntty;
//this is for checking entered qunnt is not more than avail qntty
String whole_qnt=holder.whole_qntty.getText().toString();
String whole_qnt_replce= (whole_qnt.replaceAll("KG", ""));
whole_qnt_kg= Double.parseDouble(whole_qnt_replce);
Log.d("whole_qnt_kg==",""+whole_qnt_kg);
if(kg_qntty>whole_qnt_kg)
{
Toast.makeText(getApplicationContext(), getResources().getString(R.string.outofstock), Toast.LENGTH_LONG).show();
}
else
{
holder.qty.setText("" + kg_qntty+"KG");
prodetailarray.get(index).setQntity(String.valueOf(kg_qntty));
//remove all the text except price (integer value)
String str_qnty=prodetailarray.get(index).getQntity().toString();
Double qty= Double.parseDouble(str_qnty.replaceAll("KG", ""));
String str=holder.sellprice.getText().toString();
Double sell_price= Double.parseDouble(str.replaceAll(getResources().getString(R.string.Rs), ""));
Log.d("qty * sell_price==",""+qty+" "+sell_price);
holder.singlerowprice.setText(getResources().getString(R.string.Rs)+" " + qty * sell_price);
prodetailarray.get(index).setBaseprice("" + Double.parseDouble(base_price) * kg_qntty);
}
/*for calculate value after change if any change*/
Double add = 0.0;
Double pr = 0.0;
for (int i = 0;i < listView.getCount(); i++)
{
View v = listView.getAdapter().getView(i,null,listView);
if(v==null)
{
return;
}
TextView tvquan = (TextView) v.findViewById(R.id.txtquntty_prodetail);
TextView tvprice = (TextView) v.findViewById(R.id.txtsinglerow_price);
String str_quan=tvquan.getText().toString().trim();
Log.d("strqn",str_quan);
String c = (str_quan.replaceAll("KG", ""));
Log.d("strqn", "" + c);
add = Double.parseDouble(c) + add;
String str_pri=tvprice.getText().toString().trim();
Double p= Double.parseDouble(str_pri.replaceAll(getResources().getString(R.string.Rs), ""));
pr = pr + p;
Log.d("total_qntty", "tvquan==" + tvquan.getText().toString() + "add==" + add + "price" + pr);
// totalitem.setText("" + add+" Items");
totalamount.setText(getResources().getString(R.string.Rs) +" "+ pr);
}
}
dialog_item.dismiss();
}
});
dialog_item.show();
}
});
}
return convertView;
}

Using AlertDialog.Builder to build custom AlertDialog class

I have a Class FoodDialog that extends AlertDialog that I have customized to how I would like it to look.
I am now wanting to edit the positive/negative buttons using an AlertDialog.Builder, however, when I attempt to build an instance of FoodDialog using a builder, I am facing an 'Incompatible types' error where the builder is asking for AlertDialog instead I am providing it with an extension of AlertDialog - is there a way around this?
If not, is there a way I can edit the positive/negative buttons of my custom AlertDialog class FoodDialog?
Below is my FoodDialog class. The yes/no buttons I have there are ones I have created myself, but I would like the ones that are part of the AlertDialog.Builder to appear instead as these buttons get pushed out of sight when the soft keyboard appears:
public class FoodDialog extends AlertDialog implements OnClickListener {
private TextView foodNameTextView, foodDescTextView, foodPortionTextView, catTextView, qtyText, cal, fat, sFat, carb, sug, prot, salt, imageTxt,
measureText;
private EditText foodQty;
private ImageView foodImage;
private ImageButton yesBtn, noBtn;
private int foodID, totalCal;
private Bitmap image;
private String user, portionType, foodName, foodDesc, cat, totalCalString, totalFatString,
totalSFatString, totalCarbString, totalSugString, totalProtString, totalSaltString, portionBaseString;
private double totalFat, totalSFat, totalCarb, totalSug, totalProt, totalSalt, portionBase;
private Food food;
private Portion portion;
private Nutrients nutrients;
private PortionType pType;
private DBHandler db;
public FoodDialog(Context context){
super(context);
}
public FoodDialog(Context context, int foodID, String imgLocation, final String user) {
super(context, android.R.style.Theme_Holo_Light_Dialog);
this.setTitle("Confirm?");
setContentView(R.layout.dialog_layout);
this.foodID = foodID;
this.user = user;
db = new DBHandler(context);
food = db.getFoodByID(foodID, user);
portion = db.getPortionByFoodID(foodID);
nutrients = db.getNutrientsByFoodIDAndPortionType(foodID, portion.getPortionType());
pType = db.getPortionTypeByName(portion.getPortionType());
//getting object attributes
portionType = portion.getPortionType();
portionBase = portion.getPortionBase();
//food
foodName = food.getName();
foodDesc = food.getDesc();
cat = food.getCat();
//nutrients
totalCal = nutrients.getCal();
totalFat = nutrients.getFat();
totalSFat = nutrients.getSFat();
totalCarb = nutrients.getCarb();
totalSug = nutrients.getSug();
totalProt = nutrients.getProt();
totalSalt = nutrients.getSalt();
//converting to string
totalCalString = String.valueOf(totalCal);
if (totalFat % 1 == 0) {
totalFatString = String.format("%.0f", totalFat);
} else {
totalFatString = String.valueOf(totalFat);
}
if (totalSFat % 1 == 0) {
totalSFatString = String.format("%.0f", totalSFat);
} else {
totalSFatString = String.valueOf(totalSFat);
}
if (totalCarb % 1 == 0) {
totalCarbString = String.format("%.0f", totalCarb);
} else {
totalCarbString = String.valueOf(totalCarb);
}
if (totalSug % 1 == 0) {
totalSugString = String.format("%.0f", totalSug);
} else {
totalSugString = String.valueOf(totalSug);
}
if (totalProt % 1 == 0) {
totalProtString = String.format("%.0f", totalProt);
} else {
totalProtString = String.valueOf(totalProt);
}
if (totalSalt % 1 == 0) {
totalSaltString = String.format("%.0f", totalSalt);
} else {
totalSaltString = String.valueOf(totalSalt);
}
if (portionBase % 1 == 0) {
portionBaseString = String.format("%.0f", portionBase);
} else {
portionBaseString = String.valueOf(portionBase);
}
//textviews
foodNameTextView = (TextView) findViewById(R.id.dialogName);
foodNameTextView.setText(foodName);
foodDescTextView = (TextView) findViewById(R.id.dialogDesc);
foodDescTextView.setText(foodDesc);
foodPortionTextView = (TextView) findViewById(R.id.dialogPortion);
foodPortionTextView.setText("Values based per " + portionBase + " " + portionType);
catTextView = (TextView) findViewById(R.id.dialogCat);
catTextView.setText(cat);
measureText = (TextView) findViewById(R.id.dialogMeasure);
measureText.setText(portionType);
qtyText = (TextView) findViewById(R.id.dialogQtyText);
imageTxt = (TextView) findViewById(R.id.dialogImageText);
cal = (TextView) findViewById(R.id.dialogCal);
cal.setText(totalCalString);
fat = (TextView) findViewById(R.id.dialogFat);
fat.setText(totalFatString + "g");
sFat = (TextView) findViewById(R.id.dialogSFat);
sFat.setText(totalSFatString + "g");
carb = (TextView) findViewById(R.id.dialogCarb);
carb.setText(totalCarbString + "g");
sug = (TextView) findViewById(R.id.dialogSug);
sug.setText(totalSugString + "g");
prot = (TextView) findViewById(R.id.dialogProt);
prot.setText(totalProtString + "g");
salt = (TextView) findViewById(R.id.dialogSalt);
salt.setText(totalSaltString + "g");
//img
foodImage = (ImageView) findViewById(R.id.dialogImage);
imgLocation = food.getImgURL();
image = BitmapFactory.decodeFile(imgLocation);
foodImage.setImageBitmap(image);
if (imgLocation.equals("nourl")) {
imageTxt.setText("No Image");
}
//edit tex
foodQty = (EditText) findViewById(R.id.dialogQty);
//adjusting edittext
foodQty.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
foodQty.setFilters(new InputFilter[]{
new DigitsKeyListener(Boolean.FALSE, Boolean.TRUE) {
int beforeDecimal = 4, afterDecimal = 3;
#Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
String temp = foodQty.getText() + source.toString();
if (temp.equals(".")) {
return "0.";
} else if (temp.toString().indexOf(".") == -1) {
// no decimal point placed yet
if (temp.length() > beforeDecimal) {
return "";
}
} else {
temp = temp.substring(temp.indexOf(".") + 1);
if (temp.length() > afterDecimal) {
return "";
}
}
return super.filter(source, start, end, dest, dstart, dend);
}
}
});
foodQty.setText(portionBaseString);
//btns
yesBtn = (ImageButton) findViewById(R.id.yesBtn);
noBtn = (ImageButton) findViewById(R.id.noBtn);
Bitmap tick = BitmapFactory.decodeResource(context.getResources(),
R.drawable.png_tick);
Bitmap cross = BitmapFactory.decodeResource(context.getResources(),
R.drawable.png_cross);
yesBtn.setImageBitmap(tick);
noBtn.setImageBitmap(cross);
yesBtn.setOnClickListener(this);
noBtn.setOnClickListener(this);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
#Override
public void onClick(View v) {
if (v == yesBtn) {
SimpleDateFormat currentDate = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat currentTime = new SimpleDateFormat("HH:mm:ss");
String date = currentDate.format(new Date());
String time = currentTime.format(new Date());
double qty = 0;
//get quantity amount
// if (portionMeasure.equals("singles")) {
//qty = foodQty.getValue();
// } else {
if (foodQty.getText().length() != 0) {
qty = Double.valueOf(foodQty.getText().toString());
} else {
qty = 0;
}
// }
if (qty == 0 || String.valueOf(qty) == "") {
Toast.makeText(getContext(), "Please enter an amount", Toast.LENGTH_SHORT).show();
} else {
//create new intake
Intake intake = new Intake(0, foodID, portionType, qty, date, time);
//record it and increment food used value
db.recordIntake(intake, user);
db.incrementUsedCount(intake.getFoodID(), 1);
db.close();
cancel();
Toast.makeText(getContext(), foodName + " recorded", Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("What next?");
builder.setItems(new CharSequence[]
{"Record another food intake..", "Main Menu..", "View Stats.."},
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
switch (which) {
case 0:
cancel();
break;
case 1:
Intent main = new Intent(getContext(), ProfileActivity.class);
getContext().startActivity(main);
break;
case 2:
Intent stats = new Intent(getContext(), StatsActivity.class);
getContext().startActivity(stats);
break;
}
}
});
AlertDialog choose = builder.create();
choose.show();
}
} else if (v == noBtn) {
cancel();
}
}
}
You can catch your buttons click listener as follows:
yesBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//yes button click code here
}
});
noBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//no button click code here
}
});
You can use the logcat to see if your listener are being fired.

Cart item Count increment/decrement & Sum of cart price

I need to increment / decrement of cart product.Valuse are increased / decreased.But from here I need to get Total sum of my all cart product. Got some calculation mistakes.So please help me to fix it.
cartList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(final AdapterView<?> parent, View itemView, final int position, final long rowId) {
txt_cartProduct = (TextView) itemView.findViewById(R.id.cartProduct);
txt_cartQuantity = (TextView) itemView.findViewById(R.id.cartQuantity);
txt_cartPrice = (TextView) itemView.findViewById(R.id.cartPrice);
txt_cartPriceDum = (TextView) itemView.findViewById(R.id.cartPriceDum);
txt_cartCount = (TextView) itemView.findViewById(R.id.cartCount);
img_ivDecrease = (ImageView) itemView.findViewById(R.id.ivDecrease);
img_ivIncrease = (ImageView) itemView.findViewById(R.id.ivIncrease);
but_addTowish = (Button) itemView.findViewById(R.id.addTowish);
but_remove = (Button) itemView.findViewById(R.id.remove);
img_ivIncrease.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
strPrice = txt_cartPrice.getText().toString();
price = Integer.parseInt(strPrice);
int counter = 0;
try {
counter = Integer.parseInt(txt_cartCount.getText().toString());
} catch (NumberFormatException e) {
e.printStackTrace();
}
counter++;
if (counter > 0) {
txt_cartCount.setText(Integer.toString(counter));
txt_cartPrice.setVisibility(View.GONE);
txt_cartPriceDum.setVisibility(View.VISIBLE);
quantity = txt_cartCount.getText().toString();
total = (Integer.parseInt(quantity)) * (price);
netA = String.valueOf(total);
sum += price;
if (sum == 0) {
netAmount = Integer.parseInt(txt_cartPriceDum.getText().toString());
}
netAmount = sum;
Log.e("Sum is", String.valueOf(sum));
txt_cartPriceDum.setText(String.valueOf(total));
cartCount = Integer.parseInt(quantity);
Toast.makeText(context, "netAmount" + netAmount + "\n" + "Total" + total, Toast.LENGTH_SHORT).show();
if (counter == 1) {
cartPrice = price;
cartSum = sum;
}
if (counter == 0) {
cartPrice = 0;
cartSum = 0;
cartCount = 0;
Toast.makeText(context, "Minimum Item is 1", Toast.LENGTH_SHORT).show();
}
int count_check = 1;
if (count_check >= position) {
count_check++;
} else if (count_check == 0) {
Toast.makeText(context, "Minimum Item is 1", Toast.LENGTH_SHORT).show();
}
}
}
});
img_ivDecrease.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
strPrice = txt_cartPrice.getText().toString();
price = Integer.parseInt(strPrice);
int counter = 0;
try {
counter = Integer.parseInt(txt_cartCount.getText().toString());
} catch (NumberFormatException e) {
e.printStackTrace();
}
counter--;
if (counter > 0) {
txt_cartCount.setText(Integer.toString(counter));
txt_cartPrice.setVisibility(View.GONE);
txt_cartPriceDum.setVisibility(View.VISIBLE);
quantity = txt_cartCount.getText().toString();
total = (Integer.parseInt(quantity)) * (price);
netA = String.valueOf(total);
sum -= price;
if (sum == 0) {
netAmount = Integer.parseInt(txt_cartPriceDum.getText().toString());
}
Log.e("Sum - is", String.valueOf(sum));
txt_cartPriceDum.setText(String.valueOf(total));
cartCount = Integer.parseInt(quantity);
Toast.makeText(context, "netAmount" + netAmount + "\n" + "Total" + total, Toast.LENGTH_SHORT).show();
if (counter == 1) {
cartPrice = price;
cartSum = sum;
}
if (counter == 0) {
cartPrice = 0;
cartSum = 0;
cartCount = 0;
}
}
}
});
}
});

Cart Count increment & decrement while clicking button

I populates cart items into the list using cursor adapter. My ListView item has
Price TextView
Product Name TextView
Count TextView
Increment Button
Decrement Buttton
When I click the increment / decrement button the value is incremented/ decremented in a certain condition of AFTER TOUCHING THE LISTVIEW ROW,otherwise the count is not changed.
In some case, if I click the 1st row increment button ,item count is incremented in 2nd row.
NOTE:
1. I am populating List using Cursor Adapter.
2. Using Increment & Decrement clicklistener in activity class.
Here is my code:
cartList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(final AdapterView<?> parent, View itemView, final int position, final long rowId) {
txt_cartProduct = (TextView) itemView.findViewById(R.id.cartProduct);
txt_cartQuantity = (TextView) itemView.findViewById(R.id.cartQuantity);
txt_cartPrice = (TextView) itemView.findViewById(R.id.cartPrice);
txt_cartPriceDum = (TextView) itemView.findViewById(R.id.cartPriceDum);
txt_cartCount = (TextView) itemView.findViewById(R.id.cartCount);
img_ivDecrease = (ImageView) itemView.findViewById(R.id.ivDecrease);
img_ivIncrease = (ImageView) itemView.findViewById(R.id.ivIncrease);
but_addTowish = (Button) itemView.findViewById(R.id.addTowish);
but_remove = (Button) itemView.findViewById(R.id.remove);
img_ivIncrease.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
strPrice = txt_cartPrice.getText().toString();
price = Integer.parseInt(strPrice);
int counter = 0;
try {
counter = Integer.parseInt(txt_cartCount.getText().toString());
} catch (NumberFormatException e) {
e.printStackTrace();
}
counter++;
if (counter > 0) {
txt_cartCount.setText(Integer.toString(counter));
txt_cartPrice.setVisibility(View.GONE);
txt_cartPriceDum.setVisibility(View.VISIBLE);
quantity = txt_cartCount.getText().toString();
total = (Integer.parseInt(quantity)) * (price);
netA = String.valueOf(total);
sum += price;
if (sum == 0) {
netAmount = Integer.parseInt(txt_cartPriceDum.getText().toString());
}
netAmount = sum;
Log.e("Sum is", String.valueOf(sum));
txt_cartPriceDum.setText(String.valueOf(total));
cartCount = Integer.parseInt(quantity);
Toast.makeText(context, "netAmount" + netAmount + "\n" + "Total" + total, Toast.LENGTH_SHORT).show();
if (counter == 1) {
cartPrice = price;
cartSum = sum;
}
if (counter == 0) {
cartPrice = 0;
cartSum = 0;
cartCount = 0;
Toast.makeText(context, "Minimum Item is 1", Toast.LENGTH_SHORT).show();
}
int count_check = 1;
if (count_check >= position) {
count_check++;
} else if (count_check == 0) {
Toast.makeText(context, "Minimum Item is 1", Toast.LENGTH_SHORT).show();
}
}
}
});
img_ivDecrease.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
strPrice = txt_cartPrice.getText().toString();
price = Integer.parseInt(strPrice);
int counter = 0;
try {
counter = Integer.parseInt(txt_cartCount.getText().toString());
} catch (NumberFormatException e) {
e.printStackTrace();
}
counter--;
if (counter > 0) {
txt_cartCount.setText(Integer.toString(counter));
txt_cartPrice.setVisibility(View.GONE);
txt_cartPriceDum.setVisibility(View.VISIBLE);
quantity = txt_cartCount.getText().toString();
total = (Integer.parseInt(quantity)) * (price);
netA = String.valueOf(total);
sum -= price;
if (sum == 0) {
netAmount = Integer.parseInt(txt_cartPriceDum.getText().toString());
}
Log.e("Sum - is", String.valueOf(sum));
txt_cartPriceDum.setText(String.valueOf(total));
cartCount = Integer.parseInt(quantity);
Toast.makeText(context, "netAmount" + netAmount + "\n" + "Total" + total, Toast.LENGTH_SHORT).show();
if (counter == 1) {
cartPrice = price;
cartSum = sum;
}
if (counter == 0) {
cartPrice = 0;
cartSum = 0;
cartCount = 0;
}
}
}
});
}
});
You can use setTag() and getTag() methods for saving the counter value in each list item.
Refer this answer for more details.

Store EditText values in android

I developed an android application... in which... users name last name date of birth, mobile number, email id everything is collected... I wanted to store these details... and when user wants these data's back... a button is there called recall... when user press recall button .. the place where this data's is stored can be displayed.. and he can select his name from this names... any one know how this data can be saved....I am giving the code...here ...anyone pls.. help.. the code is working.... no problem in it...
public void onDateChanged(DatePicker arg0, int arg1, int arg2, int arg3)
{
String date=Integer.toString(arg3);
String month=Integer.toString(arg2);
String year=Integer.toString(arg1);
tv1.setText(date+ month+1 +year);
tv2.setText(month+1 + year);
tv3.setText(year);
tv4.setText(month+1);
tv5.setText(date);
tv6.setText(date + year);
}
});}
public void sum(View v)
{
long sum1=0;
long sum2=0;
long sum3=0;
long sum4=0;
long sum5=0;
long sum6=0;
long sum7=0;
long sum8=0;
long sum=0;
long sum11=0;
long sum12=0;
long sum13=0;
long sum14=0;
EditText et1 = (EditText) findViewById (R.id.editText1);
EditText et2 = (EditText) findViewById (R.id.editText2);
EditText et3 = (EditText) findViewById (R.id.editText3);
EditText et4 = (EditText) findViewById (R.id.editText4);
final DatePicker date = (DatePicker) findViewById (R.id.datePicker1);
final TextView tv1 = (TextView) findViewById (R.id.textView88);
final TextView tv2 = (TextView) findViewById (R.id.textView89);
final TextView tv3 = (TextView) findViewById (R.id.textView90);
final TextView tv4 = (TextView) findViewById (R.id.textView91);
final TextView tv5 = (TextView) findViewById (R.id.textView92);
final TextView tv6 = (TextView) findViewById (R.id.textView93);
final TextView tv7 = (TextView) findViewById (R.id.textView94);
sum1=getSum(et1.getText().toString() + et2.getText().toString() + et3.getText().toString() + et4.getText().toString());
sum2=getSum1(et1.getText().toString() + et2.getText().toString() + et3.getText().toString() + et4.getText().toString());
sum3=getSum2(tv1.getText().toString());
/*sum4=getSum3(tv2.getText().toString());*/
sum5=getSum4(tv3.getText().toString());
sum6=getSum5(tv4.getText().toString());
sum8=getSum7(tv5.getText().toString());
String yearf = tv3.getText().toString();
int yearb = Integer.valueOf(yearf);
int val = 2013 - yearb;
long val1 = sum3;
/*int val2 = Integer.valueOf(val1);*/
long val3 = 36 - val1;
int val4 = 9;
long val5 = val3 + val4;
long val6 = val5 + val4;
long val7 = val6 + 50;
sum = getSum3(tv2.getText().toString()) + getSum8(tv6.getText().toString());
if (val <= val3)
{
sum4=getSum3(tv2.getText().toString());
}
else if ((val <= val5) && (val > val3))
{
sum4=getSum8(tv6.getText().toString());
}
else if ((val<=val6) && (val > val5) && (val > val3))
{
/*sum = getSum3(tv2.getText().toString()) + getSum8(tv6.getText().toString());*/
sum4=getSum9(Integer.toString((int) sum));
}
else
{
sum4=getSum9(Integer.toString((int) sum));
}
if (val <= val3)
{
if (sum5>sum6)
{
sum7 = (sum5 - sum6);
}
else
{
sum7 = (sum6 - sum5);
}
}
else if ((val <= val5) && (val > val3))
{
if(sum5>sum8)
{
sum7=(sum5 - sum8);
}
else
{
sum7 = (sum8 - sum5);
}
}
else if ((val<=val6) && (val > val5) && (val > val3))
{
sum11 = (sum5 - sum8);
sum12 = (sum8 - sum5);
sum13 = (sum6 - sum5);
sum14 = (sum5 - sum6);
if ((sum11 > sum12) && (sum13 > sum14))
{
if (sum11 > sum13)
{
sum7 = sum11 - sum13;
}
else
{
sum7 = sum13 - sum11;
}
}
else if ((sum12 > sum11) && (sum13 > sum14))
{
if (sum12 > sum13)
{
sum7 = sum12 - sum13;
}
else
{
sum7 = sum13 - sum12;
}
}
else if ((sum11 > sum12) && (sum14 > sum13))
{
if (sum11 > sum14)
{
sum7 = sum11 - sum14;
}
else
{
sum7 = sum14 - sum11;
}
}
else if ((sum12 > sum11) && (sum14 > sum13))
{
if (sum12 > sum14)
{
sum7 = sum12 - sum14;
}
else
{
sum7 = sum14 - sum12;
}
}
}
else
{
sum11 = (sum5 - sum8);
sum12 = (sum8 - sum5);
sum13 = (sum6 - sum5);
sum14 = (sum5 - sum6);
if ((sum11 > sum12) && (sum13 > sum14))
{
if (sum11 > sum13)
{
sum7 = sum11 - sum13;
}
else
{
sum7 = sum13 - sum11;
}
}
else if ((sum12 > sum11) && (sum13 > sum14))
{
if (sum12 > sum13)
{
sum7 = sum12 - sum13;
}
else
{
sum7 = sum13 - sum12;
}
}
else if ((sum11 > sum12) && (sum14 > sum13))
{
if (sum11 > sum14)
{
sum7 = sum11 - sum14;
}
else
{
sum7 = sum14 - sum11;
}
}
else if ((sum12 > sum11) && (sum14 > sum13))
{
if (sum12 > sum14)
{
sum7 = sum12 - sum14;
}
else
{
sum7 = sum14 - sum12;
}
}
}
String edtxt1 = et1.getText().toString();
String edtxt4 = et4.getText().toString();
if(edtxt1.equals(""))
{
Toast.makeText(getApplicationContext(),"First Name should not be left blank, Please enter your First Name ", Toast.LENGTH_LONG).show();
}
else if(edtxt4.equals(""))
{
Toast.makeText(getApplicationContext(),"Last Name should not be left blank, Please enter your Last Name ", Toast.LENGTH_LONG).show();
}
else{
Intent i = new Intent(this, result.class);
i.putExtra("name", sum1 + "");
i.putExtra("name1", sum2 + "");
i.putExtra("name2", sum3 + "");
i.putExtra("name3", sum4 + "");
i.putExtra("name4", sum7 + "");
startActivity(i);
}
You should have a look at Storage Options. But if this isn't for a lot of users you can use SharedPreferences. That link has a good example of getting started using them.
If you are going to have multiple users with that much different pieces of data then you may want to look at using SQLite DB. If you aren't used to using SQLite Db then this tutorial might be a good one to get you started.
You also could use the other storage options in the link I provided but a DB would probably be the best with storing a lot of different information for different users.
Have you considered using SharedPreferences?
You can store any variable with the SharedPreferences and it's super easy!
Here's a quick tutorial
SharedPreferences prefs = this.getSharedPreferences(
getApplicationContext(), Context.MODE_PRIVATE);
To read preferences:
String dateTimeKey = "com.example.app.datetime";
// use a default value using new Date()
long l = prefs.getLong(dateTimeKey, new Date().getTime());
To edit and save preferences
Date dt = getSomeDate();
prefs.edit().putLong(dateTimeKey, dt.getTime()).commit();

Categories

Resources