如何在我的代码中选择ListView项?

时间:2014-09-11 01:14:20

标签: android listview

我的代码有问题。当我在ListView中删除第3项(选中)时(如果它有5项),下一项将是setbackground。怎么会这样?任何帮助将不胜感激!

ListView代码:

experiment_left_lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            listChooseId = arg2;
            if(ExperimentAdapter.views.size() != 0){
                for (int i = 0; i < ExperimentAdapter.views.size(); i++) {
                    ExperimentAdapter.views.get(i).setBackgroundResource(R.drawable.list_bg);
                }
                ExperimentAdapter.views.removeAll(ExperimentAdapter.views);
            }
            if(view != null){
                experiment_right_rl.removeView(view);
            }
            if(views.size() != 0){
                if(((Integer)views.get(0).get("id")) == arg2){
                    ((View)views.get(0).get("view")).setBackgroundResource(R.drawable.list_bg);
                    chooseE_id = -1;
                    views.remove(0);
                    map.clear();
                }
                else{
                    ((View)views.get(0).get("view")).setBackgroundResource(R.drawable.list_bg);
                    views.remove(0);
                    map.clear();
                    map.put("id", arg2);
                    map.put("view", arg1);
                    views.add(map);
                    arg1.setBackgroundResource(R.drawable.list_select);
                    chooseE_id = experiments.get(listChooseId).getE_id();
                    showView();
                }
            }
            else{
                map.put("id", arg2);
                map.put("view", arg1);
                views.add(map);
                arg1.setBackgroundResource(R.drawable.list_select);
                chooseE_id = experiments.get(listChooseId).getE_id();
                showView();
            }
        }
    });

deleteBtn onClick:

if(chooseE_id != -1){
                AlertDialog.Builder builder = new AlertDialog.Builder(ExperimentActivity.this);
                builder.setTitle(getString(R.string.exp_sure_to_delete));
                builder.setPositiveButton(getString(R.string.sure), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        List<Step> steps = stepDao.getAllStep(ExperimentActivity.this, chooseE_id);
                        for (int i = 0; i < steps.size(); i++) {
                            stepDao.deleteStep(steps.get(i), ExperimentActivity.this);
                        }
                        experimentDao.deleteExperiment(chooseE_id, ExperimentActivity.this);
                        experimentAdapter.getView(chooseE_id,null, null).setBackgroundResource(R.drawable.list_bg);

                        experiments = experimentDao.getAllExperimentsByU_id(ExperimentActivity.this, U_id);
                        experiment_left_lv.setAdapter(new ExperimentAdapter(ExperimentActivity.this, experiments));
                        if(view != null){
                            experiment_right_rl.removeView(view);
                        }
                        chooseE_id = -1;
                    }
                })

0 个答案:

没有答案