带有过滤数据适配器的IndexOutOfBoundsException

时间:2014-04-01 10:08:26

标签: android android-fragments

好的 - 我理解我遇到的错误,但无法解决问题。我有一个FragmentActivity从SQLiteDabase中检索数据,然后是三个Fragments来过滤数据。

FragmentActivity

public static List<Item> values = null;
values = db.getItems();

FragmentOne

private ItemsByCategoryAdapter adapter;
private List<Item> v = FragmentActivity.values;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.show_items_by_subcategory_main, container, false);

    List<Item> filteredList = new ArrayList<Item>();
    for (Item b : v) {
        if (b.getItemPartner() == 1) {
            filteredList.add(b);
        }
    }

    adapter = new ItemsByCategoryAdapter(getActivity().getApplicationContext(), 0, (ArrayList<Item>) filteredList);
    list.setAdapter(adapter);

    list.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            adapter.updateIsInMyFavourites(position, true);
            adapter.notifyDataSetChanged();
        }
    });
}

FragmentTwo

private ItemsByCategoryAdapter adapter;
private List<Item> v = FragmentActivity.values;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.show_items_by_subcategory_main, container, false);

    List<Item> filteredDistanceList = new ArrayList<Item>();

    for (Item b : v) {
        _count ++;
        if (_count < 15) {
            filteredDistanceList.add(b);
        }
    }

    adapter = new ItemsByCategoryAdapter(getActivity().getApplicationContext(), 0, (ArrayList<Item>) filteredDistanceList);
    list.setAdapter(adapter);

    list.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            adapter.updateIsInMyFavourites(position, true);
            adapter.notifyDataSetChanged();
        }
    });
}

FragmentThree

private ItemsByCategoryAdapter adapter;
private List<Item> v = FragmentActivity.values;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.show_items_by_subcategory_main, container, false);

    adapter = new ItemsByCategoryAdapter(getActivity().getApplicationContext(), 0, (ArrayList<Item>) v);
    list.setAdapter(adapter);

    list.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            adapter.updateIsInMyFavourites(position, true);
            adapter.notifyDataSetChanged();
        }
    });

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

    switch (item.getItemId()) {
    case 1:
        adapter.updateIsInMyFavourites(info.position, true);
        adapter.notifyDataSetChanged();
        break;
    }

    return false;
}
}

因此,有时FragmentOne将从300中检索1条记录(FragmentThree中显示的所有300条记录)。 list.setOnItemClickListener正常工作并正确更新列表视图。但是当我把onContextItemSelected崩溃放在

之内时

适配器位于data.get(position)行。

public void updateIsInMyFavourites(int position, boolean value) {
    Item itemList = data.get(position);
    itemList.setIsInMyFavourites(value);
}

有人能指出我正确的方向来解决这个问题。它与适配器有关吗?

private ArrayList<Item> data = new ArrayList<Item>();

public ItemsByCategoryAdapter(Context context, int layoutResourceId, ArrayList<Item> d) {
    super(context, layoutResourceId, d);
    data = d;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

0 个答案:

没有答案