我的Recycler View在设置适配器时要求List Adapter参数

时间:2016-08-02 14:59:30

标签: java android android-fragments android-recyclerview android-adapter

我创建了一个新的Dialog片段,以便在here的警告对话框构建器中显示我的列表视图。当我使用普通列表视图而不是回收器视图时,我的代码已经正常工作但是只要我将其更改为回收器视图,它就会要求我在将适配器设置为对话框构建器时强制转换List Adapter参数。

这是我的DialogActivity

public class DialogActivity extends DialogFragment {

private RecyclerView recyclerView;
private FoodList adapter;
private List<Food> foodList = new ArrayList<>();
private GoogleMap mMap;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    adapter = new FoodList(foodList);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   View rootView = inflater.inflate(R.layout.flist, container, false);
   recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
   LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
   recyclerView.setLayoutManager(linearLayoutManager);
   recyclerView.setAdapter(adapter);
   return rootView;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
    alertDialogBuilder.setAdapter(adapter, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int i) {

        }
    });

    alertDialogBuilder.setCancelable(false)
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });



    return alertDialogBuilder.create();

}

}

这是我的适配器

public class FoodList extends RecyclerView.Adapter<FoodList.MyViewHolder> {

Context context;
private List<Food> foodList;

public FoodList(List<Food> foodList) {
    this.foodList = foodList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.examplelistview, parent, false);
    context = parent.getContext();
    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Food food = foodList.get(position);
    holder.txtfield1.setText(food.getfield1());
    holder.txtfield2.setText(food.getfield2());
    holder.txtfield3.setText(food.getfield3());
    holder.txtfield4.setText(food.getfield4());

    /*
        Animation animation = AnimationUtils.loadAnimation(context, R.anim.push_up_in);
        animation.setFillEnabled(true);
        animation.setFillAfter(true);
        view.startAnimation(animation);
    */
}

@Override
public int getItemCount() {
    return foodList.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView txtfield1, txtfield2, txtfield3, txtfield4;

    public MyViewHolder(View view) {
        super(view);
        txtfield1 = (TextView) view.findViewById(R.id.txtfield1);
        txtfield2 = (TextView) view.findViewById(R.id.txtfield2);
        txtfield3 = (TextView) view.findViewById(R.id.txtfield3);
        txtfield4 = (TextView) view.findViewById(R.id.txtfield4);
    }
}

这是我的flist.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical" />

我在主要活动中调用对话框片段,如此

private void startNextActivity() {
    FragmentManager fm = getFragmentManager();
    DialogActivity testDialog = new DialogActivity();
    testDialog.setRetainInstance(true);
    testDialog.show(fm, "fragment_name");
}

在对话框构建器中设置适配器时,它给出了一个错误,即给出ListAdapter的参数

alertDialogBuilder.setAdapter((ListAdapter) adapter, new DialogInterface.OnClickListener() 

但在这种情况下,它也给我一个错误,我的自定义适配器无法在列表适配器中转换。我知道我在做一些大错,但请任何人帮助我! 在此先感谢

0 个答案:

没有答案