Hi everybody I'm new in android. I am getting a problem in CursorAdapter. My UI is as follow
Problem -
When I click on EditText and then click Plus Button then some thing else goes data base. Suppose I click on EditText and then Plus Button of “Aloo Tikki” , then “Reshmi kabab ” goes to database. If I click on EditText and then Plus button of “Corn Tikki” then “Tandori chiken” goes in data base.
Without click on EditText it working fine as I wish.
I am going to post my CursorAdapter code
public class CustAdpt extends CursorAdapter{
private Context mContext;
private LayoutInflater mInflater;
Cursor cursor, c;
private String str_item_id;
private String str_item_type;
private String str_item_name;
private String str_item_cost;
String str_edt_txt_change;
int quantity =1;
double amount =0;
int int_edttxt =2;
int block_not_chk_for_first_click ;
ViewHolder holder;
SQLiteDatabase dh = DatabaseHelpereKOT.getInstance().getDb();
public CustAdpt(Context context, Cursor c) {
super(context, c);
mInflater = LayoutInflater.from(context);
mContext = context;
cursor = c;
System.out.println("cuntrutor custAdpt");
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
holder = (ViewHolder) view.getTag();
System.out.println("inside bind view");
holder.setImType((ImageView) view.findViewById(R.id.veg_nv_image));
holder.setTvTitle((TextView) view.findViewById(R.id.item_name));
holder.setTvPrice((TextView) view.findViewById(R.id.item_cost));
holder.getTvTitle().setText(cursor.getString(cursor.getColumnIndex("item_name")));
holder.getTvPrice().setText(cursor.getString(cursor.getColumnIndex("Item_cost")));
int _id = cursor.getInt(cursor.getColumnIndex("_id"));
view.setTag(R.id.item_name, _id);
String type = cursor.getString(cursor.getColumnIndex("item_type"));
if (type.trim().startsWith("n") || type.trim().startsWith("N")) {
//holder.getImType().setPadding(6, 0, 0, 0);
holder.getImType().setBackgroundResource(R.drawable.non_veg);
} else {
holder.getImType().setBackgroundResource(R.drawable.veg);
}
System.out.println("end of bind view");
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final ViewHolder holder_newview;
c = cursor;
View convertView;
convertView = mInflater.inflate(R.layout.list_data_item, parent, false);
System.out.println("only once calling of new view method");
block_not_chk_for_first_click =1;
holder_newview = new ViewHolder();
holder_newview.setId(cursor.getInt(0));
System.out.println("check edit text click 11111");
((ImageView) (convertView.findViewById(R.id.add_image)))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
System.out.println("button click");
System.out.println("quantity for item :::::::"+int_edttxt);
addToDB(holder_newview.getId());
System.out.println("i m in ryt [palce");
}
});
convertView.setTag(holder_newview);
System.out.println("check edit text click 222222");
return convertView;
}
void addToDB(Integer objId) {
if (objId != null) {
int _id = objId;
boolean flag = true;
System.out.println("on Add button click "+_id);
Cursor cursor = dh.query(DatabaseHelpereKOT.RESTAURANT_menu_temp,
new String[] { "_id", "item_id", "item_type", "item_name","Item_cost"},"_id=?", new String[] { String
.valueOf(_id) }, null, null, null);
if ((cursor != null) && (cursor.getCount() > 0)
&& cursor.moveToFirst() )
{
str_item_id = cursor.getString(cursor.getColumnIndex("item_id"));
str_item_type = cursor.getString(cursor.getColumnIndex("item_type"));
str_item_name = cursor.getString(cursor.getColumnIndex("item_name"));
str_item_cost = cursor.getString(cursor.getColumnIndex("Item_cost"));
amount = int_edttxt*Integer.parseInt(str_item_cost);
System.out.println("iten name ::::::::: "+str_item_name);
cursor.close();
ContentValues orderValues = new ContentValues();
orderValues.put("item_id", str_item_id);
orderValues.put("item_type", str_item_type);
orderValues.put("item_name", str_item_name);
orderValues.put("Item_cost", str_item_cost);
orderValues.put("qty", int_edttxt);
orderValues.put("amount", amount);
dh.insert(DatabaseHelpereKOT.RESTAURANT_menu_order, null, orderValues);
block_not_chk_for_first_click =2;
System.out.println("block not chk after insertion"+block_not_chk_for_first_click);
System.out.println("data inserted at order");
String msg = "Menu Item Added Successfully";
Message msgObject = new Message();
msgObject.what = 1;
msgObject.obj = msg;
addMenuItemHandler.sendMessage(msgObject);
}
}
}
public Handler addMenuItemHandler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
if (msg.what == 1) {
System.out.println("inside handler");
Toast.makeText(mContext, (String) msg.obj, Toast.LENGTH_SHORT)
.show();
}
};
};
}
Now this is going to post my list_data_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/relativeLayoutMainBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5px"
android:layout_marginTop="5px"
android:orientation="horizontal" >
<ImageView
android:id="@+id/add_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:layout_marginRight="20dp"
android:src="@drawable/add_to_db_1" />
<View
android:id="@+id/separator_info_add"
android:layout_width="22sp"
android:layout_height="2sp"
android:layout_toLeftOf="@id/add_image"
android:background="#000000"
android:visibility="invisible" />
<ImageView
android:id="@+id/main_body_item_title_first_pics"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_toLeftOf="@id/separator_info_add"
android:clickable="true"
android:onClick="clickHandler"
android:src="@drawable/add_item_order"
android:visibility="gone" />
<TextView
android:id="@+id/item_cost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_toLeftOf="@id/main_body_item_title_first_pics"
android:text="$00"
android:textColor="#000000"
android:textSize="18dip"
android:textStyle="italic"
android:typeface="serif" />
<ImageView
android:id="@+id/veg_nv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp" />
<ImageView
android:id="@+id/main_body_item_pics2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/veg_nv_image"
android:src="@drawable/add_item_order"
android:textSize="20dip"
android:visibility="gone" />
<TextView
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_toRightOf="@id/main_body_item_pics2"
android:text="Title"
android:textColor="@color/Txt_color_menu_list"
android:textSize="20dip"
android:textStyle="bold|italic" />
<TextView
android:id="@+id/main_body_item_title_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/item_name"
android:layout_marginLeft="7dip"
android:layout_toLeftOf="@id/item_cost"
android:layout_toRightOf="@id/main_body_item_pics2"
android:text="title description"
android:textColor="#000"
android:textSize="16dip"
android:visibility="gone" />
<EditText
android:id="@+id/edtxt_qty_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/item_cost"
android:ems="5"
android:hint="Qty"
android:inputType="number"
android:textStyle="bold"
android:textColorHint="@color/Txt_color_menu_list" >
<requestFocus />
</EditText>
</RelativeLayout>
<View
android:id="@+id/separator_bottom"
android:layout_width="fill_parent"
android:layout_height="10sp"
android:background="#000000"
android:visibility="invisible" />
I can't understand why EditText's Click alter Plus Click. Any help is really appreciated. Please some help me , It is SOS situation. Thanks in advance to all.
There is a problem by maintaining ViewHolder
and convertView
as global references. These values will get updated when newView()
is called every time. and with respect to comments, this will help you...
public class SampleAdapter extends CursorAdapter {
private Context mContext;
private LayoutInflater mInflater;
private String str_item_id;
private String str_item_type;
private String str_item_name;
private String str_item_cost;
String str_edt_txt_change;
int quantity = 1;
double amount = 0;
int int_edttxt = 2;
int block_not_chk_for_first_click;
SQLiteDatabase dh = DatabaseHelpereKOT.getInstance().getDb();
public SampleAdapter(Context context, Cursor c) {
super(context, c);
mInflater = LayoutInflater.from(context);
mContext = context;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder) view.getTag();
System.out.println("inside bind view");
holder.getTvTitle().setText(
cursor.getString(cursor.getColumnIndex("item_name")));
holder.getTvPrice().setText(
cursor.getString(cursor.getColumnIndex("Item_cost")));
int _id = cursor.getInt(cursor.getColumnIndex("_id"));
view.setTag(R.id.item_name, _id);
String type = cursor.getString(cursor.getColumnIndex("item_type"));
if (type.trim().startsWith("n") || type.trim().startsWith("N")) {
// holder.getImType().setPadding(6, 0, 0, 0);
holder.getImType().setBackgroundResource(R.drawable.non_veg);
} else {
holder.getImType().setBackgroundResource(R.drawable.veg);
}
int ID = cursor.getInt(0);
holder.getAddImage().setTag(ID);
holder.getAddImage().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("button click");
System.out.println("quantity for item :::::::" + int_edttxt);
int id = (Integer) v.getTag();
addToDB(id);
System.out.println("i m in ryt [palce");
View parent = (View) v.getParent();
EditText editText = (EditText) parent.findViewById(R.id.edtxt_qty_id);
String text = editText.getText().toString();
// Now you got the text in EditText. You can save this text
}
});
System.out.println("end of bind view");
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View convertView = mInflater.inflate(R.layout.list_data_item, parent,
false);
block_not_chk_for_first_click = 1;
ViewHolder holder = new ViewHolder();
ImageView imageView = (ImageView) convertView
.findViewById(R.id.add_image);
holder.setAddImage(imageView);
holder.setImType((ImageView) convertView
.findViewById(R.id.veg_nv_image));
holder.setTvTitle((TextView) convertView.findViewById(R.id.item_name));
holder.setTvPrice((TextView) convertView.findViewById(R.id.item_cost));
convertView.setTag(holder);
System.out.println("check edit text click 222222");
return convertView;
}
void addToDB(Integer objId) {
if (objId != null) {
int _id = objId;
boolean flag = true;
System.out.println("on Add button click " + _id);
Cursor cursor = dh.query(DatabaseHelpereKOT.RESTAURANT_menu_temp,
new String[] { "_id", "item_id", "item_type", "item_name",
"Item_cost" }, "_id=?",
new String[] { String.valueOf(_id) }, null, null, null);
if ((cursor != null) && (cursor.getCount() > 0)
&& cursor.moveToFirst()) {
str_item_id = cursor
.getString(cursor.getColumnIndex("item_id"));
str_item_type = cursor.getString(cursor
.getColumnIndex("item_type"));
str_item_name = cursor.getString(cursor
.getColumnIndex("item_name"));
str_item_cost = cursor.getString(cursor
.getColumnIndex("Item_cost"));
amount = int_edttxt * Integer.parseInt(str_item_cost);
System.out.println("iten name ::::::::: " + str_item_name);
cursor.close();
ContentValues orderValues = new ContentValues();
orderValues.put("item_id", str_item_id);
orderValues.put("item_type", str_item_type);
orderValues.put("item_name", str_item_name);
orderValues.put("Item_cost", str_item_cost);
orderValues.put("qty", int_edttxt);
orderValues.put("amount", amount);
dh.insert(DatabaseHelpereKOT.RESTAURANT_menu_order, null,
orderValues);
block_not_chk_for_first_click = 2;
System.out.println("block not chk after insertion"
+ block_not_chk_for_first_click);
System.out.println("data inserted at order");
String msg = "Menu Item Added Successfully";
Message msgObject = new Message();
msgObject.what = 1;
msgObject.obj = msg;
addMenuItemHandler.sendMessage(msgObject);
}
}
}
public Handler addMenuItemHandler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
if (msg.what == 1) {
System.out.println("inside handler");
Toast.makeText(mContext, (String) msg.obj, Toast.LENGTH_SHORT)
.show();
}
};
};
public static class ViewHolder {
private TextView tvTitle;
private TextView tvPrice;
private TextView tvDescription;
private ImageView imType;
private EditText edtxt_qty;
private ImageView addImage;
public ImageView getAddImage() {
return addImage;
}
public void setAddImage(ImageView imageView) {
addImage = imageView;
}
public TextView getTvTitle() {
return tvTitle;
}
public void setTvTitle(TextView tvTitle) {
this.tvTitle = tvTitle;
}
public TextView getTvPrice() {
return tvPrice;
}
public void setTvPrice(TextView tvPrice) {
this.tvPrice = tvPrice;
}
public TextView getTvDescription() {
return tvDescription;
}
public void setTvDescription(TextView tvDescription) {
this.tvDescription = tvDescription;
}
public ImageView getImType() {
return imType;
}
public void setImType(ImageView imType) {
this.imType = imType;
}
public EditText getEdtTxtQty() {
return edtxt_qty;
}
public void setEdtxtQty(EditText edtTxtqty) {
this.edtxt_qty = edtTxtqty;
}
}
}