我正在尝试在(Sherlock)的方法中设置一个ArrayAdapter
for a 。我得到了,但我认为布局和字符串数组的路径是正确的,因为当我直接为列表设置适配器时,会显示列表。ListView
onCreateDialog()
DialogFragment
NullPointerException
AlertDialog.Builder
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
}
}
有什么建议么?
编辑:
事实证明,行条目 XML 似乎是不正确的,因为我测试了下面提到的代码
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_single_choice, myItemArray);
它奏效了。
这是我在教程中找到的自定义行条目的 XML(不再找到链接):
<?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>
我不知道可能出了什么问题...