【问题标题】:List Item longClickListener open dialog is not working in android?列表项 longClickListener 打开对话框在 android 中不起作用?
【发布时间】:2013-06-20 05:09:12
【问题描述】:

我正在处理列表,在此列表 setOnItemLongClickListener 中编写代码部分,用户长按打开对话框但对话框未打开?请发送任何打开对话框的建议?

listshipments.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() 
        {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) 
            {
                view.setSelected(true);
                TextView tv = (TextView) view.findViewById(R.id.txtName);
                String shipmenttxt = tv.getText().toString();
                int b=delete_Message("Delete ", "Do you want delete shipment id", "Delete", "Cancel",shipmenttxt,position);
                if(b==1){
                    this.mList.remove(position);
                    adapter.notifyDataSetChanged(); 
                }
                return true;

            }

    });

@SuppressWarnings("deprecation")
    private int delete_Message(String sTitle,String sMessage,String sButton1_Text,String sButton2_Text,final String msg,final int position)
    {

        try {
            alertDialog = new AlertDialog.Builder(getParent()).create();
            alertDialog.setTitle(sTitle);
            alertDialog.setIcon(R.drawable.info);
            alertDialog.setMessage(sMessage);
            alertDialog.setButton(sButton1_Text, new DialogInterface.OnClickListener() 
            {
                public void onClick(DialogInterface dialog, int which) 
                {
                    aa=1;
                    delete(msg);

                    //new LoadDatashipment().execute();
                    return ;

                } });

            alertDialog.setButton2(sButton2_Text, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    aa=0;
                    //return;
                }});

            alertDialog.show();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   

        return aa;
    }

【问题讨论】:

    标签: android listview dialog long-press


    【解决方案1】:

    如果列表的任何行项包含可聚焦或可点击的视图,那么您的点击监听器可能无法正常工作

    您必须将此行放在您的自定义列表视图 row_item.xml 文件中 即android:descendantFocusability="blocksDescendants"

    例如:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:descendantFocusability="blocksDescendants"
    //other layout info here .....
    >
    </LinearLayout>
    

    我认为您需要做的是在显示对话框之前

    alertD = alertDialog.create();
    

    并显示

    alertD.show();
    

    例如检查这里 http://www.mkyong.com/android/android-alert-dialog-example/

    【讨论】:

      【解决方案2】:

      试试这个代码:

      listshipments.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
          @Override
          public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
              view.setSelected(true);
              TextView tv = (TextView) view.findViewById(R.id.txtName);
              String shipmenttxt = tv.getText().toString();
      
              DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      switch (which) {
                      case DialogInterface.BUTTON_POSITIVE:
                          //TODO Yes button clicked
                          this.mList.remove(position);
                          adapter.notifyDataSetChanged();
                          break;
                      case DialogInterface.BUTTON_NEGATIVE:
                          //TODO No button clicked
                          dialog.dismiss();
                          break;
                      }
                  }
              };
              AlertDialog.Builder builder = new AlertDialog.Builder(Main.this);
              builder.setMessage("Extract " + rarListView.getItemAtPosition(arg2).toString() + " \n to '" + Environment.getExternalStorageDirectory().toString() + "/AndRar/' folder?")
                  .setPositiveButton("Yes", dialogClickListener)
                  .setNegativeButton("No", dialogClickListener)
                  .show();
              return true;
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-06
        • 2014-11-18
        • 1970-01-01
        • 2016-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-21
        相关资源
        最近更新 更多