【问题标题】:Custom ListView always gets unchecked after scrolling over滚动后自定义 ListView 始终未选中
【发布时间】:2019-02-09 04:51:40
【问题描述】:

我创建了一个使用自定义ListView 的应用。在自定义中,ListViewCheckbox 选项。当我在电话屏幕上向上或向下滚动时,复选框会自动取消选中。你对这个案子有什么解决办法吗?请帮助解决这个问题。谢谢!

customelist.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
   android:orientation="vertical"
   tools:context=".menu_list">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textViewItemName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginStart="10dp"
        android:layout_toEndOf="@+id/buttonBarcode"
        android:fontFamily="serif"
        android:text="Medium Text"
        android:textColor="@android:color/holo_red_dark"
        android:textSize="18sp"
        android:textStyle="bold" />


    <CheckBox
        android:id="@+id/checkboxList"
        android:layout_width="32dp"
        android:layout_height="30dp"
        android:layout_alignParentEnd="true"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginEnd="30dp"
        android:background="@drawable/button_round_green"
        android:textAlignment="center"
        android:textColorHint="@android:color/holo_red_dark"
        android:textColorLink="@android:color/holo_red_dark" />

</RelativeLayout>
</RelativeLayout>

ListAdapter.java

public class listAdapter extends ArrayAdapter<String> {

private final Activity context;
private final String[] index_item;
private final String[] barcode_food;
private final String[] item_name;
private final String[] price_item;

//private TextView textView_name, textView_price;
//private Button button_index, button_barcode;
//private CheckBox checkBoxItem;

public listAdapter(Activity context, String[] index_item, String[] barcode_food, String[] item_name, String[] price_item) {
    super(context, R.layout.activity_menu_list, index_item);

    this.context = context;
    this.index_item = index_item;
    this.barcode_food = barcode_food;
    this.item_name = item_name;
    this.price_item = price_item;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.activity_menu_list,null,true);

    TextView textView_name = (TextView) rowView.findViewById(R.id.textViewItemName);

    final CheckBox checkBoxItem = (CheckBox) rowView.findViewById(R.id.checkboxList);

    textView_name.setText(item_name[position]);

    // Row View got click
    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (checkBoxItem.isChecked()==true){
                checkBoxItem.setChecked(false);

            }
            else checkBoxItem.setChecked(true);
        }
    });



    // Return row view
    return rowView;
}

【问题讨论】:

标签: android listview


【解决方案1】:

你需要修改如下:

private final Activity context;
private final String[] index_item;
private final String[] barcode_food;
private final String[] item_name;
private final String[] price_item;
//add Array 
private final String[] selectedItems;

//private TextView textView_name, textView_price;
//private Button button_index, button_barcode;
//private CheckBox checkBoxItem;

public listAdapter(Activity context, String[] index_item, String[] barcode_food, String[] item_name, String[] price_item) {
    super(context, R.layout.activity_menu_list, index_item);

    this.context = context;
    this.index_item = index_item;
    this.barcode_food = barcode_food;
    this.item_name = item_name;
    this.price_item = price_item;

     // Loop to set all the items are unselected
     for(int i=0;i<index_item.lenght();i++){
        selectedItems[i] = false;
      }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.activity_menu_list,null,true);

    TextView textView_name = (TextView) rowView.findViewById(R.id.textViewItemName);

    final CheckBox checkBoxItem = (CheckBox) rowView.findViewById(R.id.checkboxList);

    textView_name.setText(item_name[position]);

if (selectedItems[position]){
                checkBoxItem.setChecked(true);

            }
            else checkBoxItem.setChecked(false);

    // Row View got click
    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(selectedItems[position]){
                selectedItems[position]= false;
            }else{
                 selectedItems[position]= true;
            }
            notifydatasetchanged
        }
    });



    // Return row view
    return rowView;
}

【讨论】:

  • 谢谢!让我试试看。
【解决方案2】:

您必须维护复选框数组选择,如下所示:

private final Boolean [] buttonCheckedState;

你的构造函数应该是这样的:

public listAdapter(Activity context, String[] index_item, String[]barcode_food, String[] item_name, String[] price_item) {

super(context, R.layout.activity_menu_list, index_item);

this.context = context;
this.index_item = index_item;
this.barcode_food = barcode_food;
this.item_name = item_name;
this.price_item = price_item;
for(int i=0;i<index_item.lenght();i++){
    buttonCheckedState[i] = false;
  }

}

将此代码放入get View:

