当我将旧布局与ImageVIEws一起使用时,我已经更新了布局,将ImageVIEws替换为buttons,并且ListvIEw OnClick侦听器现在无法正常工作.如果需要进一步格式化,请告诉我,我们将提供任何帮助.
活动中的侦听器:
lvItem.setonItemClickListener(new AdapterVIEw.OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID) { Intent intent = new Intent(MainActivity.this, ItemDetailsActivity.class); intent.putExtra("position", position); startActivity(intent); }});
List_item.xml:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:orIEntation="vertical"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"> <androID.support.v7.Widget.CardVIEw androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_marginBottom="5dp"> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" androID:padding="16dp"> <TextVIEw androID:ID="@+ID/Itemnametv" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Item : " androID:textSize="20sp" androID:layout_marginBottom="8dp"/> <TextVIEw androID:ID="@+ID/qtytv" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Qty : " androID:layout_marginBottom="8dp"/> <VIEw androID:layout_wIDth="fill_parent" androID:layout_height="1dp" androID:background="#c0c0c0" androID:layout_marginBottom="8dp"/> <linearLayout androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:orIEntation="horizontal"> <button androID:ID="@+ID/EditItem" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Edit" androID:minWIDth="0dp" androID:minHeight="0dp" /> <button androID:ID="@+ID/DeleteItem" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Delete" androID:textcolor="@color/btn_logut_bg" androID:minWIDth="0dp" androID:minHeight="0dp" /> </linearLayout> </linearLayout> </androID.support.v7.Widget.CardVIEw></linearLayout>
ItemDetailsAdapter.java:
public class ItemDetailsAdapter extends BaseAdapter { private ArrayList<Item> arrayListItem; private Context context; private LayoutInflater inflater; public ItemDetailsAdapter(Context context, ArrayList<Item> arrayListItem) { this.context = context; this.arrayListItem = arrayListItem; inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @OverrIDe public int getCount() { return arrayListItem.size(); } @OverrIDe public Object getItem(int position) { return arrayListItem.get(position); } @OverrIDe public long getItemID(int position) { return 0; } @OverrIDe public VIEw getVIEw(final int position, VIEw convertVIEw, VIEwGroup parent) { VIEw v = convertVIEw; Holder holder; if (v == null) { v = inflater.inflate(R.layout.List_item, null); holder = new Holder(); holder.Itemname = (TextVIEw) v.findVIEwByID(R.ID.Itemnametv); holder.qty = (TextVIEw) v.findVIEwByID(R.ID.qtytv); holder.EditItem = (button) v.findVIEwByID(R.ID.EditItem); holder.DeleteItem = (button) v.findVIEwByID(R.ID.DeleteItem); v.setTag(holder); } else { holder = (Holder) v.getTag(); } holder.Itemname.setText(arrayListItem.get(position).getItem()); holder.qty.setText(arrayListItem.get(position).getQty()); holder.EditItem.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Intent intent = new Intent(context,AddOrUpdateItem.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra("position", position); context.getApplicationContext().startActivity(intent); } }); holder.DeleteItem.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { ShowConfirmDialog(context, position); } }); return v; } class Holder { TextVIEw Itemname, qty; button DeleteItem, EditItem; } public static voID ShowConfirmDialog(Context context, final int position) { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); alertDialogBuilder .setMessage("Are you sure you want to delete this entry?") .setCancelable(true) .setPositivebutton("YES", new DialogInterface.OnClickListener() { public voID onClick(DialogInterface dialog, int ID) { MainActivity.getInstance().deleteItem(position); } }) .setNegativebutton("NO", new DialogInterface.OnClickListener() { public voID onClick(DialogInterface dialog, int ID) { dialog.cancel(); } }); AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show(); }}
解决方法:
存在视图冲突的问题,即视图可能已被其他人重叠.
androID:descendantFocusability =“ blocksDescendants”放在您的父级布局和列表项上,对于您要为其处理点击事件的所有视图,androID:focusable =“ false”也会在按钮中添加此内容,就像在这里…
总结以上是内存溢出为你收集整理的java-Listview Onclick侦听器在更新Cardview布局后无法正常工作全部内容,希望文章能够帮你解决java-Listview Onclick侦听器在更新Cardview布局后无法正常工作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)