android – 按钮Listview适配器中的showDialog

android – 按钮Listview适配器中的showDialog,第1张

概述我有一个像 THIS的listView 当我按下删除时我想要的.它会显示一个像这个图像的对话框 所以,当我按YES.它将从列表中删除. 这是我的代码.. public class customadapter extends BaseAdapter{ ArrayList<HashMap<String, String>> oslist;Context context;private Butto 我有一个像 THIS的ListVIEw

当我按下删除时我想要的.它会显示一个像这个图像的对话框

所以,当我按YES.它将从列表中删除.

这是我的代码..

public class customadapter extends BaseAdapter{ ArrayList<HashMap<String,String>> osList;Context context;private button btnDelete;private button btnEdit;AlertDialog.Builder alertDialogBuilder;public customadapter(ArrayList<HashMap<String,String>> osList,Context context) {      System.out.println("skdjfhksdfJskfjhsdkjfh");    this.context = context;    this.osList = osList;}@OverrIDepublic int getCount() {    // Todo auto-generated method stub          return osList.size();}@OverrIDepublic Object getItem(int position) {    // Todo auto-generated method stub    return osList.get(position);}@OverrIDepublic long getItemID(int position) {    // Todo auto-generated method stub    return position;}@OverrIDepublic VIEw getVIEw(final int position,VIEw convertVIEw,VIEwGroup parent) {    System.out.println("osList osList = "+osList);    System.out.println("osList 1 = "+osList);    System.out.println("osList size = "+osList.size());    System.out.println("osList osList = "+osList.getClass());    System.out.println("position = "+position);    System.out.println("convertVIEw = "+convertVIEw);    System.out.println("parent = "+parent);    System.out.println("position =  "+position);    LayoutInflater lif = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    convertVIEw = lif.inflate(R.layout.Listitem,null);    TextVIEw ID = (TextVIEw) convertVIEw.findVIEwByID(R.ID.varID);      TextVIEw noNota = (TextVIEw) convertVIEw.findVIEwByID(R.ID.varNoNota);    TextVIEw sendername = (TextVIEw) convertVIEw.findVIEwByID(R.ID.varSendername);    TextVIEw totalAmount = (TextVIEw) convertVIEw.findVIEwByID(R.ID.varTotalAmount);    ID.setText(osList.get(position).get("ID"));    noNota.setText(osList.get(position).get("noNota"));    sendername.setText(osList.get(position).get("sendername"));    totalAmount.setText(osList.get(position).get("totalAmount"));       button btnEdit = (button) convertVIEw.findVIEwByID(R.ID.btnEdit);    button btnDelete = (button) convertVIEw.findVIEwByID(R.ID.btnDelete);    btnEdit.setonClickListener(new OnClickListener() {        @OverrIDe        public voID onClick(VIEw v) {            Toast.makeText(context,"Edit ditekan!",Toast.LENGTH_LONG).show();            //I want show YES NO dialog here.                   }    });    btnDelete.setonClickListener(new OnClickListener() {                        @OverrIDe        public voID onClick(VIEw v) {            //I want show YES NO dialog here        }    });       return convertVIEw;}

}

我该怎么做才能创建一个像这样的对话..我试过这段代码

final Dialog dialog = new Dialog(context);                dialog.setContentVIEw(R.layout.custom);                dialog.setTitle("Title...");                // set the custom dialog components - text,image and button                TextVIEw text = (TextVIEw) dialog.findVIEwByID(R.ID.text);                text.setText("AndroID custom dialog example!");                ImageVIEw image = (ImageVIEw) dialog.findVIEwByID(R.ID.image);                image.setimageResource(R.drawable.ic_launcher); //line 115                button dialogbutton = (button) dialog.findVIEwByID(R.ID.dialogbuttonOK);                // if button is clicked,close the custom dialog                dialogbutton.setonClickListener(new OnClickListener() {                    @OverrIDe                    public voID onClick(VIEw v) {                        dialog.dismiss();                    }                });                dialog.show();

但它的成功.

我收到了这个错误

@R_403_6120@ 创建一个界面:

public interface OnDeleteListener {    public voID onDelete(String message);}

在初始化customadapter时,请将OnDeleteListener作为参数发送:

private OnDeleteListener mListener;public customadapter(ArrayList<HashMap<String,Context context,OnDeleteListener mListener) {      this.context = context;    this.osList = osList;    this.mListener = mListener;}

然后在删除按钮上单击检查侦听器是否激活它:

btnDelete.setonClickListener(new OnClickListener() {                        @OverrIDe        public voID onClick(VIEw v) {            //I want show YES NO dialog here            if(mListener != null)              mListener.onDelete("The message you want to show");        }    });

最后在您的activity / fragment中初始化适配器,并在监听器上调用show Dialog:

customadaper mAdapter = new customadapter(ArrayList<HashMap<String,new OnDeleteListener(){   @OverrIDe   public voID onDelete(String msg){    //Show your dialog here    //msg - you can send any parameter or none of them through interface just as an example i send a message to show    showDialog(msg);}});

您可以创建一个单独的代码清除功能,并在您想要使用时调用它

(另请注意,要创建自定义对话框,您必须对其进行充气{这可能就是您收到错误的原因}):

private voID showDialog(String message){// set the custom dialog components - text,image and buttoninflater = mInflater.inflate(R.layout.your_custom_dialog,null,false);TextVIEw text = (TextVIEw) inflater.findVIEwByID(R.ID.text);text.setText(message);ImageVIEw image = (ImageVIEw) inflater.findVIEwByID(R.ID.image);image.setimageResource(R.drawable.ic_launcher); //line 115AlertDialog.Builder mDialogBuilder = new AlertDialog.Builder(context);             mDialogBuilder.setVIEw(vIEwFilterDialog);             mDialogBuilder.setCancelable(true);mDialogBuilder.setTitle(mRes.getString(R.string.dialog_Title_filter));             ...final AlertDialog mAlertDialog = mDialogBuilder.create();mAlertDialog.show();

注意:我已对此答案进行了硬编码,因此可能会出现语法错误

总结

以上是内存溢出为你收集整理的android – 按钮Listview适配器中的showDialog全部内容,希望文章能够帮你解决android – 按钮Listview适配器中的showDialog所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1122348.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存