checkBoxItem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                buttonCheckedState[position]=buttonView.isChecked(); // Set the value of checkbox to maintain its state.
            }
        });


    checkBoxItem .setTag(position); // This line is important. 
   checkBoxItem.setChecked(buttonCheckedState[position]);

【讨论】:

  • 谢谢!现在我的应用越来越迷恋了,可能有问题我会再次检查上面的代码。
  • 您可能忘记了将未选中状态的数组初始化为 false,如上请检查修改后的答案,它可能会解决您的问题
【解决方案3】:

试试这个....我修改了你的代码

try this....



if(rowciew==null){   
ViewHolder viewholder; 
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.activity_menu_list,null,true);

viewholder=new ViewHolder();

viewholder.textView_name = (TextView) rowView.findViewById(R.id.textViewItemName);

viewholder.checkBoxItem = (CheckBox)rowView.findViewById(R.id.checkboxList);

rowView.setTag(viewholder);
}else{
   viewholder=(ViewHolder) rowview.getTag();
}
  textView_name.setText(item_name[position]);

// Row View got click
rowView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (viewholder.checkBoxItem.isChecked()==true){
            viewholder.checkBoxItem.setChecked(false);

        }
        else viewholder.checkBoxItem.setChecked(true);
    }
});

// Return row view
return rowView;

private class ViewHolder {
    TextView textView_name;
    CheckBox checkBoxItem;
}

}

【讨论】:

  • 谢谢。我会试试你的代码,完成后通知你。
【解决方案4】:

类似的东西

    private final Activity context;
    private final String[] index_item;
    private final String[] barcode_food;
    private final String[] item_name;
    private final String[] price_item;
    private boolean[] checkboxes;

    //private TextView textView_name, textView_price;
    //private Button button_index, button_barcode;
    //private CheckBox checkBoxItem;

    public listAdapter(Activity context, String[] index_item, String[] barcode_food, String[] item_name, String[] price_item) {
        super(context, R.layout.activity_menu_list, index_item);

        this.context = context;
        this.index_item = index_item;
        this.barcode_food = barcode_food;
        this.item_name = item_name;
        this.price_item = price_item;
        this.checkboxes = new boolean[index_item.length];
        for(i = 0; i < checkboxes.length; i++)
             checkboxes[i] = false;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.activity_menu_list,null,true);

        TextView textView_name = (TextView) rowView.findViewById(R.id.textViewItemName);

        final CheckBox checkBoxItem = (CheckBox) rowView.findViewById(R.id.checkboxList);

        textView_name.setText(item_name[position]);
        checkBoxItem.setChecked(checkboxes[position]);
        // Row View got click
        rowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 checkboxes[position] = !checkboxes[position];
                 checkBoxItem.setChecked(checkboxes[position]);
                /*if (checkBoxItem.isChecked()==true){
                    checkBoxItem.setChecked(false);

                }
                else checkBoxItem.setChecked(true);*/
            }
        });



        // Return row view
        return rowView;
    }

【讨论】:

  • 你能给我举个例子吗?谢谢!
  • 谢谢!它正在工作,但有点像不正确。一些复选框工作,一些不工作。当我添加此代码“checkboxes[position] = !checkBoxItem.isChecked()”时,因此在我的公共视图 getView(final int position, View convertView, ViewGroup parent) 中使用 final 声明。
  • 可能必须是 ` checkboxes[position] = !checkboxes[position];` 抱歉,已编辑答案
  • 复选框[位置] = checkBoxItem.isChecked();也不工作。谢谢!
  • 复选框[位置] = !复选框[位置];我编辑了
【解决方案5】:

您似乎缺少保留复选框的状态。每次滚动期间可见项目时,Android 都会重新渲染它。试试看

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
  //Inflate Single View
  View v= View.inflate(context, R.layout.row_item, null);

  //Bind Components
  TextView textView_name = (TextView) v.findViewById(R.id.textViewItemName);
  CheckBox checkBoxItem = (CheckBox) v.findViewById(R.id.checkboxList);

  //Populate Componengts
  textView_name.setText(item_name[position]);

  //Identify Checkbox clicks
  checkBoxItem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
  {
    @Override
    public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
    {
      //Save checked data somewhere after checking isChecked()
    }
  });

  //Check checkbox state here
  if(checkBoxItem.isChecked())
  {
    checkBoxItem.setChecked(true);
  }
  else
  {
    checkBoxItem.setChecked(false);
  }

  //Return View
  return v;
}

【讨论】:

    猜你喜欢
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 2017-08-10
    • 2011-05-13
    • 1970-01-01
    相关资源
    最近更新 更多