2012-07-25 29 views
2

我在AlertDialog中显示列表。我想用某种风格(也许是自定义)自定义列表。还希望在警报对话框的列表中选择一个项目。我已经使用了非常简单的代码:Android:将样式应用于列表并设置AlertDialog中显示的选定项目

   new AlertDialog.Builder(TrainsListActivity.this).setTitle(curTrainRoute.getName() + " to " + stationNames[which]) 
       .setItems(timeList, new DialogInterface.OnClickListener() {   
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
        } 
       }) 

       .show(); 

我试过寻找相同的,但是找不到相同的资源。你能帮我解决吗?任何帮助,高度赞赏。

感谢警告对话框的

+0

尝试实现ListAdapter并将其设置为AlertDialogBu​​ilder。这可以更好地控制所布置的内容 – 2012-07-25 11:41:19

回答

0

setView()方法是有用的,可以设置为ListView自定义视图您的对话框。

例如。

CustomDialogListAdapter adapter = new CustomDialogListAdapter(); 
     ListView list = new ListView(this); 
     list.setAdapter(adapter); 
     list.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
       setTitle("item : " + arg2); 
      } 
     }); 

     final Builder dialogBuilder = new Builder(this); 
     dialogBuilder.setTitle("Custom List Dialog"); 
     dialogBuilder.setView(list);   
     dialogBuilder.create().show(); 

您可以创建自定义列表适配器如下

class CustomDialogListAdapter extends BaseAdapter { 


     private String[] data = { "People Names", "Dog Names", "Cat Names", "Fish Names" }; 
     private ArrayList<Boolean> checkboxStatus = new ArrayList<Boolean>(); 

     public CustomDialogListAdapter() { 
      int count = data.length; 
      for (int i = 0; i < count; i++) 
       checkboxStatus.add(false); 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      ViewHolder holder; 

      if(convertView == null) { 
       convertView = getLayoutInflater().inflate(R.layout.raw_dialog_item, null); 
       holder = new ViewHolder(); 
       holder.chk = (CheckBox) convertView.findViewById(R.id.chkbox); 
       holder.chk.setOnCheckedChangeListener(checkboxListener); 
       holder.txt = (TextView) convertView.findViewById(R.id.txtTitle); 

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

      holder.chk.setTag("" + position); 
      holder.chk.setChecked(checkboxStatus.get(position)); 
      holder.txt.setText(data[position]); 

      return convertView; 
     } 

     class ViewHolder { 
      CheckBox chk; 
      TextView txt; 
     } 

     private OnCheckedChangeListener checkboxListener = new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       int position = Integer.parseInt(buttonView.getTag().toString()); 
       checkboxStatus.set(position, isChecked); 
      } 
     }; 

     @Override 
     public int getCount() { 
      return data.length; 
     } 

     @Override 
     public Object getItem(int position) { 
      return data[position]; 
     } 

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

    } 

原列表项(raw_dialog_item.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:descendantFocusability="blocksDescendants" > 

    <CheckBox 
     android:id="@+id/chkbox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/txtTitle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_toRightOf="@id/chkbox" 
     android:text="@string/hello_world" /> 

</RelativeLayout> 
0

这里的文件正在使用它你可以能够代码以简单的步骤将选择器设置为列表视图

AlertDialog alert = builder.create(); 
            alert.getListView().setSelector(R.drawable.listview_selector); 
            alert.getListView().setBackgroundColor(Color.RED); 
            alert.getListView().invalidate(); 

           alert.show(); 
相关问题