5

I'm trying to have my AlertDialog with a custom list view but can't seem to get it to show or run without error.

   private void buildDialog(){

        int selectedItem = -1; //somehow get your previously selected choice
        LayoutInflater inflater = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
        View customView = inflater.inflate(R.layout.listview, null, false);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(customView);
        builder.setTitle("Select Weapon").setCancelable(true);
        builder.setSingleChoiceItems(inventory, selectedItem, "Desc", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which)
            { 
                ListView lv = ((AlertDialog) dialog).getListView();
                itemId = lv.getAdapter().getItemId(which);
                new changeEQ().execute();
            }
        });



        dialog = builder.create();
    }

This is my AlertDialog but can't figure out what to add to get my custom layouts, listview & listrow to be used. I've looked around at guides online but nothing they show seems to work for me. IE I must be doing something wrong.

EDIT: changed code to include answer but has no change on what is showed on screen. No errors yet no change in look.

1
  • could you please provide the Logcat output for the error you're getting Commented Apr 28, 2012 at 22:19

1 Answer 1

11

If you have a custom layout that you want to pass to your AlertDialog try:

LayoutInflater inflater = ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
View customView = inflater.inflate(R.layout.custom_dialog, null, false);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(customView);

If you want to define listeners try:

ListView list = (ListView) customView.findViewById(R.id.listView1);
list.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // Do as you please
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Do I need to take out setSingleChoiceItems and treat the alertDialog as if it was an activity listview?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.