我的目标是创建一个显示选项列表的alertdialog,每个选项都由图像和文本表示。用户应该在alertdialog中做出选择并进行验证。 我决定用它里面的自定义ListView构建这个alertdialog。到目前为止,没有问题,但我找不到一种方法来做选择。 我想创建一个单击事件或复选框,但这些选项似乎都不起作用。对我来说最优雅的方式是列表视图的特定行上的click事件,这是我的代码。现在,如果我点击它,它什么都不做。 非常感谢您的帮助。
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>
答案 0 :(得分:0)
所以你想在列表中有复选框吗?解决方案很简单
在对话框内,列表适配器在模型类中添加一个变量,以便在选中复选框时保存
并在您的适配器中添加侦听器,如此
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,它只有更好的原则。
答案 1 :(得分:0)
我已经解决了我的问题。
我所做的是修改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>
这是我的listview布局:
<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" />
这是我的活动布局:
<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>
单行java类:
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;
}
自定义适配器java类:
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();
}
主要活动:
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();
}