Android的列表视图获取视图的位置 [英] android listview getting the position of the view

查看:87
本文介绍了Android的列表视图获取视图的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的列表视图的getview我指定的位置到另一个变量。 我有我的listrow中的复选框。

In the getview of my listview i am assigning the position to another variable . I have a checkbox within my listrow.

我已经定义setOnCheckedChangeListener的复选框。

I have defined setOnCheckedChangeListener for checkbox.

现在,当我强制检查/取消选中行,我看到getview是没有得到调用。 但我用的位置,设在getview和给我正确的价值的价值。

Now when i jus check / uncheck row , i see that the getview is not getting called. But i use the value of position which is set in getview and its giving me the right value.

也就是说,如果我点击第10位,该变量也有10。

ie if i click 10th position , the variable is also having 10 .

请给我解释一下这是怎么工作。

Please explain me how is this working.

我的code低于

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        final int pos = position;
        barcodeForSelectedRow = productList.get(pos).getBarcode();
        formatForSelectedRow = productList.get(pos).getFormat();
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.product_list_row, null);

            holder = new ViewHolder();
            holder.text1 = (TextView) convertView.findViewById(R.id.productid);

            holder.text2 = (TextView) convertView.findViewById(R.id.price);
            holder.text2.setOnClickListener(listener);
            holder.image = (ImageView) convertView
                    .findViewById(R.id.productimageid);
            holder.check=(CheckBox) convertView.findViewById(R.id.checkbox);
            convertView.setTag(holder);
            holder.image.setOnClickListener(listener);

    } else {
            holder = (ViewHolder) convertView.getTag();
        }


        // setWishListItemsInScanList(pos, convertView);
        holder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton checkBox,
                    boolean isChecked) {
                Log.i("CheckBox check", "" + pos);


        });
        return convertView;
    }

我在这里设置内getview POS =位置 当我点击一行10复选框,则oncheckchnaged听众是越来越调用,而不是getview功能 但我看到它的位置是否正确凑onchechchangedlistener。 我wnat要懂得其发生

Here i am setting pos= position within getview When i click checkbox of row 10, the oncheckchnaged listener is getting invoked, but not the getview function but i see its position is getting correctly in onchechchangedlistener . I wnat to know how its happening

注:getview被称为每当我来到了这个活动,这是预期的行为,而且当我点击复选框getview不应该被调用,它也工作正常。我的问题是如何oncheckchangedlistener得到正确位置值

NB : getview is called whenever i come to this activity, which is the expected behaviour, Also when i click a checkbox getview shouldnt be called, and it is also working fine. My question is how oncheckchangedlistener gets position value correctly

推荐答案

getView()被调用每一个特定的视图,因此每次设置setOnCheckedChangeListener时间()你传递本地值POS。每次getview()被调用时,会创建一个新的变量POS因为你设置它最后也可能不会有任何其他方式。因此,每个setOnCheckedChangeListener()具有POS变量它自己的实例。

getView() is called for each particular view, so each time you set the setOnCheckedChangeListener() you pass the local value "pos". Each time getview() is called, a new variable "pos" is created and because you set it final it couldn't be any other way. So each setOnCheckedChangeListener() has it's own instance of the "pos" variable.

这篇关于Android的列表视图获取视图的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