2014-09-25 78 views
0

我正在使用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; 
    } 
} 

我不知道爲什麼我沒有收到。我想這將是簡單的,但我發現它很難啃的骨頭

+0

什麼是錯誤? – 2014-09-25 13:03:06

+0

錯誤是:android.view.WindowManager $ BadTokenException:無法添加窗口 - 標記null不適用於應用程序 – anand 2014-09-25 13:13:16

回答

2

變化getApplicationContext()MainActivity.this即更改

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

new AlertDialog.Builder(MainActivity.this).setTitle("Select Image") 
        .setAdapter(adapter, new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int item) { 
          Toast.makeText(MainActivity.this, "Item Selected: " + item, Toast.LENGTH_SHORT).show(); 
         } 
        }).show(); 
+0

我沒有錯過部分..我編輯了我的問題 – anand 2014-09-25 13:15:25

+0

@anand然後更改getApplicationContext() MainActivity.this參見編輯答案。 – 2014-09-25 13:18:17

+0

我已經做了那部分......它沒有考慮..在編輯器中給出錯誤 – anand 2014-09-25 13:20:54

0

我想申請之前有同樣的問題定製佈局AlertDialog當我將其更改爲對話框它工作的很好請在這裏看看我的Question

1

不喜歡這個 -

new AlertDialog.Builder(MainActivity.this) 

它可能爲你工作。

+0

..我已經做了這部分..它不工作......它不只是接受 – anand 2014-09-25 13:17:15