Unable to calculate proper value from each row in ListView - android

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

Related

How to get Id of ChildView Items of ExpandableListView in Android

I'm using ExpandapbeListView its work perfect all expand and collapse but I just want to update only particular child item here when I click on plus value should be update in EditText and I don't want to use notifyDatasetChanged() Just want to update only particular field and also i should able to update EditText directly. It can have multiple child item.
Ex: Colgate may have multiple child Item like 100gm,200gm,300gm
So when I click on Plus It should able to get Id of EditText and Update Quantity
#Override
public View getChildView(int productPos, int weightPos, boolean b, View plusMinusView, ViewGroup viewGroup) {
if (plusMinusView == null) {
csHolder = new ChildHolder();
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
plusMinusView = inflater.inflate(R.layout.orders_products_plus_minus_design, null);
}
csHolder.minQty = (TextView) plusMinusView.findViewById(R.id.orderProducts_submitMinQtyTxt);
csHolder.totalPriceTxt = (TextView) plusMinusView.findViewById(R.id.orderProducts_submitTotalPriceTxt);
csHolder.weightTxt = (TextView) plusMinusView.findViewById(R.id.orderProducts_submitWeightTxt);
csHolder.unitPrice = (TextView) plusMinusView.findViewById(R.id.orderProducts_submitUnitPriceTxt);
csHolder.totalTaxTxt = (TextView) plusMinusView.findViewById(R.id.orderProducts_submitTotalTaxTxt);
csHolder.netPriceTxt = (TextView) plusMinusView.findViewById(R.id.orderProducts_submitNetPriceTxt);
csHolder.orderQtyEdit = (CustomEditText) plusMinusView.findViewById(R.id.orderProducts_orderQtyTxt);
csHolder.plusTxt = (RelativeLayout) plusMinusView.findViewById(R.id.orderProducts_plusTxtLay);
csHolder.minusTxt = (RelativeLayout) plusMinusView.findViewById(R.id.orderProducts_minusTxtLay);
plusMinusView.setTag(productPos + "_" + weightPos);
String tag = "weight_" + productPos + "_" + weightPos;
csHolder.orderQtyEdit.setTag(tag);
csHolder.plusTxt.setTag(tag);
csHolder.minusTxt.setTag(tag);
//csHolder.plusTxt.setOnClickListener(weightClick);
//csHolder.minusTxt.setOnClickListener(weightClick);
final View finalPlusMinusView = plusMinusView;
csHolder.plusTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
csHolder.orderQtyEdit.setText("5");
}
});
if(!csStatus.equalsIgnoreCase("DRAFT")) {
csHolder.plusTxt.setVisibility(View.GONE);
csHolder.minusTxt.setVisibility(View.GONE);
csHolder.orderQtyEdit.setEnabled(false);
}
else
{
csHolder.plusTxt.setVisibility(View.VISIBLE);
csHolder.minusTxt.setVisibility(View.VISIBLE);
csHolder.orderQtyEdit.setEnabled(true);
}
try
{
csHolder.orderQtyEdit.addTextChangedListener(new MyTextWatcher(csHolder.orderQtyEdit));
JSONObject weighObj = csMainArr.getJSONObject(productPos).getJSONArray("weights").getJSONObject(weightPos);
if(weighObj.has("unit"))
{
csHolder.weightTxt.setText(weighObj.getString("unit"));
}
if (weighObj.has("order_qty") == false)
weighObj.put("order_qty", "0");
if (weighObj.has("sell_price") == false)
weighObj.put("sell_price", "0");
if (weighObj.has("order_qty") == false)
{
csHolder.totalPriceTxt.setText("0.00");
csHolder.totalTaxTxt.setText("0.00");
csHolder.netPriceTxt.setText("0.00");
}
else {
int qty = 0;
double sellPrice = 0.00;
float tax = 0f;
double totalPrice = 0.00;
double totalTax = 0.00;
double netPrice = 0.00;
if(csStatus.equals("DRAFT")) {
qty = Integer.parseInt(weighObj.getString("order_qty"));
sellPrice = weighObj.getDouble("sell_price");
}
else
{
qty = Integer.parseInt(weighObj.has("qty") ? weighObj.getString("qty"):"0");
sellPrice = weighObj.getDouble("unit_price");
}
tax = Float.parseFloat(csMainArr.getJSONObject(productPos).has("tax") ? csMainArr.getJSONObject(productPos).getString("tax") : "0");
totalPrice = qty*sellPrice;
totalTax = (totalPrice*tax)/100;
netPrice = totalPrice + totalTax;
csHolder.totalPriceTxt.setText(getResources().getString(R.string.Rs)+" "+String.format("%.2f",totalPrice));
csHolder.totalTaxTxt.setText(getResources().getString(R.string.Rs)+" "+String.format("%.2f",totalTax));
csHolder.netPriceTxt.setText(getResources().getString(R.string.Rs)+" "+String.format("%.2f",netPrice));
}
csHolder.orderQtyEdit.isValueChangeMySelf = true;
if(csStatus.equals("DRAFT"))
csHolder.orderQtyEdit.setText(weighObj.getString("order_qty"));
else
csHolder.orderQtyEdit.setText(weighObj.has("qty")?weighObj.getString("qty"):"0");
/*if(weightPos == iClickedItem) {
csHolder.orderQtyEdit.requestFocus();
}
else {
csHolder.orderQtyEdit.clearFocus();
}*/
if(csStatus.equals("DRAFT"))
csHolder.unitPrice.setText(getResources().getString(R.string.Rs)+" "+String.format("%.2f",weighObj.has("sell_price")?weighObj.getDouble("sell_price") : 0.00));
else
csHolder.unitPrice.setText(getResources().getString(R.string.Rs)+" "+String.format("%.2f",weighObj.has("unit_price")?weighObj.getDouble("unit_price") : 0.00));
csHolder.minQty.setText(weighObj.has("min_order_qty")?weighObj.getString("min_order_qty"):"0");
//View line = (View) plusMinusView.findViewById(R.id.orderProducts_submitDividerLine);
//if(isLast)
// line.setVisibility(View.GONE);
}
catch(Exception e)
{
}
return plusMinusView;
}

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

