Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlertDialog with RecyclerView

I want to create custom dialog with list of some items in my app. Here my code of adapter

Context context; ArrayList statusList;

public MaritalStatusAdapter(Context context, ArrayList<String> statusList){
    this.context = context;
    this.statusList = statusList;
    Logger.msg("Reg", ":" + statusList.get(0));
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(context).inflate(R.layout.spinner_list_item, parent, false);
    Logger.msg("Reg", "here");
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    holder.item.setText(statusList.get(position));
    Logger.msg("Reg", "here");
}

@Override
public long getItemId(int position) {
    return position;
}

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

class ViewHolder extends RecyclerView.ViewHolder {
    @BindView(R.id.item)
    TextView item;

    public ViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
        Logger.msg("Reg", "heee555y");
    }

    @OnClick(R.id.item)
    public void onClick(){
        Logger.msg("Reg", "heeey");
    }
}

I think here all code is fine and it should work as I want. Here my code of dialog builder

AlertDialog.Builder builder = new AlertDialog.Builder(getContext());

            LayoutInflater inflater = getLayoutInflater();
            View dialogView = (View) inflater.inflate(R.layout.custom_alert_dialog, null);

            builder.setView(dialogView);

            RecyclerView rv = (RecyclerView) dialogView.findViewById(R.id.rv);

            MaritalStatusAdapter adapter = new MaritalStatusAdapter(getActivity(), maritalStatusList);
            rv.setAdapter(adapter);

            AlertDialog dialog = builder.create();

            dialog.show();

So, this code is displaying me my custom dialog view with my edit text, which was added in xml file, but do not display anything in recyclerView. I am sure that my xml file is ok. However I do not understand why I cannot see my items in my list.

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@null"
        android:drawableLeft="@drawable/search"
        android:drawablePadding="10dp"
        android:hint="@string/search"
        android:singleLine="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:background="@color/vertical_line"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/search" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view1" />
</android.support.constraint.ConstraintLayout>

At the top my custom_alert_dialog.xml file, but how I said I think here there is no mistake

Can someone help me or share some good tutorial for such activity. It may be small mistake, by the way.))

like image 448
Asset Bekbossynov Avatar asked May 23 '18 07:05

Asset Bekbossynov


2 Answers

I think you should set a LayoutManager to your recyclerView. add this line before setting the adapter:

rv.setLayoutManager(new LinearLayoutManager(getActivity()));
like image 83
Ali Haghighatdoost Avatar answered Nov 04 '22 00:11

Ali Haghighatdoost


If someone will find it useful, the mistake was that my RecyclerView height is 0dp, so I changed it to wrap_content. I thought it will expand to its parent size, as it should be according to ConstraintLayout properties.

like image 2
Asset Bekbossynov Avatar answered Nov 04 '22 02:11

Asset Bekbossynov