有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

带有复选框滚动问题的java ListView

我有安卓程序,但我的列表视图不能正常工作。 如果列表视图有更多滚动项,当我要单击最后一个滚动复选框时,请选中另一项。请找到我的代码示例。请帮我解决这个问题

这是我的getView for adapter类代码

    @SuppressLint("InflateParams")
    public View getView(int position, View convertView, ViewGroup parent) {

        View vi = convertView;
        if(convertView==null){
            vi = inflater.inflate(R.layout.unsettled_invoice_layout, null);
            holder = new ViewHolder();
            holder.desc = (TextView) vi.findViewById(R.id.usinvoicedesc);
            holder.chkbox = (CheckBox) vi.findViewById(R.id.selected);
            holder.chkbox.setOnClickListener(new OnItemClickListener(position));
            vi.setTag(holder);
            vi.setTag(R.id.selected, holder.chkbox);
        } else 
            holder=(ViewHolder)vi.getTag();
        if(data.size()<=0){
            holder.desc.setText("No Data");
        }else{
            tempValues=null;
            tempValues = (Invoice)data.get(position);
            if(mode == 1){
                 int myColor =  Color.rgb(0, 128, 0);
                 if(tempValues.getStatus() == 2){
                        myColor = Color.rgb(0, 128, 0);     // Green        
                        holder.chkbox.setChecked(false);
                 }else if(tempValues.getStatus() == 3){
                         myColor = Color.rgb(128, 0, 0);    // Red
                         holder.chkbox.setChecked(true);
                 } 
                 holder.desc.setBackgroundColor(myColor);
            }
             holder.desc.setText(tempValues.toString());
             vi.setOnClickListener(new OnItemClickListener( position));
        }
        return vi;
    }

这是布局。我在安卓中使用的xml

<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="fill_parent"
    安卓:layout_height="wrap_content"
    安卓:layout_gravity="top"
    安卓:clickable="false"
    安卓:descendantFocusability="blocksDescendants"
    安卓:focusable="false"
    安卓:focusableInTouchMode="false"
    安卓:paddingTop="0dip"
    安卓:textColor="#000000" >

    <LinearLayout
        安卓:id="@+id/usinvoicelayout"
        安卓:layout_width="fill_parent"
        安卓:layout_height="95dp"
        安卓:layout_marginBottom="3dp"
        安卓:layout_marginTop="2dip"
        安卓:clickable="false"
        安卓:descendantFocusability="blocksDescendants"
        安卓:focusable="false"
        安卓:focusableInTouchMode="false"
        安卓:orientation="horizontal"
        安卓:paddingTop="0dip" >

        <TextView
            安卓:id="@+id/usinvoicedesc"
            安卓:layout_width="520dp"
            安卓:layout_height="90dp"
            安卓:layout_gravity="start"
            安卓:gravity="center_vertical"
            安卓:textColor="#FFFF00"
            安卓:textSize="18sp" />

        <CheckBox
            安卓:id="@+id/selected"
            安卓:layout_width="50dp"
            安卓:layout_height="80dp"
            安卓:layout_marginStart="15dip"
            安卓:button="@drawable/checkbox_background"
            安卓:clickable="false"
            安卓:focusable="false"
            安卓:focusableInTouchMode="false" />
    </LinearLayout>

</LinearLayout>

共 (2) 个答案

  1. # 1 楼答案

    请查看以下链接: 这是针对您在滚动中选中的另一项复选框问题的特定解决方案:

    http://lalit3686.blogspot.in/2012/06/today-i-am-going-to-show-how-to-deal.html

    在上面的链接示例中,在listview适配器类getView()方法中添加以下代码:

    viewHolder.checkbox.setTag(position); // This line is important.
    

    对于复选框,请单击添加以下代码:

    viewHolder.checkbox.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.
            list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
        }
    });
    
  2. # 2 楼答案

    为此,您需要维护一个hashmap。当您单击复选框put position in hashmap和getview中的复选框set check check position in hashmap时。如果存在,则选中复选框