0

好的,我正在改写这个问题,

我有一个具有多个布局的自定义 BaseAdaptor,在选择列表行时选择复选框时遇到问题,

实际上它不适用于使用布局的第一行,其他的完全按照我想要的方式工作。

***编辑添加一些代码的时间:在我的主类中:

myAdaptor = new ListAdapter(this, myValues);
    listview.setAdapter(myAdaptor);
    Log.d("SettingsScreen", "Setup listview click");
    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> l, View v, int pos, long arg3) {
            if (pos == 1) {
                myAdaptor.displayNeedle.setChecked(!myAdaptor.displayNeedle
                        .isChecked());
            } else if (pos == 2) {
                accent1st.setChecked(!accent1st.isChecked());
            } else if (pos == 3) {
                myAdaptor.wakeLock.setChecked(!myAdaptor.wakeLock
                        .isChecked());
            } else if (pos == 4) {
                tapTimeSig.setChecked(!tapTimeSig.isChecked());
            }
        }
    });

}

我的自定义适配器:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = layoutType[position];
        holder = null;
        holder = new ViewHolder();

        if (convertView == null) {
            holder = new ViewHolder();
            switch (type) {
            case 0:
                convertView = mInflater.inflate(R.layout.tickbox, null);
                holder.textView = (TextView) convertView
                        .findViewById(R.id.label);
                if (position == 1) {
                    displayNeedle = (CheckBox) convertView
                            .findViewById(R.id.mycheckbox);
                    displayNeedle
                            .setOnCheckedChangeListener(new OnCheckedChangeListener() {
                                @Override
                                public void onCheckedChanged(
                                        CompoundButton arg0, boolean arg1) {
                                    getData().setDisplayNeedle(
                                            !getData().getDisplayNeedle());
                                }
                            });
                }
                else if (position == 2) {
                    accent1st = (CheckBox) convertView
                            .findViewById(R.id.mycheckbox);
                    accent1st.setChecked(getData().getAccentBeat(0));
                    accent1st
                            .setOnCheckedChangeListener(new OnCheckedChangeListener() {
                                @Override
                                public void onCheckedChanged(
                                        CompoundButton arg0, boolean arg1) {
                                    getData().setAccentBeat(0,
                                            accent1st.isChecked());
                                }
                            });
                }
                if (position == 3) {
                    wakeLock = (CheckBox) convertView
                            .findViewById(R.id.mycheckbox);
                    wakeLock.setChecked(getData().getWakeLock());
                    if (!getData().getPro()) {
                        wakeLock.setEnabled(false);
                        values[3] += " (Pro version only)";
                    }
                    wakeLock.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton arg0,
                                boolean arg1) {
                            if (getData().getPro())
                                getData().setWakeLock(wakeLock.isChecked());
                        }
                    });
                }
                if (position == 4) {
                    tapTimeSig = (CheckBox) convertView
                            .findViewById(R.id.mycheckbox);
                    tapTimeSig.setChecked(getData().getTapTimeSig());
                    tapTimeSig
                            .setOnCheckedChangeListener(new OnCheckedChangeListener() {
                                @Override
                                public void onCheckedChanged(
                                        CompoundButton arg0, boolean arg1) {
                                    getData().setTapTimeSig(
                                            tapTimeSig.isChecked());
                                }
                            });
                }
                break;

感谢您到目前为止的支持,我已经坚持了太久了,例如几周!我真的很想让它正常运行。

4

1 回答 1

0

我认为你可以这样做:

yourListView.setOnItemClickListener(new OnItemClickListener() {
   @Override
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
        for(int i=0;i<parent.getCount();i++){
            View v=parent.getChildAt(parent.getCount()-1-i);
            if (position == i) {
                v.setBackgroundColor(Color.RED);//highlight color
            } else {
                v.setBackgroundColor(Color.TRANSPARENT);//not highlight color
            }
        }
    }
});

希望有帮助。

于 2012-08-02T05:34:22.093 回答