Android CustomListViewAdapter在删除数据时表现得很奇怪

时间:2015-09-05 18:33:37

标签: java android

基本上,这是MainActivity类的相关代码块:

private ListView listView;
private ArrayList<String> tasks = new ArrayList<String>();

private CustomListViewAdapter customListViewAdapter;

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

    tasks.add("Hfrfello");
    tasks.add("Heefrefllo");
    tasks.add("Hellefrefo");
    tasks.add("Helggglo");
    tasks.add("Helggglo");
    tasks.add("aaHello");
    tasks.add("aaaaaaHelaaalo");
    tasks.add("Hfrfello");
    tasks.add("Heefrefllo");
    tasks.add("Hellefrefo");
    tasks.add("Helggglo");
    tasks.add("Helggglo");
    tasks.add("aaHello");
    tasks.add("aaaaaaHelaaalo");

    this.listView = (ListView) findViewById(R.id.tasks);

    this.customListViewAdapter = new CustomListViewAdapter(getApplicationContext(), this.tasks);

    this.listView.setAdapter(this.customListViewAdapter);

    this.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //deleteTask(position);

            customListViewAdapter.remove(position);
            //tasks.remove(position);
            customListViewAdapter.notifyDataSetChanged();
        }
    });
}

这是我的CustomListViewAdapter课程:

package com.hassanalthaf.dailytaskmanager;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class CustomListViewAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<String> tasks;
    private static LayoutInflater inflater = null;

    public CustomListViewAdapter(Context context, ArrayList<String> data) {
        this.context = context;
        this.tasks = data;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return this.tasks.size();
    }

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

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

    public void remove(int position) {
        this.tasks.remove(position);
        System.out.println(this.tasks);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;

        if (convertView == null) {
            view = this.inflater.inflate(R.layout.list_row, null);

            TextView taskName = (TextView) view.findViewById(R.id.taskName);
            ImageView image = (ImageView) view.findViewById(R.id.deleteButton);

            String task = this.tasks.get(position);

            taskName.setText(task);
        }

        return view;
    }
}

我的问题:当我点击我的图形用户界面时,它会删除并更新ListView,但是它会更新错误。它的作用是,当我点击前3个元素(应该删除它们)时,它会从ArrayLists中删除它们,并在删除它们后得到正确的输出。但是,在用户界面中,不是删除我点击的内容,而是删除底部项目,而不是触摸顶部项目。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您的getView()未设置回收的convertView个对象的状态,因此它们会继续显示旧状态。试试这个:

@Override 
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;

    if (convertView == null) {
        view = this.inflater.inflate(R.layout.list_row, null);
    }

    TextView taskName = (TextView) view.findViewById(R.id.taskName);
    String task = this.tasks.get(position);
    taskName.setText(task);

    return view;
} 

答案 1 :(得分:1)

我同意@cybersam。我认为如果你使用它会优化  Holder pattern as:

@Override 
public View getView(int position, View convertView, ViewGroup parent) {        
    MyHolder holder;
    View view = convertView;

    if (view == null) {
        view = this.inflater.inflate(R.layout.list_row, null);
        holder = new MyHolder(view);
        view.setTag(holder);
    }
    else
    {
       holder = (MyHolder) view.getTag();
    }


    String task = this.tasks.get(position);
    holder.taskName.setText(task);

    return view;
} 



 class MyHolder {
        TextView taskName;

        public MyHolder(View view) {
            taskName = (TextView) view.findViewById(R.id.taskName);
        }
    }