在按钮ListView适配器中显示对话框

4
我有一个类似于这个的ListView。
我希望当我按下删除按钮时,它会显示如下图所示的对话框:

enter image description here

如果我按下“是”,它将从列表中移除。
以下是我的代码...
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();

但这并不是成功。
我遇到了这个错误。 在此输入图片描述

我已经更新了我的帖子...我发布了日志logcat。 - user2809386
你在 customadapter 中的 行号115 是什么? - Kushal Sharma
1
也许这可以帮助 - http://stackoverflow.com/a/10336214/3913366 ... - Shubham A.
你应该使用命名规范,例如将customadapter改为CustomAdapter,因为它是一个类,就像BaseAdapter一样。 - Kushal Sharma
@rogerthatcode 是的,在我的自定义适配器中,第115行。 - user2809386
显示剩余6条评论
5个回答

7
创建一个接口:
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;
}

然后在 删除按钮点击 时检查是否启用了 监听器

 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初始化适配器,并在listener调用时显示对话框

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();

注意:我已经硬编码了这个答案,因此可能会出现任何语法错误


1

尝试使用以下代码。对于对话框,您必须使用Activity的实例而不是应用程序上下文。

final Dialog dialog = new Dialog(youractivity.this);
dialog.setContentView(R.layout.custom);

0

首先按照这样的方式传递活动。

list.setAdapter(new customadapter(oslist,getApplicationContext(),registerItem.this));

然后像这样获取父活动。

private Activity parentActivity;
    private Button btnDelete;
    private Button btnEdit;

    AlertDialog.Builder alertDialogBuilder;

    public customadapter(ArrayList<HashMap<String, String>> oslist,Context context, Activity parentactivity) {  
        System.out.println("skdjfhksdfjskfjhsdkjfh");
        this.context = context;
        this.oslist = oslist;
        this.parentActivity = parentactivity;

    }

并设置对话框生成器如下所示...

final Dialog dialog = new Dialog(parentActivity);

0

您还可以在活动中完成此操作。对于适配器代码,为按钮添加一些标签以标识哪个按钮被按下。您还可以将位置设置为标签。

Button btnEdit = (Button) convertView.findViewById(R.id.btnEdit);
Button btnDelete = (Button) convertView.findViewById(R.id.btnDelete);

btnEdit.setTag(oslist.get(position).get("id"),R.id.varId);
btnDelete .setTag(oslist.get(position).get("id"),R.id.varId);

您可以在自定义对话框的xml中添加以下内容:

android:onclick="deleteMethod"

在活动中编写您的方法,如下所示:

public void deleteMethod(View v)
{
// Get your id or position for which delete button was pressed
 String id=v.getTag().toString();

 new AlertDialog.Builder(this)
.setTitle("Title")
.setMessage("Do you really want to delete ?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int whichButton) {
        Toast.makeText(MainActivity.this, "Deleting...", Toast.LENGTH_SHORT).show();
// You know which item to delete from id / position. delete here.
    }})
 .setNegativeButton(android.R.string.no, null).show();



}

0

我认为有一种简单的方法可以做到这一点,在我的情况下,我已经按照以下代码进行了操作。 编写一个名为alertMessage()的方法,并在您的按钮监听器代码中调用它。

btnDelete.setOnClickListener(new OnClickListener() {                
        @Override
        public void onClick(View v) {
            //I want show YES NO dialog here
              alertMessage();
        }
    }); 



public void alertMessage() { 
    DialogInterface.OnClickListener dialogClickListener = new  DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) { 
      switch (which) { 
         case DialogInterface.BUTTON_POSITIVE: // Yes button clicked 
            Toast.makeText(AlertDialogActivity.this, "Yes Clicked",
                             Toast.LENGTH_LONG).show(); 
             // set your own logic for removal item from listview      
             break; 

            case DialogInterface.BUTTON_NEGATIVE: // No button clicked // do nothing 
             Toast.makeText(AlertDialogActivity.this, "No Clicked", 
                              Toast.LENGTH_LONG).show();    
             break; 
          } 
        } 
    }; 

   AlertDialog.Builder builder = new AlertDialog.Builder(this);  
   builder.setMessage("Are you sure?") 
           .setPositiveButton("Yes", dialogClickListener) 
           .setNegativeButton("No", dialogClickListener).show(); 
}

当用户点击“是”或“否”按钮后,对话框将自动关闭。您无需进行任何操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接