删除listView中的行不能按预期工作

时间:2014-02-09 08:53:57

标签: android listview adapter

我正在尝试在用户点击自定义行视图中的X按钮时删除listView中的行。遗憾的是,当我尝试使用remove()函数时,它似乎不起作用。我可以错误地设置ListView的数据类型吗?目前,它使用2个按钮和ImageButton来扩展linearLayout。 ImageButton是X按钮。这是适配器。它扩展了一个Classes对象的arrayAdapter。 (他们只握住2个字符串)所以new Classes("", "");我是否告诉删除函数删除位置0,1,2等等并不重要。它将始终删除列表中的最后一个位置。我做错了什么?

public PlannerListAdapter(Context context, ArrayList<String> rubric,
        SortedMap<String, String> currentTerms, ArrayList<String> termList, ArrayList<Classes> listItems) {
    super(context, R.layout.schedule_planner_list_item);

    this.context = context;
    this.rubricList = rubric;
    this.currentTerms = currentTerms;
    this.termList = termList;
    this.rubricList = rubric;
    courseHandler = new CourseHandler();
    courseDataTask = new GetCourseData();
    this.listItems = listItems;
    inflater = LayoutInflater.from(context);

}
static class ClassViewHolder {
    private Button rubric;
    private Button course;
    private ImageButton clearView;

    //pub constructor
    ClassViewHolder(Button rubric, Button course, ImageButton clearView) {
        this.rubric = rubric;
        this.course = course;
        this.clearView = clearView;
    }

    /**
     * @return the rubric
     */
    public Button getRubric() {
        return rubric;
    }
    /**
     * @param rubric the rubric to set
     */
    public void setRubric(Button rubric) {
        this.rubric = rubric;
    }
    /**
     * @return the course
     */
    public Button getCourse() {
        return course;
    }
    /**
     * @param course the course to set
     */
    public void setCourse(Button course) {
        this.course = course;
    }

    /**
     * @return the clearView
     */
    public ImageButton getClearView() {
        return clearView;
    }

    /**
     * @param clearView the clearView to set
     */
    public void setClearView(ImageButton clearView) {
        this.clearView = clearView;
    }
}


/* (non-Javadoc)
 * @see android.widget.ArrayAdapter#getCount()
 */
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return listItems.size();
}



/* (non-Javadoc)
 * @see android.widget.ArrayAdapter#getItem(int)
 */
@Override
public Classes getItem(int position) {
    // TODO Auto-generated method stub
    return listItems.get(position);
}

/* (non-Javadoc)
 * @see android.widget.ArrayAdapter#getPosition(java.lang.Object)
 */
@Override
public int getPosition(Classes item) {
    // TODO Auto-generated method stub
    return listItems.indexOf(item);
}

public int getViewTypeCount() {
    return 1;
}


/* (non-Javadoc)
 * @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
 */
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
    final Button rubric;
    final Button course;
    final ImageButton clearView;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.schedule_planner_list_item, null);
        rubric = (Button)convertView.findViewById(R.id.rubric_spinner);
        course = (Button)convertView.findViewById(R.id.course_spinner);
        clearView = (ImageButton)convertView.findViewById(R.id.clear_courseRubric_view);
        convertView.setTag(new ClassViewHolder(rubric, course, clearView));
    }
    else {
        ClassViewHolder viewHolder = (ClassViewHolder)convertView.getTag();
        rubric = viewHolder.getRubric();
        course = viewHolder.getCourse();
        clearView = viewHolder.getClearView();
    }

    rubric.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            RubricArray = rubricList.toArray(RubricArray);
            //Toast.makeText(context, rubricList.get(0), Toast.LENGTH_SHORT).show();
            AlertDialog.Builder rubricDialog = new AlertDialog.Builder(context);
            rubricDialog.setTitle("Pick a Rubric");
            rubricDialog.setItems(RubricArray, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, final int which) {
                    RubricString = RubricArray[which];
                    rubric.setText(RubricString);
                    notifyDataSetChanged();
                    currentSubject = RubricString;
                    courseList.clear();
                    CourseArray = courseList.toArray(CourseArray);
                    courseDataTask.execute();
                    courseHandler = new CourseHandler();
                    courseDataTask = new GetCourseData();
                }
            });

            rubricDialog.show();
        }

    });
    course.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            AlertDialog.Builder courseDialog = new AlertDialog.Builder(context);
            courseDialog.setTitle("Pick a Course") ;
            courseDialog.setItems(CourseArray, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    CourseString = CourseArray[which];

                    course.setText(CourseString);
                    notifyDataSetChanged();
                }
            });

            courseDialog.show();

        }
    });

    clearView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            listItems.remove(listItems.lastIndexOf(v);
            notifyDataSetChanged();
        }
    });
    return convertView;
}

1 个答案:

答案 0 :(得分:0)

因为我没有足够的声誉来评论你的问题,所以我写了这个。 我没有测试你的代码,但问题是找到点击视图的索引。 我问为什么你不使用位置删除列表项,如

listItems.remove(position); then, 
notifyDataSetChanged(); 
or you can also use
listview.invalidateViews();

编辑:

add this line in (convertView == null) {} block.
clearView.setTag(position); then,

clearView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        listItems.remove(v.getTag());
        notifyDataSetChanged();
    }
});