Wednesday, May 1, 2013

[android help] Getting Blank Activity not with existing Product Information while click on List View Item row


I am writing a Cart App, and allowing user to add items into Cart Activity, in cart item list i am using 3 Textviews for numeric value (i.e: Item Cost, Qty, Total) and these values are coming from ProductInformationActivity.java, now i want to allow user to update any of the item which is still in Cart Activity by click on that particular item, i want to open that item in ProductInformationActivity.java with existing details like: what Qty user has entered earlier (like: we see in Cart Apps), to do that i am using below code in my Cart Adapter....


but i guess i am doing something wrong, due to two reasons:


  1. I think this is not the exact code to open that particular item with existing details

  2. I am getting force close whenever i do click on any of the Item in List View [now this problem has been resolved by @Grishu and @Pragnani]

below is the Line in ProductInformationActivity.java where i am getting NumberFormatException:


itemamount = Double.parseDouble(text_cost_code.getText().toString());



public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.listrow_cart, null);
vi.setClickable(true);
vi.setFocusable(true);

vi.setOnClickListener(new OnClickListener() {
@Override

public void onClick(View v)
{
HashMap item = Constant.wishProducts.get(position);
Log.d("CartAdapter", "onClick :: " + item);
Intent myIntent = new Intent
(activity, com.era.restaurant.versionoct.menu.ProductInformationActivity.class);
Log.d("CartAdapter", "Intent :: " + myIntent);
myIntent.putExtra("Item", item);
activity.startActivity(myIntent);
}
});


So here i need your help, tell me what you think what code i should to show selected item in ProductInformationActivity.java with existing details, because i think if i will put correct code in method then i will not get that force close error message...


ProductInformationActivity.java:



public class ProductInformationActivity extends Activity {

public static final String KEY_TITLE = "title";
public static final String KEY_DESCRIPTION = "description";
public static final String KEY_COST = "cost";
public static final String KEY_TOTAL = "total";
public static final String KEY_QTY = "qty";
public static final String KEY_THUMB_URL = "imageUri";


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.information_product);

Intent in = getIntent();
String title = in.getStringExtra(KEY_TITLE);
String description = in.getStringExtra(KEY_DESCRIPTION);
String thumb_url = in.getStringExtra(KEY_THUMB_URL);
String cost = in.getStringExtra(KEY_COST);
String total = in.getStringExtra(KEY_TOTAL);

ImageLoader imageLoader = new ImageLoader(getApplicationContext());

ImageView imgv = (ImageView) findViewById(R.id.single_thumb);
final TextView txttitle = (TextView) findViewById(R.id.single_title);
final TextView txtdescription = (TextView) findViewById(R.id.single_description);
TextView txtheader = (TextView) findViewById(R.id.actionbar);
text_cost_code = (TextView) findViewById(R.id.single_cost);
edit_qty_code = (EditText) findViewById(R.id.single_qty);
edit_qty_code.setText("1");
txt_total = (TextView) findViewById(R.id.single_total);

txttitle.setText(title);
txtheader.setText(title);
text_cost_code.setText(cost);
txt_total.setText(total);
txtdescription.setText(description);
imageLoader.DisplayImage(thumb_url, imgv);


if(text_cost_code.getText().length() > 0)
{
try
{
itemamount = Double.parseDouble(text_cost_code.getText().toString());
Log.d("ProductInformationActivity", "ItemAmount :: " + itemamount);
}
catch(NumberFormatException e)
{
itemamount=0.00;
Log.d("ProductInformationActivity", "NumberFormatException :: " + e);
}
}

txt_total.setText(Double.toString(itemamount));
edit_qty_code.addTextChangedListener(new TextWatcher() {

public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
if (!edit_qty_code.getText().toString().equals("")
|| !edit_qty_code.getText().toString().equals("")) {

// accept quantity by user
itemquantity = Integer.parseInt(edit_qty_code.getText()
.toString());

// changes in total amount as per change in qty (entered by user)
txt_total.setText(new DecimalFormat("##.#").format(itemamount*itemquantity));
} else {
txt_total.setText("0.00");
}
}

public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

public void afterTextChanged(Editable s) {

}
});

ImageButton mImgAddCart = (ImageButton) findViewById(R.id.button_add);
mImgAddCart.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
mTitle = txttitle.getText().toString();

mCost = text_cost_code.getText().toString();
mCost = mCost.replace("From ", "");
mTotal = txt_total.getText().toString();
mTotal = mTotal.replace("From ", "");
mQty = edit_qty_code.getText().toString();

if ( Constant.wishProducts.size() <= 0) {
HashMap mTempObj = new HashMap();
mTempObj.put(KEY_TITLE, mTitle);
mTempObj.put(KEY_QTY, mQty);
mTempObj.put(KEY_COST, mCost);
mTempObj.put(KEY_TOTAL, mTotal);
Constant.wishProducts.add(mTempObj);
/**
* code to update, already added item details
*/

} else {
for (int i = 0; i < Constant.wishProducts.size(); i++) {
if ( Constant.wishProducts.get(i).get(KEY_TITLE)
.equals(mTitle)) {
Constant.wishProducts.remove(i);
break;
} else {
}
}

HashMap mTempObj = new HashMap();
mTempObj.put(KEY_TITLE, mTitle);
mTempObj.put(KEY_QTY, mQty);
mTempObj.put(KEY_COST, mCost);
mTempObj.put(KEY_TOTAL, mTotal);
Constant.wishProducts.add(mTempObj);
}

AlertDialog.Builder alertdialog = new AlertDialog.Builder(
ProductInformationActivity.this);
alertdialog.setTitle(getResources()
.getString(R.string.app_name));
alertdialog.setMessage("Item Added to Order");

alertdialog.setPositiveButton("OK",
new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
finish();
}
});
alertdialog.show();


}
});


See some screen shots:



> Now i want to get this kind of Activity with existing Product Information,
which i have clicked in List View Item row in Cart Activity


enter image description here



> Getting this blank Activity, whenever i do click on ListView Item
row in CartActivity:


enter image description here



> ListView Item row, which i have clicked in CartActivity.java:


enter image description here


Now i have posted code for ProductInformationActivity.java, and also written that i am passing values from ProductInformationActivity.java to CartActivity.java, and 3 main screen shots to explain you what i want, when i do click on ListView Item row and What i am getting....


Please help me..... :)



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...