gpt4 book ai didi

java - 如何使用 setMultiChoiceItems 等复选框列表创建自定义对话框,接收和发送数据?

转载 作者:行者123 更新时间:2023-11-29 23:09:48 24 4
gpt4 key购买 nike

我需要使用类似 setMultiChoiceItems 样式的对话框但包含更多信息,所以我使用自定义布局。

我的布局包含一个图像字段、另一个标题字段、另一个描述字段和另一个来自复选框的字段。

enter image description here

我已经设法让它像这样工作了:

enter image description here

我知道我需要使用 setAdapter 而不是 setMultiChoiceItems。到目前为止一切顺利。

我的问题是如何打开已经选择了一些选项的对话框?稍后接收所有选定的项目以发送到服务器?

使用 setMultiChoiceItems 我可以让它像这样工作:

enter image description here

但我似乎无法使用自定义布局。

这是我的代码:

##### ArrayList - ALItensValues #####

public class ALItensValues {
private int icon;
private String value;
private String titulo;
private String descricao;

public ALItensValues(int icon, String value, String titulo, String descricao) {
this.icon = icon;
this.value = value;
this.titulo = titulo;
this.descricao = descricao;
}

int getIcon() { return icon; }
String getValue() { return value; }
String getTitulo() { return titulo; }
String getDescricao() { return descricao; }
}

##### 适配器 - ALItensValuesAdapter #####

private class ALItensValuesAdapter extends BaseAdapter {

private ArrayList<ALItensValues> mData;
private Context mContext;

ALItensValuesAdapter(ArrayList<ALItensValues> aldata, Context context) {
this.mData = aldata;
this.mContext = context;
}

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

@Override
public Object getItem(int position) {
return null;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = Objects.requireNonNull(mInflater).inflate(R.layout.multipleselectcustom_lines, null);
}

ImageView icon = convertView.findViewById(R.id.imgvicon);
icon.setImageResource(mData.get(position).getIcon());
TextView titulo = convertView.findViewById(R.id.txtv_titulo);
titulo.setText(mData.get(position).getTitulo());
TextView decricao = convertView.findViewById(R.id.txtv_desc);
decricao.setText(mData.get(position).getDescricao());
CheckBox checkbox = convertView.findViewById(R.id.chkbox);

convertView.setOnClickListener(v -> {
checkbox.toggle();
});

return convertView;
}
}

#### Layout Itens - R.layout.multipleselectcustom_lines ####

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:gravity="center">

<ImageView
android:id="@+id/imgvicon"
tools:srcCompat="@tools:sample/avatars"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
android:adjustViewBounds="true"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_weight="1">
<TextView
android:text="Titulo"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/txtv_titulo"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp" android:textSize="18sp"/>
<TextView
android:text="Descrição"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/txtv_desc"
android:layout_marginLeft="8dp" android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp" android:visibility="visible" android:layout_marginTop="3dp"/>
</LinearLayout>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/chkbox"
android:layout_marginEnd="8dp" android:layout_marginRight="8dp"
android:layout_marginTop="8dp" android:layout_marginBottom="8dp"
/>
</LinearLayout>

#### Activity 类####

public class FormsMultipleSelectCustomJava extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multipleselectcustom);

// I get the items that should be selected separated by commas.
// And I do the following to transform into array,
// but I do not know how to use it in dialog.:

String selectItensServer = "0b2,0e5,0f6,0h8";
String[] selectInitItens = selectItensServer.split(",");

// List of items that should appear in the dialog.

