从自定义适配器调用remove总是删除最后一项

时间:2014-12-26 23:27:51

标签: android android-listview android-adapter

我已经实现了自定义适配器和listItemView。适配器将onlclick侦听器设置为listItemView上的按钮。 onclick侦听器只是调用我在适配器中的私有方法,并将其传递给要删除的项目的位置。我知道位置是正确的,因为数据库删除了正确的项目。我发现了类似的问题,但无法让答案适应我。非常感谢您的想法和想法。谢谢。

这是完整的适配器类

public class FoodListAdapter extends ArrayAdapter<FoodListItem> {

    //private
    private int type;

    public FoodListAdapter(Context context, ArrayList<FoodListItem> _objects) {
        super(context, 0, _objects);
        type = 0;       
    }

    public FoodListAdapter(Context context, ArrayList<FoodListItem> _objects, int _type) {
        super(context, 0, _objects);
        type = _type;       
    }

    @Override
    public View getView(int position, View reusableView, ViewGroup parent)
    {
        //Cast the reusable view to a listAdpaterItemView
        FoodListItemView listItemView = (FoodListItemView) reusableView;

        //Check if the listAdapterItem is null
        if(listItemView == null)
        {
            //If it is null, then create a view.
            listItemView = FoodListItemView.inflate(parent, this, type);
        }

        if (type == 2)
        {
            Button deleteButton = (Button) listItemView.findViewById(R.id.listItemViewDeleteBTN);
            deleteButton.setTag(new Integer(position));
        }

        //Now we need to set the view to display the data.
        listItemView.setData(getItem(position));

        return listItemView;
    }
}

这是我在片段中使用的代码的一部分。请注意,我在listAdapter的类中有一个私有变量decadered,虽然我不认为我需要它。

    private void displayListForDate(Calendar _date)
{
    //get the list view
    ListView listView = (ListView) getView().findViewById(1);
    //Clear the listview by removing the listadapter and setting it to null.
    //listView.setAdapter(null);

    //First we must get the items.
    Global global = (Global) getActivity().getApplicationContext();
    DietSQLiteHelper database = global.getDatabase();

    //Create a list to hold the items we ate. This list will then be added to the listView.
    final ArrayList<FoodListItem> consumedList;
    //Add the items to the array.
    consumedList = database.getConsumed(_date.getTimeInMillis());

    //Create an adapter to be used by the listView
    listAdapter = new FoodListAdapter(getActivity().getBaseContext(), consumedList, 2);

    //Add the adapter to the listView.
    listView.setAdapter(listAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            consumedList.remove(position);
            listAdapter.notifyDataSetChanged();
        }
    });
}

3 个答案:

答案 0 :(得分:0)

如果你没有实施&#34; equals&#34; FoodListItem的方法,尝试实现它。

答案 1 :(得分:0)

我会建议, 您只需更新基础数据,在您的情况下为ArrayList<FoodItems>

在你的适配器中制作这个简单的方法并改变:

private List<FoodListItem> myList = new ArrayList<FoodListItem>();

public FoodListAdapter(Context context, List<FoodListItem> myList) {
    super(context, 0, myList);
    type = 0;
    this.myList = myList;
}

public FoodListAdapter(Context context, List<FoodListItem> myList, int _type) {
    super(context, 0, myList);
    type = _type; 
    this.myList = myList;     
}

// Also update your getView() method to use myList!
@Override
public View getView(int position, View reusableView, ViewGroup parent)
{
   ...
   listItemView.setData(myList.get(position));

public void removeItem(int positio){
    if(myList != null){
        myList.remove(position);
    }
}

然后在课堂上,你正在创建适配器(Activity / Fragment),只需调用方法。

// Update the underlying ArrayAdapter
adapter.removeItem(position);
// Notify the adapter, the data has changed
adapter.notifyDataSetChanged();

此外,您不应该在UI线程上打开与SQLiteDatabase的连接,因为您正在阻止它。你永远不知道,从磁盘读取的速度有多快。如果花费太长时间,用户可以认为,您的应用程序冻结了,因此,他离开了,您不想要的。我建议使用AsyncTask,你会发现很多examples

答案 2 :(得分:0)

我经历了清理我的代码,它现在可以工作了,这是工作代码。除了更新我用来分配和获取视图的ID之外,我真的不知道其他区别。如果有人能解释我遇到问题的原因,我会很感激。

这是片段中的片段,我在其中创建列表视图并分配适配器。

private void displayListForDate(Calendar _date)
{
    //get the list view
    ListView listView = (ListView) getView().findViewById(R.id.listView);
    //Clear the listview by removing the listadapter and setting it to null.
    //listView.setAdapter(null);

    //First we must get the items.
    Global global = (Global) getActivity().getApplicationContext();
    DietSQLiteHelper database = global.getDatabase();

    //Create a list to hold the items we ate. This list will then be added to the listView.
    ArrayList<FoodListItem> consumedList;
    //Add the items to the array.
    consumedList = database.getConsumed(_date.getTimeInMillis());

    //Create an adapter to be used by the listView
    listAdapter = new FoodListAdapter(getActivity().getBaseContext(), consumedList, 2);

    //Add the adapter to the listView.
    listView.setAdapter(listAdapter);
}

这是我的适配器类。

public class FoodListAdapter extends ArrayAdapter<FoodListItem> 
{

    //private
    private int type;

    public FoodListAdapter(Context context, ArrayList<FoodListItem> _objects) {
        super(context, 0, _objects);
        type = 0;       
    }

    public FoodListAdapter(Context context, ArrayList<FoodListItem> _objects, int _type) {
        super(context, 0, _objects);
        type = _type;       
    }

    @Override
    public View getView(int position, View reusableView, ViewGroup parent)
    {
        //Cast the reusable view to a listAdpaterItemView
        FoodListItemView listItemView = (FoodListItemView) reusableView;

        //Check if the listAdapterItem is null
        if(listItemView == null)
        {
            //If it is null, then create a view.
            listItemView = FoodListItemView.inflate(parent, type);
        }

        if (type == 2)
        {
            Button deleteButton = (Button) listItemView.findViewById(R.id.listItemViewDeleteBTN);
            deleteButton.setTag(new Integer(position));
            deleteButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Integer tag = (Integer) view.getTag();
                    deleteItem(tag.intValue());
                }
            });
        }

        //Now we need to set the view to display the data.
        listItemView.setData(getItem(position));

        return listItemView;
    }

    private void deleteItem(int position)
    {
        FoodListItem item = getItem(position);
        Global global = (Global) getContext().getApplicationContext();

        DietSQLiteHelper database = global.getDatabase();
        database.removeConsumed(item.getID());

        remove(getItem(position));
    }
}