在AlertDialog multichoice自定义列表视图 [英] Custom listview on AlertDialog multichoice
问题描述
我想创建AlertDialog相同的图片:
I would like to create AlertDialog same as on the picture:
我的code:
adapter_book_desc.xml
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
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类
and class in 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的注释 - 这一切它显示,除选择题
But in that look as on the picture it isn't displayed. However if to note as the comment of the line builder.setMultiChoiceItems - that everything it is displayed, except a multiple choice.
如何纠正code所以这是在图像上。选择题和标题,在一个项目的信息?
How to correct a code so that was as on the image. Multiple choice and title and the message in one item?
推荐答案
如果您必须显示两个的TextView
的再上市,你必须使用自定义适配器,并且您已经使用,所以你没有必要使用 builder.setMultiChoiceItems
,只自定义您的布局把复选框布局和管理。
If you must have to display two TextView
in listing then you have to use custom adapter and you already using that, so you no need to use builder.setMultiChoiceItems
, just customize your layout put checkbox in your layout and manage..
如果你没有需要两个TextView的然后删除自定义适配器。
If you don't need two TextView then remove custom adapter.
这篇关于在AlertDialog multichoice自定义列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!