带有自定义列表视图的警报对话框 - 不触发点击事件

Alertdialog with a custom listview - click event is not triggered

我的目标是创建一个显示选项列表的提醒对话框,每个选项都由一个图像和一个文本表示。用户应该在警报对话框中做出选择并进行验证。 我决定构建这个带有自定义 ListView 的警报对话框。到目前为止,没问题,但我找不到进行选择的方法。 我想创建一个点击事件或复选框,但这些选项中的 none 似乎有效。对我来说最优雅的方法是在列表视图的特定行上单击事件,这是我的代码。现在,如果我点击,它不会做任何事情。 非常感谢您的帮助。

MainActivity

    public class MainActivity extends AppCompatActivity implements 
    View.OnClickListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b1 = (Button) this.findViewById(R.id.button);
        b1.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = 
    (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.row_item,null);

        ListView LV = (ListView)row.findViewById(R.id.Listview_card);

        Resources res = getApplicationContext().getResources();
        String[] names = res.getStringArray(R.array.image_name);
        int[] images =
    {R.drawable.image01,R.drawable.image02,R.drawable.image03,  
    R.drawable.image04};

        ArrayList<singleRow> array_list = new ArrayList<>();

        for (int i = 0; i < names.length; i++){
            array_list.add(new singleRow(names[i],images[i]));
        }

        CustomAdapter adapter = new CustomAdapter(getApplicationContext(), 
    array_list);
        LV.setAdapter(adapter);
        LV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                      @Override
                                      public void onItemClick(AdapterView<?> 
    parent, View view, int position, long id) {
                                         Toast.makeText(getApplicationContext(), 
    "coucou2", Toast.LENGTH_SHORT);
                                      }
                                  });

        builder.setView(row);
        builder.setPositiveButton("Validate Choice",new 
    DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

            Toast.makeText(getApplicationContext(),"coucou3",Toast.LENGTH_LONG);
            }
        });

        AlertDialog dialog = builder.create();

        dialog.show();
    }}

activity_main xml
    <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"
    tools:context="com.replics.customlist.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        tools:layout_editor_absoluteX="49dp"
        tools:layout_editor_absoluteY="44dp" /
    </android.support.constraint.ConstraintLayout>

CustomAdapter
    public class CustomAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<singleRow> array_list;

    CustomAdapter(Context mContext, ArrayList<singleRow> List_Sent) {
        this.mContext = mContext;
        this.array_list = List_Sent;
    }

    @Override
    public View getView(int i, View View, ViewGroup Viewgroup) {
        LayoutInflater inflater = 
    (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.custom_listview,Viewgroup,false);

        //find objet in each line
        TextView TV = (TextView)row.findViewById(R.id.textview_custo_LV);
        ImageButton IM = 
    (ImageButton)row.findViewById(R.id.image_button_custo_LV);

        //get the position
        singleRow temp_obj = array_list.get(i);

        //set each line
        TV.setText(temp_obj.name);
        IM.setImageResource(temp_obj.image);

        //give the right properties to the image
        IM.setScaleType(ImageView.ScaleType.FIT_XY);
        IM.setAdjustViewBounds(true);
        IM.setPadding(0, 0, 0, 0);

        //return final line
        return row;
    }

    @Override
    public Object getItem(int i) {
        return array_list.get(i);
    }

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

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

    }

row_item xml
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="List all images" />
    <ListView
        android:id="@+id/Listview_card"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />
    </RelativeLayout>

singleRow
    public class singleRow {
    String name;
    int image;
    public singleRow(String name, int image) {
        this.name = name;
        this.image = image;
         }
    }

custom_listview xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageButton
        android:id="@+id/image_button_custo_LV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/image01" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id = "@+id/textview_custo_LV"
        android:layout_weight="1"
        android:text="image01" />

    </LinearLayout>

所以您想在列表中添加复选框?解决方法很简单

在你的对话框中,列表适配器在你的模型中再添加一个变量class如果复选框被选中则保存

然后在您的适配器中将监听器添加到您的复选框中,就像这样

 mQuestionCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        modelList.get(getAdapterPosition()).setChecked(true);
                    } else {
                        modelList.get(getAdapterPosition()).setChecked(false);
                    }
                }
            });

基本上它所做的是修改你当前的项目对象,无论它是否被检查,并获取值只是循环你当前的模型列表并通过我们的布尔变量检查它是否被检查较早制作。

像这样:

for (int i = 0; i < modelList.size(); i++) { //loop through your data list
            QuestionerKerusakanModel current = modelList.get(i);
            if (current.isChecked()) { //check if the item is checked or not
                tempGejala.add(current); //adding it to new filtered List or modify the object however you want
            }
        }

而且你应该开始使用 RecyclerView 而不是 ListView,它的原理相同只是更好。

我的问题已经解决了。

我所做的是修改列表视图的每一行,使每一行都包含一个复选框。 单击验证按钮时,我会阅读用户答案并确定它是否是有效答案。 然而,我现在有另一个问题,但我发布了一个新问题,因为这两个问题并没有真正相关。

这是我的自定义行布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageButton
    android:id="@+id/image_button_custo_LV"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:src="@drawable/wonder01" />
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <CheckBox
        android:id="@+id/checkBox_custo_LV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Choisir" />
    <TextView
        android:id="@+id/textview_custo_LV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Wonder1" />
