Reputation: 1254
I'm trying to set an ArrayAdapter
for a ListView
inside the onCreateDialog()
method of a (Sherlock)DialogFragment
. I'm getting just NullPointerException
but I think the path to the layout and string-array is correct, because when I set the Adapter directly for the AlertDialog.Builder
the List is shown.
public class InputMyDialogFragment
extends SherlockDialogFragment
implements OnClickListener {
private ListView listView;
private ArrayAdapter<String> adapter;
private String[] myItemArray;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
myItemArray = getResources().getStringArray(R.array.my_item_array);
listView = (ListView) getActivity().findViewById(R.id.dialog_listView);
adapter = new ArrayAdapter<String>(getActivity(), R.id.row_relativeLayout, myItemArray);
listView.setAdapter(adapter);
return new AlertDialog.Builder(getActivity())
.setView(getActivity().getLayoutInflater().inflate(R.layout.dialog, null))
//.setAdapter(adapter, this)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}
Any suggestions?
Edit:
It turns out that the row-entry XML seems to be incorrect, since I tested the below mentioned code with
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_single_choice, myItemArray);
and it worked.
Here's the XML for the custom row-entry, I found in a tutorial (can't find the link anymore):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row_relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true" >
<TextView
android:id="@+id/row_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="left|center_vertical"
android:singleLine="false"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Small" />
<CheckedTextView
android:id="@+id/row_checkstate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="6dip"
android:background="#00000000"
android:checkMark="@android:drawable/btn_radio"
android:checked="false"
android:clickable="false"
android:focusable="false" />
</RelativeLayout>
I have no idea what could be wrong...
Upvotes: 0
Views: 1087
Reputation: 6622
You try to find yout list before inflating your layout.
Try this, at the beginning of your method :
View v = getActivity().getLayoutInflater().inflate(R.layout.dialog, null);
//if you only have one listview in your layout, v is your listview
// ListView lv = (ListView)v;
ListView lv = v.findById(R.id.dialog_listView);
Then, give v
instead or inflating the layout in your return statement.
return new AlertDialog.Builder(getActivity())
.setView(v)...
Upvotes: 2
Reputation: 405
U should use ur own layout for dialogFragment in onCreateView method
Ex:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout to use as dialog or embedded fragment
View v = inflater.inflate(R.layout.layout_frogos_password, container, false);
Button buttonOk = (Button) v.findViewById(R.id.buttonForgotPasswordDialogOk);
buttonOk.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
mListener.onDialogPositiveClick(ForgotPasswordDialog.this);
}
});
return v;
}
/** The system calls this only when creating the layout in a dialog. */
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
// The only reason you might override this method when using
// onCreateView() is
// to modify any dialog characteristics. For example, the dialog
// includes a
// title by default, but your custom layout might not need it. So here
// you can
// remove the dialog title, but you must call the superclass to get the
// Dialog.
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
return dialog;
}
Upvotes: 0