I have search a lot of questions here and already implemented every answer i get from here, but no one has solved this problem. I have 2 buttons in a row of my custom listview and i want to perform on click on them. Here is my custom adapter and xml code.
CartAadpter.java
public class CartAdapter extends ArrayAdapter<CartItemListData> {
Context context;
int layoutResourceId;
ArrayList<CartItemListData> data = new ArrayList<CartItemListData>();
LayoutInflater mInflater;
CartItemListData listData;
CartDatabaseHelper db;
public CartAdapter(Context context, int layoutResourceId,
ArrayList<CartItemListData> data) {
super(context, layoutResourceId, data);
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.data = data;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
mInflater = LayoutInflater.from(context);
convertView = mInflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.itemListData = data.get(position);
holder.text = (TextView) convertView
.findViewById(R.id.cart_item_id);
holder.text1 = (TextView) convertView
.findViewById(R.id.cart_item_boxes);
holder.edit = (Button) convertView.findViewById(R.id.editdata);
holder.delete = (Button) convertView.findViewById(R.id.deletedata);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
listData = data.get(position);
holder.text.setText(listData.getTilesId());
holder.text1.setText(String.valueOf(listData.getNumberOfBox()));
// Button's Tag
holder.edit.setTag(listData.getId()); // I have set Tag here, is it wrong?
holder.delete.setTag(listData.getId());// same as above here too
holder.edit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Edit Box");
builder.setMessage("Please Enter Number of Box.");
final EditText editText = new EditText(context);
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
editText.setLayoutParams(lp);
builder.setView(editText);
builder.setNegativeButton("Cancel", null);
builder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
if (editText.equals(null)
&& editText.getText().length() == 0) {
Toast.makeText(
context,
"To delete Data use \"delete button\"",
Toast.LENGTH_LONG).show();
} else {
int data = Integer.parseInt(editText
.getText().toString());
if (data <= 0) {
Toast.makeText(
context,
"To delete Item Please use delete button",
Toast.LENGTH_LONG).show();
} else {
CartDatabaseHelper cartDatabaseHelper = new CartDatabaseHelper(
context);
cartDatabaseHelper
.updateCart(new CartItemListData(
holder.text.getText()
.toString(),
data));
}
}
}
});
}
});
holder.delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final int id = Integer.parseInt(v.getTag().toString());
db = new CartDatabaseHelper(context);
db.delete_cartitem(id);
setNotifyOnChange(true);
}
});
return convertView;
}
class ViewHolder {
TextView text;
TextView text1;
Button edit;
Button delete;
CartItemListData itemListData;
}
}
cartlist_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/cart_item_id"
android:layout_width="0sp"
android:layout_height="fill_parent"
android:layout_marginLeft="2dp"
android:layout_weight="2"
android:background="@drawable/cart_list_text_background"
android:gravity="center_vertical|center_horizontal"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="22sp" />
<TextView
android:id="@+id/cart_item_boxes"
android:layout_width="0sp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/cart_list_text_background"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="22sp" />
<Button
android:id="@+id/editdata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="@drawable/edit"
android:focusable="false" // Here I have checked it with true value too
android:focusableInTouchMode="false" />
<Button
android:id="@+id/deletedata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="@drawable/ic_delete"
android:focusable="false"
android:focusableInTouchMode="false" />
MylistView:
<ListView
android:id="@+id/lvExp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:cacheColorHint="#FFFFFF"
android:childDivider="#f8dfdb"
android:choiceMode="singleChoice"
android:clipChildren="true"
android:clipToPadding="true"
android:divider="#ff2200"
android:dividerHeight="3dp"
android:drawSelectorOnTop="true"
android:fastScrollEnabled="true"
android:focusable="false"
android:footerDividersEnabled="true"
android:hapticFeedbackEnabled="true"
android:headerDividersEnabled="true"
android:scrollingCache="true"
android:soundEffectsEnabled="true"
android:textFilterEnabled="false"
android:transcriptMode="normal"
android:translationX="10dp" />
Can anyone tell me where did i make mistake? My work is about to finish and app is almost ready but i am stuck in this problem. I will be thankfull to any type of suggestion or help.
No comments:
Post a Comment