ListView-项目选择随滚动而消失



我是新手。几天来,我试图解决这个问题,但没有运气。有帮助吗?预先感谢。

下面是图像,我选择了几个项目,然后滚动一旦滚动滚动,选择就会消失

在我实施的适配器类下方,

/**
 * Created by abcd on 27/1/17.
 */
public class ListItemAdapterTaxCheckBox extends BaseAdapter {
    //flag = 1 name/id
    //flag =2 //ID/Name
    public class HolderCheck
    {
        TextView tv;
        CheckBox ck;
    }
    private static LayoutInflater inflater=null;
    Tax[] result;
    Context context;
    Tax[] selections = null;
    String [] IDs = null;
    boolean bAllSel = false;
    Resources res = null;
    public ListItemAdapterTaxCheckBox(Activity mainActivity, Tax[] prgmNameList, Tax[] sel, Resources rs, String [] ids) {
        result=prgmNameList;
        context=mainActivity;
        selections = sel;

        if(selections==null)
        {
            selections = new Tax[1];
            selections[0] = new Tax();
        }
        IDs = ids;
        res = rs;
        inflater = ( LayoutInflater )context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        return result.length + 1;
    }
    @Override
    public Object getItem(int position) {
        return position;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final HolderCheck holderCk = new HolderCheck();;
        View rowView = convertView;
        final int pos = position;
        rowView = inflater.inflate(R.layout.custom_list_check_box, null);
        holderCk.tv = (TextView) rowView.findViewById(R.id.code);
        holderCk.ck = (CheckBox) rowView.findViewById(R.id.checkBox1);
        if(position==0) {
            holderCk.tv.setText(context.getString(R.string.all_text));
        }
        else {
            holderCk.tv.setText("" + result[position-1].m_TaxID + " - " + result[position-1].m_TaxName);
        }
        holderCk.tv.setTextSize(16);
        holderCk.tv.setTextColor(Color.BLACK);
        rowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holderCk.ck.isChecked()) {
                    holderCk.tv.setBackgroundColor(Color.WHITE);
                    holderCk.ck.setChecked(false);
                    if(pos!=0)
                    {
                        if (IDs != null)
                            IDs[pos -1] = "0";
                    }
                    else
                    {
                        bAllSel = false;
                    }
                } else {
                    holderCk.tv.setBackgroundColor(GetResourceColor.getColorWrapper(context, R.color.selHintColor));
                    holderCk.ck.setChecked(true);
                    if(pos!=0)
                    {
                        if (IDs != null)
                            IDs[pos -1] = "" + result[pos-1].m_TaxID;
                    }
                    else
                    {
                        bAllSel = true;
                    }
                }
            }
        });
        return rowView;
    }
    public String [] getIDs() {
        if(bAllSel)
            return null;
        else {
            ArrayList<String> arrayList = new ArrayList<String>();
            for (int i = 0, j = 0; i < IDs.length; i++) {
                if (IDs[i].trim() != "0") {
                    arrayList.add(j, IDs[i].trim());
                    j = j + 1;
                    if (arrayList.size() == MySQLHelper.MAXITEMSLISTDELETE)
                        break;
                }
            }
            return arrayList.toArray(new String[arrayList.size()]);
        }
    }
}  

在我填充ListView

的方式下方
//class member
Tax[] valuesTaxID = null;

//inside a method
//default
Tax[] tx = new Tax[1];
tx[0] = new Tax();
if (valuesTaxID != null) {
    listV.setAdapter(null);
    listV.setAdapter(new ListItemAdapterTaxCheckBox(ctx, valuesTaxID, tx, rs, Ids));
listV.setVisibility(View.VISIBLE);

ListView将在滚动时创建和破坏视图。单击视图时,您的代码正在更改复选框,但是您可以将这些信息存储在其他任何地方,您滚动,滚动时会破坏视图,并且在那里丢失了视图的状态。

您想在某个地方存储视图的"检查"状态。可能是带有状态的阵列列表,可以是附上的标签,只是将其存储在某个地方。

最新更新