Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add custom ListView in alert dialog android

I have one scenario in which,when I click on Button I want the AlertDialog to popup. AlertDialog is a custom alert dialog as it has custom Listview.

I am using the following code to assign the AlertDialog OnClick of button

 top.setOnClickListener(new OnClickListener() {
 Context mcontext;
 @Override
 public void onClick(View arg0) {
 // TODO Auto-generated method stub
 LayoutInflater li = LayoutInflater.from(getActivity());
 View view= li.inflate(R.layout.topingslist, null);

 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
 alertDialogBuilder.setView(view);
 ListView lv2 = (ListView) getActivity().findViewById(R.id.toplist);
 ArrayList<SearchResultsToping> results1 = new ArrayList<SearchResultsToping>();
 SearchResultsToping sr1;                                               
 sr1 = new SearchResultsToping();
 sr1.setToppingname("cheese");
 results1.add(sr1);
 MyCustomBaseAdapterTop arrayAdapter=new MyCustomBaseAdapterTop(getActivity(), results1);
 lv2.setAdapter(arrayAdapter);
 lv2.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
    // TODO Auto-generated method stub
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

    }

    });

   alertDialogBuilder
        .setCancelable(false)
        .setPositiveButton("OK",new DialogInterface.OnClickListener() 
                  public void onClick(DialogInterface dialog,int id) {
                      })
        .setNegativeButton("Cancel",  new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog,int id) {
                    dialog.cancel()
               }
            });

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

This is my customBaseadapter to customlistview

public class MyCustomBaseAdapterTop extends BaseAdapter {
    private static ArrayList<SearchResultsToping> searchArrayListtop;
    private LayoutInflater mInflater;
     Context ctx=null;

     public MyCustomBaseAdapterTop(Activity activty, ArrayList<SearchResultsToping> results1) {
          searchArrayListtop = results1;

          this.ctx=activty;
          mInflater = activty.getLayoutInflater();  

    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
    //  return 0;
        return searchArrayListtop.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;

        //return searchArrayListtop.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int arg0, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub

        final ViewHolder holder;
        if (convertView == null) {

            Log.d("base", "listbase");
       convertView = mInflater.inflate(R.layout.each_toping, null);
       holder = new ViewHolder();


      holder.txttopname = (TextView) convertView.findViewById(R.id.topname);


       convertView.setTag(holder);
      } else {
       holder = (ViewHolder) convertView.getTag();
      }

      holder.txttopname.setText(searchArrayListtop.get(arg0).getToppingname());
      return convertView;

}
static class ViewHolder {
        TextView txttopname;
        }

}

This is topinglist.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
      <ListView 
            android:id="@+id/toplist" 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:cacheColorHint="#0000"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />        
</LinearLayout>

The line on which I am setting the Adapter to Listview lv2.setAdapter(arrayAdapter); is giving me NullPointerException please help me to achieve this.

like image 845
Ankita Deshmukh Avatar asked Nov 27 '13 06:11

Ankita Deshmukh


People also ask

How do I show a list in alerts?

This example demonstrates how do I display a listView in an android aler dialog. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I show alert dialog in Kotlin?

To set the action on alert dialog call the setPositiveButton(), setNeutralButton() and setNegativeButton() methods for positive, neutral and negative action respectively. The show() method of AlertDialog. Builder is used to display the alert dialog.

How many types of dialogs are there in Android?

There are three kinds of lists available with the AlertDialog APIs: A traditional single-choice list. A persistent single-choice list (radio buttons) A persistent multiple-choice list (checkboxes)

Which of the following is the method that sets the message to be displayed in the alert dialog?

setMessage() method for displaying the message. setIcon() method is used to set the icon on the Alert dialog box.


1 Answers

Within alert dialog builder change below line

ListView lv2 = (ListView) getActivity().findViewById(R.id.toplist);

with

ListView lv2 = (ListView) view.findViewById(R.id.toplist);
like image 155
SathishKumar Avatar answered Oct 13 '22 08:10

SathishKumar