2016-08-11 104 views
0

這是我爲我的AlertDialog到目前爲止的代碼:如何將自定義列表視圖添加到Android中的AlertDialog?

public class FriendOptionsDialogFragment extends DialogFragment { 

    public static FriendOptionsDialogFragment newInstance(String firstName) { 
     FriendOptionsDialogFragment options = new FriendOptionsDialogFragment(); 
     Bundle bundle = new Bundle(); 
     bundle.putString("firstName", firstName); 
     options.setArguments(bundle); 
     return options; 
    } 

    @NonNull 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_friend_options, null); 
     Bundle extras = getArguments(); 
     String friendName = extras.getString("firstName"); 
     FriendOptionsAdapter adapter = new FriendOptionsAdapter(getActivity(), friendName); 

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setCancelable(true); 
     builder.setView(view); 
     builder.setAdapter(adapter, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 

      } 
     }); 

     return builder.create(); 
    } 
} 

我想添加自定義適配器和佈局,以我的AlertDialog的列表視圖。我用的是AlertDialog.Builder對象的方法setAdapter並得到以下錯誤的堆棧跟蹤:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference 
    at com.hb.birthpay.adapter.FriendOptionsAdapter.getView(FriendOptionsAdapter.java:67) 

這是錯誤發生在我的自定義適配器行:

holder.friendOptionImageView.setImageResource(mFriendOptionImageId[position]); 

我可能做這個錯誤,但是我需要知道如何將自定義列表視圖添加到AlertDialog。

+0

請發佈FriendOptionsAdapter源代碼 – Lino

回答

0
AlertDialog.Builder builder = new AlertDialog.Builder(context); 

final View customView = View.inflate(context, R.layout.custom_dialog_view, null); 
builder.setView(customView); 
///builder.setCancelable(false);   //====> Disable the outside touch from the dialog window. 

final AlertDialog backDialog = builder.create(); 

backDialog.setTitle(....string...); 

backDialog.setButton(AlertDialog.BUTTON_NEGATIVE, getResources().getString(R.string.text_cancel), 
new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     backDialog.cancel(); 
    } 
}); 
backDialog.setCanceledOnTouchOutside(false); 
backDialog.show(); 

CustomAdapter adapter = new CustomAdapter(context, ...arrayValues...); 

ListView listView = (ListView) customView 
     .findViewById(R.id.list_view); 
listView.setAdapter(adapter); 
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, 
    int location, long id) { 

     // handle list view item click. 
     //you can access values using parent.getItemAtPosition(location).toString());            

     backDialog.dismiss(); 
    } 
}); 

適配器類別:

public class CustomAdapter extends ArrayAdapter<String> { 

public CustomAdapter(Context context, String[] arrayValues) { 
    super(context, android.R.layout.simple_list_item_1); 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    if (convertView == null) { 
     convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1, parent, false); 
    } 

    String value = getItem(position); 

    //do your work. 

    return super.getView(position, convertView, parent); 
}} 

custom_dialog_view:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 


    <ListView 
     android:id="@+id/list_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_marginLeft="@dimen/dimen_10" 
     android:layout_marginRight="@dimen/dimen_10" 
     android:cacheColorHint="@android:color/transparent" 
     > 
    </ListView> 

</RelativeLayout> 

希望這將幫助... :)謝謝。

+0

答案的第一個代碼部分在哪裏?我在onCreateDialog()方法中猜測,對吧? –

相關問題