在ListView内的按钮上单击后显示自定义对话框

时间:2018-08-25 11:09:56

标签: android listview button dialog

我的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;
    }
}

Custom Dialog的XML:

<?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 :(得分:1)

通过设置按钮的文本,标题和消息,根据需要自定义对话框,如下所示:

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();
    }
});