показать пользовательский диалог после нажатия кнопки в ListView - PullRequest
0 голосов
/ 25 августа 2018

У меня есть Button в моем ListView Adapter.Я хотел бы показать Custom Dialog после того, как пользователь нажмет на этот Button.

Как я могу это сделать?

Код Adapter:

package ...
import ...
public class UserBooksListAdapter extends BaseAdapter {

    private List<UserBook> items;
    private LayoutInflater inflater;

    public UserBooksListAdapter(Activity context, List<UserBook> items) {
        super();
        this.inflater = LayoutInflater.from(context);
        this.items = items;
    }

    @Override
    public int getCount() {
        return this.items.size();
    }

    @Override
    public UserBook getItem(int position) {
        return this.items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return this.items.get(position).hashCode();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        final UserBook item = items.get(position);
        if(convertView==null){
            vi=inflater.inflate(R.layout.userbook_info, null);
        }

        //set the elements in the UI for this book
        TextView bookName = (TextView) vi.findViewById(R.id.bookName);
        TextView booksDates = (TextView) vi.findViewById(R.id.bookDates);
        Button LibrariesButton = (Button) vi.findViewById(R.id.LibrariesButton);
        final Switch isActive = (Switch) vi.findViewById(R.id.isActive);

        //check if the user still get notification for this book (= the book is active).
        if (item.getACTIVE().equals("1")){
            //the book is active
            isActive.setChecked(true);
            //coloring the the to white
            bookName.setTextColor(0xffffffff);
            booksDates.setTextColor(0xffffffff);
        } else {
            //the book is not active
            isActive.setChecked(false);
            //coloring the text to gray
            bookName.setTextColor(0Xff838383);
            booksDates.setTextColor(0Xff838383);
        }

        //put the values to the TextViews
        bookName.setText(item.getBook().getName());
        booksDates.setText(item.getSTART_DATE() + " - " + item.getEND_DATE());


        //if the user click on Library Button:
        LibrariesButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder mBuilder = new AlertDialog.Builder(UserBooksListAdapter.this);
            }
        });


        return vi;
    }
}

XML Custom Dialog:

<?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">

    <TextView
        android:id="@+id/tv"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="TextView"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="16dp" />

    <Button
        android:id="@+id/but"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="@+id/tv"
        app:layout_constraintStart_toStartOf="@+id/tv"
        app:layout_constraintTop_toBottomOf="@+id/tv" />
</android.support.constraint.ConstraintLayout>

1 Ответ

0 голосов
/ 25 августа 2018

Настройте диалоговое окно так, как вам нравится, установив текст кнопок, заголовок и сообщение следующим образом:

LibrariesButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        AlertDialog.Builder mBuilder = new AlertDialog.Builder(this);
        mBuilder.setTitle("title");
        mBuilder.setMessage("message");
        mBuilder.setCancelable(false);
        mBuilder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                // your code here
            }
        });
        mBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                dialog.dismiss();
            }
        });
        mBuilder.show();
    }
});
...