ArrayList<ALItensValues> AlertDialogitemList = new ArrayList<>();
AlertDialogitemList.add(new ALItensValues(R.drawable.atrib_bicycle, "0a1", "Test 1", "Lorem ipsum dolor sit amet"));
AlertDialogitemList.add(new ALItensValues(R.drawable.atrib_btc, "0b2", "Test 2", "Duis at ullamcorper quam"));
AlertDialogitemList.add(new ALItensValues(R.drawable.atrib_car, "0c3", "Test 3", "Aenean aliquam sit amet nibh eget bibendum"));
AlertDialogitemList.add(new ALItensValues(R.drawable.atrib_credit_card, "0d4", "Test 4", "Sed a est vel velit rutrum porta"));
AlertDialogitemList.add(new ALItensValues(R.drawable.atrib_gamepad, "0e5", "Test 5", "Phasellus pellentesque arcu et fermentum tempus"));
AlertDialogitemList.add(new ALItensValues(R.drawable.atrib_gift, "0f6", "Test 6", "Integer arcu leo, consequat eget tempus sed"));
AlertDialogitemList.add(new ALItensValues(R.drawable.atrib_paypal, "0g7", "Test 7", "Pellentesque habitant morbi tristique senectus"));
AlertDialogitemList.add(new ALItensValues(R.drawable.atrib_taxi, "0h8", "Test 8", "Proin iaculis, velit ac consectetur bibendum, orci nisi scelerisque"));
AlertDialogitemList.add(new ALItensValues(R.drawable.atrib_utensils, "0i9", "Test 9", "Ut ullamcorper nisi dui, et sagittis ante mollis vitae"));
AlertDialogitemList.add(new ALItensValues(R.drawable.atrib_whatsapp, "k10", "Test 10", "Donec rhoncus magna in mauris ultricies"));


// Button that opens the dialog.
Button btnopendialog = findViewById(R.id.btnopendialog);
btnopendialog.setOnClickListener(v -> {
AlertDialog.Builder dialogbuider = new AlertDialog.Builder(context);
dialogbuider.setCancelable(false);
dialogbuider.setTitle("Itens");
ALItensValuesAdapter mAdapter = new ALItensValuesAdapter(ALitensvalues, context);
dialogbuider.setAdapter(mAdapter, (dialog, which) -> {
// I do not know what to do here.
// But I think it should be here that I start the selected items.
});
dialogbuider.setPositiveButton("OK", (dialogInterface, which) -> {
// And once selected, I would like to display in textview the items that are currently selected.
});
dialogbuider.setNeutralButton("Clear", (dialogInterface, which) -> {
// Here should clear all checkboxes
});
AlertDialog dialog = dialogbuider.create();
ListView listView = dialog.getListView();
listView.setDivider(new ColorDrawable(Color.GRAY));
listView.setDividerHeight(2);
dialog.show();
});

//Button to send to server.
findViewById(R.id.btnsend).setOnClickListener(v -> {
//I need to know how to retrieve the selected items.
});
}

最佳答案

