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

时间:2017-04-30 12:01:06

标签: java android

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

下面是图片, I selected few items and then scrolled up Once scrolled down selection disappears

在我实现的适配器类下面,

long ans=1;
while(n>=1)
{
    ans*=x;
    n--;
}
System.out.println(ans);

在我填充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()]);
        }
    }
}  

1 个答案:

答案 0 :(得分:0)

滚动时ListView将创建和销毁视图。单击视图时,您的代码正在更改复选框,但是您无法在其他地方存储该信息,滚动时,ListView会在滚动时销毁视图,并且您丢失了视图的状态。

您想要存储&#34;已选中&#34;某处的观点状态。可以是带有状态的ArrayList,可以是附加到ViewHolders的标签,...只是将它存储在某处。