android - 按钮 ListView 适配器中的 showDialog

标签 android listview android-listview showdialog

我有一个类似 THIS 的 ListView

enter image description here

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

enter image description here

所以当我按"is"时。它将从列表中删除。

这是我的代码..

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;

}

@Override
public int getCount() {
    // TODO Auto-generated method stub      
    return oslist.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return oslist.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public 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();

但它没有成功。

我遇到了这个错误

enter image description here

最佳答案

创建一个接口(interface):

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

当您初始化 customadapter 时,发送 OnDeleteListener 作为参数:

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

然后点击删除按钮检查listener是否激活它:

 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/fragmentlistener invokeinitialize 适配器显示Dialog:

customadaper mAdapter = new customadapter(ArrayList<HashMap<String, String>> oslist,Context context, 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);
}
});

您可以创建一个单独的代码清除函数,并在需要时调用它

(另请注意,要创建一个自定义对话框,您必须inflate它{可能这就是您收到错误的原因}) :

private void showDialog(String message){
// set the custom dialog components - text, image and button
inflater = 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 115

AlertDialog.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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30118595/

相关文章:

c# - 带有分组项目的 ListView - 通过组标题复选框选择所有组成员

android - 不幸的是应用程序已停止,doinbackground() 出错

android - Facebook SDK 3.0 : Unable to call openForPublish after the during a session state CLOSED_LOGIN_FAILED

android - ListFragment OnListItemClick 未被调用

java - Android 手机 radio 类型的值是否始终为 GSM、LTE、CDMA 或 WCDMA?

android - android中列表的listview

android - 实现这些 ListActivity 以在 url 中显示视频

android - 如何在 Android 中创建带有虚线/点线分隔符的 ListView?

android - 在 Android 上检测重要的设备运动

android - 为什么 android 深度链接使我在 Whatsapp 上的链接在 Whatsapp 的应用程序中打开?