如何使用RecyclerView项填充AlertDialog

时间:2019-05-08 12:23:35

标签: android android-recyclerview alertdialog

我是android编程的初学者。我创建了一个recyclerview,它显示了MySQL数据库中的项目。但是我希望这些项目显示在警报对话框中而不是片段中。我不知道该怎么做。

这是我的代码:

public class profileFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {

    private SpotsAdapter adapter;
    private RecyclerView recyclerView;
    private ArrayList<Spots> spotsList;
    private Context context;
    private SwipeRefreshLayout swipeLayout;
    private int refresh_count =0;
    private TextView TVspot;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_profile, container, false);
        swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout);
        swipeLayout.setOnRefreshListener(this);

        context = view.getContext();

        return view;
    }

    @Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);



        spotsList = new ArrayList<Spots>();

        recyclerView = view.findViewById(R.id.recy);
        LinearLayoutManager lm = new LinearLayoutManager(context);
        lm.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(lm);
        recyclerView.setHasFixedSize(false);
        recyclerView.setAdapter(adapter);

        Api api = RetrofitClient.getREtrofit().create(Api.class);
        Call<ArrayList<Spots>> call = api.getSpotslList();

        call.enqueue(new Callback<ArrayList<Spots>>() {
            @Override
            public void onResponse(retrofit2.Call<ArrayList<Spots>> call, Response<ArrayList<Spots>> response) {
                spotsList = response.body();
                Log.e("REsult", (spotsList.get(0)).toString());
                adapter = new SpotsAdapter(spotsList, context);


                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                Log.e("sid", adapter.valueOfSid());
                TVspot= view.findViewById(R.id.parkingTV);
                TVspot.setText("Your parking spot is:" + adapter.valueOfSid());

            }

            @Override
            public void onFailure(retrofit2.Call<ArrayList<Spots>> call, Throwable t) {
                Log.e("FAil", String.valueOf(t.toString()));
            }
        });



    }

    @Override
    public void onRefresh() {

        swipeLayout.setRefreshing(false);
        Api api = RetrofitClient.getREtrofit().create(Api.class);
        Call<ArrayList<Spots>> call = api.getSpotslList();

        call.enqueue(new Callback<ArrayList<Spots>>() {
            @Override
            public void onResponse(retrofit2.Call<ArrayList<Spots>> call, Response<ArrayList<Spots>> response) {
                spotsList = response.body();
                Log.e("REsult", String.valueOf(spotsList.size()));
                adapter = new SpotsAdapter(spotsList, context);

                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                TVspot.setText("Your parking spot is:" + adapter.valueOfSid());
            }

            @Override
            public void onFailure(retrofit2.Call<ArrayList<Spots>> call, Throwable t) {
                Log.e("FAil", String.valueOf(t.toString()));
            }
        });

        adapter.notifyDataSetChanged();
        Log.e("FAil", String.valueOf(spotsList.size()));


    }
}

对于将recyclerview元素放入警报对话框中的任何帮助和建议,将深表感谢。

1 个答案:

答案 0 :(得分:1)

您可以通过将DialogFragment扩展到自定义类来创建自定义对话框。 您可以参考此link

退出适配器类

内部适配器类定义接口。

public interface ClickListener{
     void onClickDismiss();
}

创建ClickListener对象,并将DialogFragment实例分配给构造函数中的该对象

ClickListener clickListner = (ClickListner) dialogFragmentInstance;

在recyclerview项目的OnbindViewHolder方法内部的onclickListner中添加此代码以调用接口方法。

clickListner.onClickDismiss();

内部DialogFragment类

 class YourDialog extends DialogFragment implements Adapter.ClickListener{
    ......

    @Override
    public void onClickDismiss(){
    dismiss();
    }
}