我想创建与图片相同的AlertDialog:
我的代码:
adapter_book_desc.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:gravity="center_vertical"
android:paddingLeft="15dip"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<CheckedTextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary"
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity
final CharSequence[] items = arr_book_title.toArray(new CharSequence[arr_book_title.size()]);
final ArrayList<Integer> seletedItems = new ArrayList<Integer>();
AlertDialog.Builder builder = new AlertDialog.Builder(_context);
builder.setAdapter(new adapterBookDesc(), null);
builder.setTitle(_context.getString(R.string.alert_selectbook_message));
builder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
if (isChecked) {
seletedItems.add(indexSelected);
}else if(seletedItems.contains(indexSelected)){
seletedItems.remove(Integer.valueOf(indexSelected));
}
}
}).setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
String rr = "";
for (Object s : seletedItems){
rr += s.toString() + ";";
}
if(!rr.equals("")){
new saveBookInAutor().executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, rr, selGroupParam);
}else{
Toast.makeText(_context, R.string.toast_select_newbookautor, Toast.LENGTH_LONG).show();
}
}
}).setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
});
builder.create().show();
和BaseAdapter中的类
class adapterBookDesc extends BaseAdapter
{
@Override
public int getCount()
{
return arr_book_title.size();
}
@Override
public Object getItem(int position)
{
return arr_book_title.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.adapter_book_desc, null);
}
((TextView)convertView.findViewById(R.id.text1)).setText(arr_book_title.get(position));
((TextView)convertView.findViewById(R.id.text2)).setText(arr_book_param.get(position));
return convertView;
}
}
但是在图片中看起来并没有显示出来。但是,如果要注意行builder.setMultiChoiceItems的注释 - 它显示的所有内容,除了多项选择。
如何更正代码,使其与图像一样。多项选择和标题以及一个项目中的消息?
答案 0 :(得分:2)
如果您必须在商家信息中显示两个TextView
,那么您必须使用自定义适配器并且已经使用了该适配器,因此您无需使用builder.setMultiChoiceItems
,只需自定义您的布局放置复选框即可布局和管理..
如果您不需要两个TextView,则删除自定义适配器。