【问题标题】:showDialog in button Listview adapter按钮 Listview 适配器中的 showDialog
【发布时间】:2015-05-08 07:50:19
【问题描述】:

我有一个类似THIS 的列表视图

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

所以当我按“是”时。它将从列表中删除。

这是我的代码..

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
  • customadapter 中的line number 115 是什么?
  • 也许,这有帮助 - stackoverflow.com/a/10336214/3913366 ...
  • 你也应该使用 customadapter 的 insted 命名约定,将其命名为 CustomAdapter,因为它是一个类,如 BaseAdapter
  • 是您的上下文应用上下文吗?对于对话框,您必须使用 Activity 的实例而不是应用上下文。

标签: android listview android-listview showdialog


【解决方案1】:

创建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;
}

然后在delete button click 上检查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");
        }
    });  

最后是initialize 适配器在您的activity/fragmentlistener invoke 上显示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);
}
});

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

(另请注意,要创建 custom dialog,您必须 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();

注意:我已经硬编码了这个答案,所以任何syntax error 都可以出现

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      首先通过这样的活动。

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

      【讨论】:

        【解决方案4】:

        您也可以在活动中执行此操作。对于适配器代码,将一些标签添加到您的按钮以识别按下了哪个按钮。您也可以将位置设置为标签。

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

        【讨论】:

          【解决方案5】:

          我认为有一种简单的方法可以做到这一点,就我而言,我已遵循此代码。 编写一个方法 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(); 
          }
          

          用户单击“是”或“否”按钮后,对话框将自行关闭。你不需要做任何事情

          【讨论】:

            猜你喜欢
            • 2012-09-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-10-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多