ListView方法触发错误的位置

时间:2015-06-10 09:16:00

标签: android listview checkbox

我有listview,包含复选框和textview,当isCheck然后我将更改此View和TextView的颜色。但错误的立场。请告诉我为什么?非常感谢

Ravensbusch street 
[04:41, 05:41, 06:09, 06:38, 07:08, 07:38, 08:08, 08:38, 09:08, 09:38, 10:08, 10:38, 11:08, 11:38, 12:08, 12:38, 13:08, 13:38, 14:08, 14:38, 15:08, 15:38, 16:08, 16:38, 17:08, 17:38, 18:08, 18:38, 19:08, 19:38, 20:08, 20:38, 21:38, 22:38, 23:38]

}

enter image description here 我上传图片,当我点击ComboBox时,这个日志说出确切的位置(没关系)。但我写的事件发生在其他pos。 上面是我的适配器,所有组件都是从布局中找到的(没问题)。

4 个答案:

答案 0 :(得分:1)

问题在于你的getView。基本上你的代码是正确的,但你需要做更多的步骤。问题是您的物品正在回收。

我猜你的ListView是多选的,所以选择ArrayList。

第1步:

声明一个arrayList ArrayList id_array = new ArrayList();

第2步:

像这样更改arrayList

holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton btn,
                    boolean isChecked) {
               if (!id_array.contains(holder.cb.getId())){
//add id to your arraylist
}else{
//Remove id from arrayList
}

            }
        });

以下代码需要在oncheckedchangeListener之外写入

 if (id_array.contains(holder.cb.getId())){
                    Log.e("", "checked in pos : " + position);
                    holder.tvFlightName.setText("POS :" + position);
                    holder.rllFlightName.setBackgroundColor(Color.BLUE);
                } else {
                    Log.e("", "unchecked in pos : " + position);
                    holder.tvFlightName
                            .setText("returned POS :" + position);
                    holder.rllFlightName.setBackgroundColor(Color.WHITE);
                }

注意: 我会查看您不使用setId复选框的代码。我希望你必须有一些ID对checkBox。那么只有我的代码才能正常工作

答案 1 :(得分:0)

替换

@Override
public Object getItem(int pos) {
    return null;
}

@Override
public Object getItem(int pos) {
    return pos;
}

答案 2 :(得分:0)

持有者'引用应该在getView()函数内部..不确定它是什么,但它可能是。

答案 3 :(得分:0)

我发现这种方式可以解决这个问题。只需使用数据并将其传递给适配器。触发事件时,更改数据并调用notifidatasetchanged()。

public class GLPSelectFlight extends Activity implements OnClickListener {
    private Button btnContinue;
    private TextView tvHeader;
    private LinearLayout linBack;
    private ListView lvSelectFlight;
    private int[] data = new int[] { Color.WHITE, Color.WHITE, Color.WHITE,
            Color.WHITE, Color.WHITE };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_glpselect_flight);
        initView();
    }

    private void initView() {

        linBack = (LinearLayout) findViewById(R.id.home);
        linBack.setOnClickListener(this);
        linBack.setVisibility(View.VISIBLE);

        tvHeader = (TextView) findViewById(R.id.tvHeader);
        tvHeader.setSingleLine(true);
        tvHeader.setEllipsize(TruncateAt.END);
        tvHeader.setText(getString(R.string.SELECT_FLIGHT));

        lvSelectFlight = (ListView) findViewById(R.id.lvGlpShowFlightInfo);
        adapter = new GLPShowFlightAdapter(data);
        lvSelectFlight.setAdapter(adapter);

        btnContinue = (Button) findViewById(R.id.btnContinue);
        btnContinue.setOnClickListener(this);
    }

    private class ViewHolder {
        TextView tvFlightName;
        RelativeLayout rllFlightName;
        CheckBox cb;
    }

    GLPShowFlightAdapter adapter;
    ViewHolder holder;

    // for temporary Adapter.
    private class GLPShowFlightAdapter extends BaseAdapter {
        private int[] data;

        public GLPShowFlightAdapter(int[] data) {
            this.data = data;
        }

        @Override
        public int getCount() {
            return 5;
        }

        @Override
        public Integer getItem(int pos) {
            return data[pos];
        }

        @Override
        public long getItemId(int pos) {
            return pos;
        }

        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(GLPSelectFlight.this)
                        .inflate(R.layout.glp_list_flight, parent, false);

                holder.cb = (CheckBox) convertView
                        .findViewById(R.id.cbSelectFlight);
                holder.cb.setId(position * 69);
                holder.rllFlightName = (RelativeLayout) convertView
                        .findViewById(R.id.rllFlightName);
                holder.tvFlightName = (TextView) convertView
                        .findViewById(R.id.tvFlightName);

                convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton btn,
                        boolean isChecked) {
                    if (isChecked) {
                        Log.e("", "checked in pos : " + position);
                        data[position] = Color.BLUE;
                        adapter.notifyDataSetChanged();
                    } else {
                        Log.e("", "unchecked in pos : " + position);
                        data[position] = Color.WHITE;
                        adapter.notifyDataSetChanged();
                    }
                }
            });
            holder.rllFlightName.setBackgroundColor(data[position]);
            return convertView;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.home:
            onBackPressed();
            break;
        case R.id.btnContinue:
            Intent in = new Intent(new Intent(GLPSelectFlight.this,
                    TranferBonusPaymentActivity.class));
            startActivity(in);
            break;
        }
    }
}

但我不知道上面的问题:为什么会出现这个问题。如果有人知道请告诉我。谢谢:)