如何在警告对话框android中添加自定义ListView

时间:2013-11-27 06:02:44

标签: android android-listview android-alertdialog

我有一个场景,当我点击Button时,我想要弹出AlertDialogAlertDialog是一个自定义提醒对话框,因为它具有自定义Listview

我使用以下代码分配按钮的AlertDialog OnClick

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

这是我对customlistview的customBaseadapter

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

}

这是topinglist.xml文件

<?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>

我将Adapter设置为Listview lv2.setAdapter(arrayAdapter);的行  给了我NullPointerException 请帮助我实现这一目标。

2 个答案:

答案 0 :(得分:4)

在警告对话框构建器中更改以下行

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

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

答案 1 :(得分:0)

使用DialogFragment

将listview添加到其中设置适配器。 并做任何你想做的事