</LinearLayout>

这是我的列表视图布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
    <TextView
            android:id="@+id/Text_Expl_List"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:text="Choose one card: "
            android:textSize="18sp" />
        <ListView
            android:id="@+id/Listview_card"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true" />

这是我的 activity 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
tools:context="com.boardgamereplics.customlistforstackflow.MainActivity">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

单行javaclass:

public class singleRow {
String name;
int image;
boolean ischecked;


public singleRow(String name, int image, boolean ischecked) {
    this.name = name;
    this.image = image;
    this.ischecked = ischecked;
}

自定义适配器javaclass:

public class CustomAdapter extends BaseAdapter { //implements View.OnClickListener {

private Context mContext;
private ArrayList<singleRow> array_list;
//TODO change l'objet singlerow par card et arrange toi pour passer les cartes que tu veux.

CustomAdapter(Context mContext, ArrayList<singleRow> List_Sent) {
    this.mContext = mContext;
    this.array_list = List_Sent;
}

@Override
public View getView(int i, View View, ViewGroup Viewgroup) {
    LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = inflater.inflate(R.layout.one_line_model,null);

    //find objet in each line
    TextView TV = (TextView)row.findViewById(R.id.textview_custo_LV);
    ImageButton IM = (ImageButton)row.findViewById(R.id.image_button_custo_LV);
    CheckBox CB = (CheckBox)row.findViewById(R.id.checkBox_custo_LV);

    //get the position
    singleRow temp_obj = array_list.get(i);

    //set each line
    TV.setText(temp_obj.name);
    IM.setImageResource(temp_obj.image);
    CB.setChecked(false);

    //give the right properties to the image
    IM.setScaleType(ImageView.ScaleType.FIT_XY);
    IM.setAdjustViewBounds(true);
    IM.setPadding(0, 0, 0, 0);

    //return final line
    return row;
}

@Override
public Object getItem(int i) {
    return array_list.get(i);
}

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

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

主要Activity:

public class MainActivity extends AppCompatActivity {
    Button b1 = null;
    Context ctx = null;
    TextView T_pop1 = null;
    TextView T_pop2 = null;
    AlertDialog.Builder builder_pop2 = null;
    AlertDialog.Builder builder_pop1 = null;
    // ListView LV_pop1 = null;
    // ListView LV_pop2 = null;
    Button Validate_btn_pop1 = null;
    Button Validate_btn_pop2 = null;

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

    ctx = this;
    b1 = (Button) this.findViewById(R.id.button);

    b1.setOnClickListener(new android.view.View.OnClickListener() {
                              @Override
                              public void onClick(View arg0) {
                                  displayAlert();
                              }
                          }
    );

}

public void displayAlert() {
    builder_pop1 = new AlertDialog.Builder(ctx);

    LayoutInflater inflater_pop1 = (LayoutInflater) ctx.getSystemService(LAYOUT_INFLATER_SERVICE);
    View listview_lay_pop1 = inflater_pop1.inflate(R.layout.listview_lay, null);

    //identify objet in second alertdialog
    final ListView LV_pop1 = (ListView) listview_lay_pop1.findViewById(R.id.Listview_card);

    //populate listview
    Resources res = getApplicationContext().getResources();
    String[] names = res.getStringArray(R.array.wonders_name1);
    int[] images = {R.drawable.wonder01, R.drawable.wonder02, R.drawable.wonder03, R.drawable.wonder04};

    ArrayList<singleRow> array_list_pop1 = new ArrayList<>();

    for (int i = 0; i < names.length; i++) {
        array_list_pop1.add(new singleRow(names[i], images[i], false));
    }

    CustomAdapter adapter_pop1 = new CustomAdapter(getApplicationContext(), array_list_pop1);
    LV_pop1.setAdapter(adapter_pop1);

    builder_pop1.setView(listview_lay_pop1);
    builder_pop1.setPositiveButton("OK", null);
    final AlertDialog dialog_pop1 = builder_pop1.create();

    dialog_pop1.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog_ins_pop1) {
            //Overriding the handler immediately after show is probably a better approach than OnShowListener as described below
            T_pop1 = (TextView) ((AlertDialog) dialog_pop1).findViewById(R.id.Text_Expl_List);
            Validate_btn_pop1 = ((AlertDialog) dialog_pop1).getButton(AlertDialog.BUTTON_POSITIVE);
            Validate_btn_pop1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v_pop1) {
                    Integer choice = 0;
                    Integer counter_ischecked = 0;
                    for (int i = 0; i < 4; i++) {
                        CheckBox CBF_pop1 = (CheckBox) LV_pop1.getChildAt(i).findViewById(R.id.checkBox_custo_LV);
                        boolean isSelected = CBF_pop1.isChecked();
                        if (isSelected == true) {
                            choice = choice + i;
                            counter_ischecked++;
                        }
                    }
                    if (counter_ischecked == 0) {
                        T_pop1.setText("Choose one card");
                    }
                    if (counter_ischecked == 1) {
                        b1.setText("Success");
                        dialog_pop1.dismiss();
                    }
                    if (counter_ischecked != 0 && counter_ischecked != 1) {
                        T_pop1.setText("Choose only one card");
                    }

                }
            }
            );
        }
    });
    dialog_pop1.show();
}