我正在使用AlertDialog,它應該顯示圖像和文本大小爲2的列表。但我無法得到它的工作。自定義佈局AlertDialog不起作用?
我跟着這個鏈接
Icons in a List dialog
當我把getActivity()它不是從我把getApplicationContext職位的建議,以便識別();
public MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_complaint);
cameraButton = (ImageView) findViewById(R.id.cmp_camera);
cameraButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
final String[] items = new String[]{"From Gallery", "From Camera"};
final Integer[] icons = new Integer[]{R.drawable.ic_launcher, R.drawable.ic_drawer};
ListAdapter adapter = new CameraPickAdapter(getApplicationContext(), items, icons);
new AlertDialog.Builder(getApplicationContext()).setTitle("Select Image")
.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), "Item Selected: " + item, Toast.LENGTH_SHORT).show();
}
}).show();
}
});
}
}
下面的代碼是適配器類
CameraPickAdapter
public class CameraPickAdapter extends ArrayAdapter<String> {
private List<Integer> images;
public CameraPickAdapter(Context context, List<String> items, List<Integer> images) {
super(context, android.R.layout.select_dialog_item, items);
this.images = images;
}
public CameraPickAdapter(Context context, String[] items, Integer[] images) {
super(context, android.R.layout.select_dialog_item, items);
this.images = Arrays.asList(images);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(android.R.id.text1);
textView.setCompoundDrawablesWithIntrinsicBounds(images.get(position), 0, 0, 0);
textView.setCompoundDrawablePadding(
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 12, getContext().getResources().getDisplayMetrics()));
return view;
}
}
我不知道爲什麼我沒有收到。我想這將是簡單的,但我發現它很難啃的骨頭
什麼是錯誤? – 2014-09-25 13:03:06
錯誤是:android.view.WindowManager $ BadTokenException:無法添加窗口 - 標記null不適用於應用程序 – anand 2014-09-25 13:13:16