在alertDailog android中显示ListView

时间:2017-07-19 10:49:53

标签: android listview

当用户点击列表项时查看警报对话框将显示给用户,其中包括"查看","删除"或者"更新" 列表视图项。首次单击列表视图项将显示带有选项的警报对话框,但是当我单击另一个列表时查看项目或同一列表第二次应用程序崩溃时查看项目。提前感谢。

错误:java.lang.IllegalStateException:指定的子级已有父级。您必须先在孩子的父母身上调用removeView()。

  //The main list displayed when the user log in successfully  
  ListView = (ListView) findViewById(R.id.list);
    ListView2 = new ListView(this);  //List to be displayed in the alert Dailog box     
    ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            long i =  ListView.getItemIdAtPosition(position);
            showAlertDailog();//The dialog box method
        }
    });
    if(mCursorAdapter == null) //To check if the list is empty
    {
        TextView emptyTextView = (TextView) findViewById(R.id.empty);
        emptyTextView.setText("No Notes");
        ListView.setEmptyView(emptyTextView);
    }else {
        mCursorAdapter = new DataCursorAdapter(this, null);
        ListView.setAdapter(mCursorAdapter);
    }
 public void showAlertDailog()
{
    String [] items = {"View","Delete","Update"};

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item_todo_2,R.id.textView4,items);
    ListView2.setAdapter(adapter);


    final android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(HomeActivity.this);
           builder.setView(ListView2)
            .setCancelable(false)
            .setPositiveButton("Close",null);
    android.app.AlertDialog alertDialog = builder.create();
    alertDialog.show();
}

4 个答案:

答案 0 :(得分:2)

ArrayList<String> arrayList = new ArrayList<String>();

CharSequence[] animals = arrayList.toArray(new String[arrayList.size()]);

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

        dialogBuilder.setItems(animals, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {

                String selectedText = animals[item].toString();
            }
        });

        AlertDialog alertDialogObject = dialogBuilder.create();

        alertDialogObject.show();

This is simple way to display list inside alert dialog, where in arraylist just add string whatever you have to display inside alert dialog list.

答案 1 :(得分:1)

lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                String[] listItems = { "Colour", "Font Size", };
                if (listItems[position].equals("Font Size")) {


                    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                            Settings.this );

                        // set title
                        alertDialogBuilder.setTitle("Choose Font Size");

                        // set dialog message
                        alertDialogBuilder
                            .setMessage("Click yes to exit!")
                            .setCancelable(false)
                            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int id) {
                                    // if this button is clicked, close
                                    // current activity

                                }
                              })
                            .setNegativeButton("No",new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int id) {
                                    // if this button is clicked, just close
                                    // the dialog box and do nothing
                                    dialog.cancel();
                                }
                            });

                            // create alert dialog
                            AlertDialog alertDialog = alertDialogBuilder.create();

                            // show it
                            alertDialog.show();


                }

            }
        });

答案 2 :(得分:1)

这正是您所需要的

public class MainActivity extends Activity implements OnClickListener {

    private Button mDoneButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDoneButton = (Button) findViewById(R.id.done_button);
        mDoneButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        final CharSequence[] items = {
                "Rajesh", "Mahesh", "Vijayakumar"
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Make your selection");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                // Do something with the selection
                mDoneButton.setText(items[item]);
            }
        });
        AlertDialog alert = builder.create();
        alert.show();

    }
}

http://rajeshvijayakumar.blogspot.in/2013/04/alert-dialog-dialog-with-item-list.html

<强>输出

enter image description here

答案 3 :(得分:0)

public void showAlertDailog(Context context) {
ListView listView=new ListView(context);
yourLayout.LayoutParams params= yourLayout.LayoutParams(Yourlayout.LayoutParams.MATCH_PARENT,Yourlayout.LayoutPa
rams.MATCH_PARENT);
listview.setLayoutParams(params);
String [] items = {"View","Delete","Update"}; 
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items);
listView.setAdapter(adapter);
final android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context);
       builder.setView(listView)
        .setCancelable(false)
        .setPositiveButton("Close",(dialog,something)->{dialog.dismiss()});
android.app.AlertDialog alertDialog = builder.create();
alertDialog.show();

}