Я работаю над 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();
}
});
}
}
код ниже класс Adapter
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;
}
}
Я не знаю, почему я не получаю. Я думал, что это будет просто, но я нахожу, что это крепкий орешек
Какая ошибка? –
Ошибка: android.view.WindowManager $ BadTokenException: Не удалось добавить окно - токен null не для приложения – anand