正如 Le Thanh Tan 所提到的,接口(interface)是必经之路。

  1. 创建MyInterface.java文件并添加这段代码

    public interface MyInterface {
    void onItemChecked(int position);
    }
  2. 如下更新您的 ALItensValues 类

    public class ALItensValues {
    private int icon;
    private String value;
    private String titulo;
    private String descricao;
    private Boolean isChecked;

    public ALItensValues(int icon, String value, String titulo, String descricao, Boolean isChecked) {
    this.icon = icon;
    this.value = value;
    this.titulo = titulo;
    this.descricao = descricao;
    this.isChecked = isChecked;
    }

    int getIcon() { return icon; }
    String getValue() { return value; }
    String getTitulo() { return titulo; }
    String getDescricao() { return descricao; }
    Boolean getChecked() { return isChecked; }
    void setChecked(Boolean isChecked) {
    this.isChecked = isChecked;
    }
    }
  3. 现在您的 Activity 将是

    public class FormsMultipleSelectCustomJava extends AppCompatActivity {

    String selectItensServer;
    String[] selectInitItens;
    ArrayList<ALItensValues> AlertDialogitemList = new ArrayList<>();
    ArrayList<ALItensValues> selectedItemsList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_multipleselectcustom);

    selectItensServer = "0b2,0e5,0f6,0h8";
    selectInitItens = selectItensServer.split(",");

    AlertDialogitemList.add(new ALItensValues(R.drawable.atrib_bicycle, "0a1", "Test 1", "Lorem ipsum dolor sit amet", true));
    AlertDialogitemList.add(new ALItensValues(R.drawable.atrib_btc, "0b2", "Test 2", "Duis at ullamcorper quam", true));
    AlertDialogitemList.add(new ALItensValues(R.drawable.atrib_car, "0c3", "Test 3", "Aenean aliquam sit amet nibh eget bibendum", false));
    AlertDialogitemList.add(new ALItensValues(R.drawable.atrib_credit_card, "0d4", "Test 4", "Sed a est vel velit rutrum porta", true));
    AlertDialogitemList.add(new ALItensValues(R.drawable.atrib_gamepad, "0e5", "Test 5", "Phasellus pellentesque arcu et fermentum tempus", false));
    AlertDialogitemList.add(new ALItensValues(R.drawable.atrib_gift, "0f6", "Test 6", "Integer arcu leo, consequat eget tempus sed", true));
    AlertDialogitemList.add(new ALItensValues(R.drawable.atrib_paypal, "0g7", "Test 7", "Pellentesque habitant morbi tristique senectus", false));
    AlertDialogitemList.add(new ALItensValues(R.drawable.atrib_taxi, "0h8", "Test 8", "Proin iaculis, velit ac consectetur bibendum, orci nisi scelerisque", true));
    AlertDialogitemList.add(new ALItensValues(R.drawable.atrib_utensils, "0i9", "Test 9", "Ut ullamcorper nisi dui, et sagittis ante mollis vitae", false));
    AlertDialogitemList.add(new ALItensValues(R.drawable.atrib_whatsapp, "k10", "Test 10", "Donec rhoncus magna in mauris ultricies", true));

    Button btnopendialog = findViewById(R.id.btnopendialog);
    btnopendialog.setOnClickListener(v -> {
    AlertDialog.Builder dialogbuider = new AlertDialog.Builder(context);
    dialogbuider.setCancelable(false);
    dialogbuider.setTitle("Itens");
    ALItensValuesAdapter mAdapter = new ALItensValuesAdapter(ALitensvalues, context, new MyInterface() {
    @Override
    public void onItemChecked(int position)
    {
    AlertDialogitemList.get(position).setChecked(!AlertDialogitemList.get(position).getChecked())
    }
    });
    dialogbuider.setAdapter(mAdapter, (dialog, which) -> {

    });
    dialogbuider.setPositiveButton("OK", (dialogInterface, which) -> {

    });
    dialogbuider.setNeutralButton("Clear", (dialogInterface, which) -> {
    });
    AlertDialog dialog = dialogbuider.create();
    ListView listView = dialog.getListView();
    listView.setDivider(new ColorDrawable(Color.GRAY));
    listView.setDividerHeight(2);
    dialog.show();
    });


    findViewById(R.id.btnsend).setOnClickListener(v -> {
    for(ALItensValues item : AlertDialogitemList) {
    if(item.isChecked()) {
    selectedItemsList.add(item);
    }
    }
    // selected items can be sent to server
    });
    }
    }
  4. 你的适配器

    private class ALItensValuesAdapter extends BaseAdapter {
    private ArrayList<ALItensValues> mData;
    private Context mContext;
    private MyInterface interface;

    ALItensValuesAdapter(ArrayList<ALItensValues> aldata, Context context, MyInterface interface) {
    this.mData = aldata;
    this.mContext = context;
    this.interface = interface;
    }

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

    @Override
    public Object getItem(int position) {
    return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
    LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    convertView = Objects.requireNonNull(mInflater).inflate(R.layout.multipleselectcustom_lines, null);
    }

    ImageView icon = convertView.findViewById(R.id.imgvicon);
    icon.setImageResource(mData.get(position).getIcon());
    TextView titulo = convertView.findViewById(R.id.txtv_titulo);
    titulo.setText(mData.get(position).getTitulo());
    TextView decricao = convertView.findViewById(R.id.txtv_desc);
    decricao.setText(mData.get(position).getDescricao());
    CheckBox checkbox = convertView.findViewById(R.id.chkbox);
    if(mData.get(position).getChecked()) {
    checkbox.setChecked(true);
    } else {
    checkbox.setChecked(false);
    }
    convertView.setOnClickListener(v -> {
    this.interface.onItemChecked(position);
    checkbox.toggle();
    });
    return convertView;
    }
    }

希望这能奏效。

关于java - 如何使用 setMultiChoiceItems 等复选框列表创建自定义对话框,接收和发送数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56160093/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com