我想用自定义数组适配器填充列表视图,但看起来好像从不调用getView()。下面是我的代码,这部分创建了对话框:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do deletion
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
builder.setTitle("Dialog title").setView(getCustomView());
return builder.create();
}
这部分创建了视图,我评论了哪些作品没有
public View getCustomView(){
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dialog_delete_option, null);
ListView listView = (ListView) view.findViewById(R.id.listDelOptions);
/*this is default array adapter, works */
List<String> list = new ArrayList<String>();
list.add("del one");
list.add("del all");
ArrayAdapter<String> defaultAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_single_choice, list);
/*this is custom adapter, NOT working */
mAdapter = new DeleteAdapter(getActivity(),0); //last int is not used since we
//know which layout to inflate
//NOT working
//listView.setAdapter(mAdapter);
//mAdapter.notifyDataSetChanged();
//working
listView.setAdapter(defaultAdapter);
return view;
}
这是自定义数组适配器,只有getView
是相关的,但在这里发布整个类以防万一。
public static class DeleteAdapter extends ArrayAdapter{
private final int mLayoutId;
private int mCheckPos;
Context mContext;
String [] mButtonDescription;
public DeleteAdapter(Context context, int resource) {
super(context, resource);
mLayoutId= R.layout.delete_list_item_view;
mContext = context;
mCheckPos = 0;
mButtonDescription = new String[3];
mButtonDescription[0] = context.getString(R.string.del_only_this_event);
mButtonDescription[1] = context.getString(R.string.del_this_and_future_events);
mButtonDescription[2] = context.getString(R.string.del_all_events);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view =convertView;
if (view==null){
view = View.inflate(mContext,mLayoutId, null);
}
RadioButton button = (RadioButton) view.findViewById(R.id.rdDelOption);
button.setTag(new Integer(position));
button.setOnCheckedChangeListener(checkedChangeListener);
if (position== mCheckPos)
button.setChecked(true);
else
button.setChecked(false);
button.setText(mButtonDescription[position]);
return view;
}
CompoundButton.OnCheckedChangeListener checkedChangeListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
mCheckPos = (Integer) buttonView.getTag();//tag is the position
}
}
};
public int getSelectedItem(){
return mCheckPos;
}
}
我的DeleteAdapter出了什么问题?感谢。