getting duplicate objects in json in android

I am putting objects in Json but I want only 1 key to be update.Right now it is creating new object every time function is called I want only quantity should get updated if it is already there in json. Please help
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {
private List<ProductBean> productList ;
static int qtyValues;
static int itemRem = -1;
static int addItem = 1;
static boolean isSKU1;
static boolean isSKU2;
static boolean isSKU;
static int value;
static int totalAmount = 0;
static int totalItems = 0;
SharedPreferences myPrefs;
SharedPreferences.Editor editor;
private final Activity context;
private AQuery aq;
JSONObject jsonObject = new JSONObject();
JSONArray obj = new JSONArray();
ArrayList list = new ArrayList();
public ProductAdapter(Activity context,List<ProductBean> productList){
this.productList = productList;
this.context = context;
isSKU = true;
}
#Override
public int getItemCount() {
return productList.size();
}
#Override
public void onBindViewHolder(final ProductViewHolder holder, final int position) {
myPrefs = context.getSharedPreferences("qty", Context.MODE_PRIVATE);
editor = myPrefs.edit();
aq = new AQuery(context.getApplicationContext());
final ProductBean productBean = productList.get(position);
holder.extratxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
holder.extratxt.setTextColor(Color.BLACK);
holder.extratxt2.setTextColor(Color.GRAY);
holder.extratxt3.setTextColor(Color.GRAY);
holder.extratxt1.setText(productBean.getProductPrice() + "");
isSKU = true;
isSKU1 = false;
isSKU2 = false;
holder.qtyCounter.setText(myPrefs.getInt("product"+productBean.getProductId()+productBean.getSkuId1(),0)+"");
}
});
holder.extratxt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
holder.extratxt.setTextColor(Color.GRAY);
holder.extratxt2.setTextColor(Color.BLACK);
holder.extratxt3.setTextColor(Color.GRAY);
holder.extratxt1.setText(productBean.getProductPrice2() + "");
isSKU1 = true;
isSKU = false;
isSKU2 = false;
holder.qtyCounter.setText(myPrefs.getInt("product"+productBean.getProductId()+productBean.getSkuId2(),0)+"");
}
});
holder.extratxt3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
holder.extratxt.setTextColor(Color.GRAY);
holder.extratxt2.setTextColor(Color.GRAY);
holder.extratxt3.setTextColor(Color.BLACK);
holder.extratxt1.setText(productBean.getProductPrice3() + "");
isSKU2 = true;
isSKU = false;
isSKU1 = false;
holder.qtyCounter.setText(myPrefs.getInt("product"+productBean.getProductId()+productBean.getSkuId3(),0)+"");
}
});
holder.decreaseQty.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
if(isSKU1 == true) {
if(myPrefs.getInt("product"+productBean.getProductId()+productBean.getSkuId2(),0)>0) {
qtyValues = myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId2(), 0);
qtyValues = qtyValues - 1;
editor.putInt("product" + productBean.getProductId() + productBean.getSkuId2(), qtyValues);
editor.commit();
holder.qtyCounter.setText(myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId2(), 0) + "");
value = 0 - productBean.getProductPrice2();
add(itemRem);
amount(value);
}
}
else if (isSKU2 == true){
if(myPrefs.getInt("product"+productBean.getProductId()+productBean.getSkuId3(),0)>0) {
qtyValues = myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId3(), 0);
qtyValues = qtyValues - 1;
editor.putInt("product" + productBean.getProductId() + productBean.getSkuId3(), qtyValues);
editor.commit();
holder.qtyCounter.setText(myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId3(), 0) + "");
value = 0 - productBean.getProductPrice3();
add(itemRem);
amount(value);
}
}
else{
if(myPrefs.getInt("product"+productBean.getProductId()+productBean.getSkuId1(),0)>0) {
qtyValues = myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId1(), 0);
qtyValues = qtyValues - 1;
editor.putInt("product" + productBean.getProductId() + productBean.getSkuId1(), qtyValues);
editor.commit();
holder.qtyCounter.setText(myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId1(), 0) + "");
value = 0 - productBean.getProductPrice();
add(itemRem);
amount(value);
}
}
}
});
holder.increaseQty.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
if(isSKU1 == true) {
if(myPrefs.getInt("product"+productBean.getProductId() + productBean.getSkuId2(),0)<10) {
qtyValues = myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId2(), 0);
qtyValues = qtyValues + 1;
editor.putInt("product" + productBean.getProductId() + productBean.getSkuId2(), qtyValues);
editor.commit();
holder.qtyCounter.setText(myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId2(), 0) + "");
value = productBean.getProductPrice2();
add(addItem);
amount(value);
setCartItem(productBean.getSkuId2(),myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId2(), 0),productBean.getProductId(),productBean.getDescription() , productBean.getProductName() , productBean.getProductPrice2());
}
}
else if (isSKU2 == true){
if(myPrefs.getInt("product"+productBean.getProductId() + productBean.getSkuId3(),0)<10) {
qtyValues = myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId3(), 0);
qtyValues = qtyValues + 1;
editor.putInt("product" + productBean.getProductId() + productBean.getSkuId3(), qtyValues);
editor.commit();
holder.qtyCounter.setText(myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId3(), 0) + "");
value = productBean.getProductPrice3();
add(addItem);
amount(value);
setCartItem(productBean.getSkuId3(),myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId3(), 0),productBean.getProductId(),productBean.getDescription() , productBean.getProductName() , productBean.getProductPrice3());
}
}
else{
if(myPrefs.getInt("product"+productBean.getProductId() + productBean.getSkuId1(),0)<10) {
qtyValues = myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId1(), 0);
qtyValues = qtyValues + 1;
editor.putInt("product" + productBean.getProductId() + productBean.getSkuId1(), qtyValues);
editor.commit();
holder.qtyCounter.setText(myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId1(), 0) + "");
value = productBean.getProductPrice();
add(addItem);
amount(value);
setCartItem(productBean.getSkuId1(),myPrefs.getInt("product" + productBean.getProductId() + productBean.getSkuId1(), 0),productBean.getProductId(),productBean.getDescription() , productBean.getProductName() , productBean.getProductPrice());
}
}
}
});
holder.txtTitle.setText(productBean.getProductName());
holder.extratxt.setText(productBean.getSkuId1() + "");
holder.extratxt2.setText(productBean.getSkuId2() + "");
holder.extratxt3.setText(productBean.getSkuId3() + "");
holder.extratxt1.setText(productBean.getProductPrice() + "");
holder.imageView1.setImageResource(R.drawable.rupee);
if(myPrefs.getInt("totalItems", 0)>0){
BeveragesMainActivity.cartTotal.setVisibility(View.VISIBLE);
BeveragesMainActivity.totalPrice.setText("Rs " + myPrefs.getInt("totalAmount", 0) + "");
}
BeveragesMainActivity.cartTotal.setText(myPrefs.getInt("totalItems", 0) + "");
holder.qtyCounter.setText(myPrefs.getInt("product"+productBean.getProductId()+productBean.getSkuId1(),0) + "");
}
#Override
public ProductViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View itemView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.list_beverages_item, viewGroup, false);
return new ProductViewHolder(itemView);
}
public class ProductViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView txtTitle;
TextView extratxt;
TextView extratxt2;
TextView extratxt3;
ImageView imageView1;
TextView extratxt1;
TextView qtyCounter;
ImageView decreaseQty;
ImageView increaseQty;
/**
* Instantiates a new card view holder.
*
* #param v the v
*/
public ProductViewHolder(View v) {
super(v);
imageView = (ImageView) v.findViewById(R.id.drinks);
txtTitle = (TextView) v.findViewById(R.id.drinkTitle);
extratxt = (TextView) v.findViewById(R.id.drinkQuantity);
extratxt2 = (TextView) v.findViewById(R.id.drinkQuantity1);
extratxt3 = (TextView) v.findViewById(R.id.drinkQuantity2);
imageView1 = (ImageView) v.findViewById(R.id.rupee);
extratxt1 = (TextView) v.findViewById(R.id.drinkPrice);
qtyCounter = (TextView) v.findViewById(R.id.drinkCounter);
decreaseQty = (ImageView) v.findViewById(R.id.decreaseCounter);
increaseQty = (ImageView) v.findViewById(R.id.increaseCounter);
extratxt.setTextColor(Color.BLACK);
}
}
public void setCartItem(final int skuId ,final int qty , final long productId , String description , String productName , int price) {
try {
jsonObject.put("skuId",skuId);
jsonObject.put("quantity",qty);
jsonObject.put("productId",productId);
jsonObject.put("price",price);
jsonObject.put("productName",productName);
jsonObject.put("description",description);
obj.put(jsonObject);
editor.putString("data",obj.toString());
editor.commit();
}catch (JSONException e){
e.printStackTrace();
}
}
public void add(int items){
totalItems = totalItems + items;
editor.putInt("totalItems",totalItems);
editor.commit();
if(myPrefs.getInt("totalItems", 0)>0){
BeveragesMainActivity.cartTotal.setVisibility(View.VISIBLE);
}
else{
BeveragesMainActivity.cartTotal.setVisibility(View.INVISIBLE);
}
BeveragesMainActivity.cartTotal.setText(myPrefs.getInt("totalItems", 0) + "");
}
public void amount(int price){
totalAmount = totalAmount+price;
editor.putInt("totalAmount", totalAmount);
editor.commit();
BeveragesMainActivity.totalPrice.setText("Rs " + myPrefs.getInt("totalAmount", 0) + "");
}
}
Use this code inside setCartItem(...) method
// loop through each object
for (int i = 0; i < obj.length(); i++) {
try {
JSONObject item = obj.getJSONObject(i);
int itemId = item.getInt("skuId ");
if (itemId == skuId) {// object already exists update quantity
item.put("quantity", qty);
// update other keys if you want to
// do other stuff
return;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
JSONObject jsonObject = new JSONObject();
// If skudId does not exits in the array list add new one
try {
jsonObject.put("skuId", skuId);
jsonObject.put("quantity", qty);
jsonObject.put("productId", productId);
jsonObject.put("price", price);
jsonObject.put("productName", productName);
jsonObject.put("description", description);
obj.put(jsonObject);
editor.putString("data", obj.toString());
editor.commit();
} catch (JSONException e) {
e.printStackTrace();
}

Android data storage and retrieving

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.

Change two textViews inside a listview

Let's say, I have two textviews, TextView A and B.
when I click on A, I need to change both textviews;
When I click on B, I need it to do something else.
I used the setTag(key,A); and setTag(key,B); The textView A changed correctly but the textView B is null. And when I click on B it works.
Here is my code :
public class CustomeAdapterHowComment extends ArrayAdapter<ItemsHowComment> {
Context context;
ItemsHowComment items;
List<LikersComment> likeItems;
List<ItemsHowComment> Items1;
Bitmap bitmap;
String imageUser = "";
String filePath_Image = "/Pictures/jehad/joj/";
String user_id = "1510";
String secret_id = "789654120";
View v;
String onclick = " ";
int x;
String table = "10";
String target = "";
String type = "";
DataBaseHandler dbobj;
public static String server_List_of_Comments = "https://www.ashabe.comt";
public static String server_Comment_like = "https://www.ashabe.com/";
public static String server_Comment_like_remove = "https://www.ashabe.com/";
public static String server_Comment_Delte = "https://www.ashabe.com/";
public static String server_Comment_likers = "https://www.ashabe.com/";
String response;
public CustomeAdapterHowComment(Context context, int textViewResourceId,
List<ItemsHowComment> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
this.context = context;
}
public View getView(final int position, View convertView, ViewGroup parent) {
Log.d("View ", "View>>>>>>>>");
commentholder h = null;
v = convertView;
items = getItem(position);
LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
v = vi.inflate(R.layout.how_comment_list, null);
h = new commentholder();
h.img = (ImageView) v.findViewById(R.id.CommentImg);
h.name = (TextView) v.findViewById(R.id.CommentName);
h.commentBody = (TextView) v.findViewById(R.id.CommentBody);
h.like = (TextView) v.findViewById(R.id.CommentLike);
h.numoflike = (TextView) v.findViewById(R.id.CommentNumOfLike);
v.setTag(h);
} else {
h = (commentholder) v.getTag();
}
imageUser = items.getUserId() + items.getRand();
boolean flag_ImagePath = Methods.checkIfImage_DirExists(filePath_Image
+ imageUser);
if (flag_ImagePath == true) {
Log.v("flag_ImagePath", String.valueOf(flag_ImagePath));
bitmap = Methods.displayBitmapImage(imageUser);
h.img.setImageBitmap(bitmap);
}
h.commentBody.setText(items.getCommentBody());
h.name.setText(items.getName());
if (items.getiLike().equals("0")) {
h.like.setText("Like");
}
if (items.getiLike().equals("1")) {
h.like.setText("Unlike");
}
if (items.getNumOfLike().equals("0")) {
h.numoflike.setText("0");
} else {
h.numoflike.setText(items.getNumOfLike());
}
h.name.setOnClickListener(listener);
h.name.setTag(R.id.CommentName, h.name);
h.name.setTag(position);
h.numoflike.setOnClickListener(listener);
h.numoflike.setTag(R.id.CommentNumOfLike, h.numoflike);
h.numoflike.setTag(position);
h.like.setOnClickListener(listener);
h.like.setTag(R.id.CommentLike, h.like);
h.like.setTag(position);
h.img.setOnClickListener(listener);
h.img.setTag(R.id.CommentImg, h.img);
h.img.setTag(position);
return v;
}
private OnClickListener listener = new View.OnClickListener() {
#SuppressWarnings("deprecation")
#Override
public void onClick(View view) {
final int id = view.getId();
type = " ";
CommentEvents t = new CommentEvents();
t.execute();
switch (id) {
case R.id.CommentImg:
dbobj = new DataBaseHandler(getContext());
Items1 = dbobj.select_HowComment();
dbobj.CloseDataBase();
Collections.reverse(Items1);
x = (Integer) view.getTag();
Log.v("value", x + Items1.get(x).getName());
Toast.makeText(getContext(), Items1.get(x).getName(),
Toast.LENGTH_SHORT).show();
break;
case R.id.CommentName:
// ///profile
try {
dbobj = new DataBaseHandler(getContext());
Items1 = dbobj.select_HowComment();
dbobj.CloseDataBase();
Collections.reverse(Items1);
x = (Integer) view.getTag();
TextView tv = (TextView) view.getTag(R.id.CommentName);
Log.v("value", x + Items1.get(x).getName());
Toast.makeText(getContext(), Items1.get(x).getName(),
Toast.LENGTH_SHORT).show();
// String user = Items1.get(x).getUserId();
} catch (IndexOutOfBoundsException e) {
// TODO: handle exception
}
break;
case R.id.CommentLike:
dbobj = new DataBaseHandler(getContext());
Items1 = dbobj.select_HowComment();
dbobj.CloseDataBase();
Collections.reverse(Items1);
x = (Integer) view.getTag();
Log.v("value", x + "");
TextView like = (TextView) view.getTag(R.id.CommentLike);
target = Items1.get(x).getCommentId().toString();
type = "like";
CommentEvents s = new CommentEvents();
s.execute(user_id, secret_id, table, target);
if (Items1.get(x).getiLike().equals("0")) {
like.setText("Unlike");
onclick = "true";
} else if (Items1.get(x).getiLike().equals("1")) {
like.setText("Like");
onclick = "true";
}
case R.id.CommentNumOfLike:
// ////howlike
if (onclick.equals("true")) {
x = (Integer) view.getTag();
TextView numofLike = (TextView) view
.getTag(R.id.CommentNumOfLike);
if (numofLike != null) {
if (Items1.get(x).getiLike().equals("0")) {
Log.e("getNumOfLike____unlike", Items1.get(x)
.getNumOfLike());
int num = Integer.parseInt(Items1.get(x)
.getNumOfLike().toString());
num = num + 1;
Log.e("numberunlike", num + "");
numofLike.setText(num + "");
} else if (Items1.get(x).getiLike().equals("1")) {
Log.e("getNumOfLike_______like", Items1.get(x)
.getNumOfLike());
int num = Integer.parseInt(Items1.get(x)
.getNumOfLike().toString());
num = num - 1;
Log.e("numberlike", num + "");
numofLike.setText(num + "");
}
onclick=" "; }
} else {
try {
dbobj = new DataBaseHandler(getContext());
Items1 = dbobj.select_HowComment();
dbobj.CloseDataBase();
Collections.reverse(Items1);
x = (Integer) view.getTag();
Log.v("value", x + "");
target = Items1.get(x).getCommentId().toString();
type = "numoflike";
CommentEvents r = new CommentEvents();
r.execute(user_id, secret_id, table, target);
} catch (IndexOutOfBoundsException e) {
// TODO: handle exception
}
}
}
}
};
Use
View view = v.getParent();
and then try to initialize views with view in onClick
You can set Tag
h.name.setOnClickListener(listener);
h.name.setTag(h.numoflike); // use setTag only once
h.numoflike.setOnClickListener(listener);
h.numoflike.setTag(h.name); // use setTag only once
Then in on click
public void onClick(View v) {
switch(v.getId())
{
case R.id.CommentName :
TextView tv1 = (Button) v.getTag();
TextView tv2 = (TextView) v;
tv1.setText("clicked");
tv2.setText("clicked");
break;
case R.id.CommentNumOfLike :
//do something
break;
}
}
Or in onClick Initialize yours views
View view = v.getParent();
TextView tv = view.findVIewById(R.id.CommentName);
TextView tv1 = view.findVIewById(R.id.CommentNumOfLike);
tv.setText("clicked");
tv1.setText("clicked");

Categories

Resources