我在这里搜索了很多问题并且已经实现了我从这里得到的每个答案,但没有人解决这个问题。我的自定义列表视图中有两个按钮,我想点击它们。这是我的自定义适配器和xml代码。
修改
CartAadpter.java
public class CartAdapter extends ArrayAdapter<CartItemListData> {
Context context;
int layoutResourceId;
ArrayList<CartItemListData> data = new ArrayList<CartItemListData>();
LayoutInflater mInflater;
CartItemListData listData;
CartDatabaseHelper db;
int i;
public CartAdapter(Context context, ArrayList<CartItemListData> data) {
super(context, R.layout.cart_listitems, data);
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
this.data = data;
notifyDataSetChanged();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
mInflater = LayoutInflater.from(context);
convertView = mInflater.inflate(R.layout.cart_listitems, parent,
false);
holder = new ViewHolder();
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);
// Button's Tag
holder.edit.setTag(data.get(position).getId());
holder.delete.setTag(data.get(position).getId());
// SetText to Textview
holder.text.setText(data.get(position).getTilesId());
holder.text1.setText(String.valueOf(listData.getNumberOfBox()));
Log.e("Database Data ID: ", listData.getId() + " " + position);
holder.edit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
i = data.get(position).getId();
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.getText().toString() == 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 {
Log.e("Edited", "Yes");
CartDatabaseHelper cartDatabaseHelper = new CartDatabaseHelper(
context);
cartDatabaseHelper
.updateCart(new CartItemListData(
i, holder.text
.getText()
.toString(),
data));
CartAdapter.this.data.clear();
CartAdapter.this.data.add(listData);
notifyDataSetChanged();
}
}
}
});
builder.create().show();
}
});
holder.delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Deleted", "Yes");
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Delete!!");
builder.setMessage("Deleting Item from List!!!");
builder.setIcon(R.drawable.ic_alerts_and_states_warning);
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
db = new CartDatabaseHelper(context);
db.delete_cartitem(data.get(position).getId());
data.remove(position);
notifyDataSetChanged();
}
});
builder.setNegativeButton("No", null);
builder.create().show();
}
});
return convertView;
}
static class ViewHolder {
TextView text, text1;
Button edit, delete;
}
}
cartlist_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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" />
谁能告诉我哪里弄错了?我的工作即将完成,应用程序几乎准备就绪,但我陷入了这个问题。我将感谢任何类型的建议或帮助。
答案 0 :(得分:3)
我不确定,但请尝试holder.edit.setOnClickListener(new View.OnClickListener()) 而不是holder.edit.setOnClickListener(new OnClickListener())。
答案 1 :(得分:1)
试试这个
将此cartlist_row.xml添加到父Linearlayout
android:clickable="false"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false"
为Button
添加此
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
和AlertDialog.Builder构建器无法正常工作,你从未使用过show()..请交叉检查..使用
AlertDialog alert = builder.create(); alert.show();