Android : Custom array adapter doesn't work in DialogFragment

on Monday, August 18, 2014


I want to populate a list view with custom array adapter, however it looks like getView() is never called. Below is my code, this part creates the dialog:



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


This part creates the view, I commented on which works, which doesn't



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


This is the custom array adapter, only getView is relevant, but post here the whole class in case.



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

}


What's wrong with my DeleteAdapter? Thanks.


0 comments:

Post a Comment