使用自定义适配器和数据库处理程序更新列表视图

Update listview with custom adapter and database handler

我无法使用自定义适配器和数据库处理程序更新列表视图。 项目在数据库处理程序中更新,但列表视图仅在我离开片段并返回时更新。我想在添加新项目后立即看到变化。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.shopping_list_fragment_layout, container, false);
    super.onCreate(savedInstanceState);

    //// adding an item to itemList
    nameTxt = (EditText) view.findViewById(R.id.txtItem);
    couponsAmount = 3;
    dbHandler = new ShoppingListDatabaseHandler(getActivity());

    itemListView = (ListView) view.findViewById(R.id.listViewShoppingItems);
    final ShoppingListListAdapter itemAdapter = new ShoppingListListAdapter(getActivity(), ITEMS_DATA);

    final ImageButton addBtn = (ImageButton) view.findViewById(R.id.btnAddItem);
    addBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view){
            ShoppingListItem item = new ShoppingListItem(dbHandler.getItemsCount(), String.valueOf(nameTxt.getText()), 3);

            if (!itemExists(item)) {
                dbHandler.createItem(item);
                //getListView().invalidateViews();
                itemAdapter.notifyDataSetChanged(); // TODO: is not updating
                Toast.makeText(getActivity(), String.valueOf(nameTxt.getText()) + " has been added", Toast.LENGTH_SHORT).show();
                nameTxt.setText("");
                return;
            }
            Toast.makeText(getActivity(), String.valueOf(nameTxt.getText()) + " already exists", Toast.LENGTH_SHORT).show();
            nameTxt.setText("");
        }
    });

    if (dbHandler.getItemsCount() != 0){
        ITEMS_DATA.addAll(dbHandler.getAllItems());
    }

    //itemListView.getListView().invalidateViews();
    //itemAdapter.notifyDataSetChanged();
    itemListView.setAdapter(itemAdapter);
    return view;
}

和我的适配器:

public class ShoppingListListAdapter extends BaseAdapter {

private Context context;

private final List<ShoppingListItem> Items; // = new ArrayList<ShoppingListItem>();

//Constructor to initialize values
public ShoppingListListAdapter(Context context, List<ShoppingListItem> Items) {

    this.context        = context;
    this.Items        = Items;
}


@Override
public int getCount() {

    // Number of times getView method call depends upon
    return Items.size();
}

@Override
public Object getItem(int position) {

    return null;
}

@Override
public long getItemId(int position) {

    return 0;
}


// Number of times getView method call depends upon

public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;

    if (convertView == null) {

        gridView = new View(context);

        gridView = inflater.inflate(R.layout.shopping_list_list_item, null);

        ShoppingListItem currentItem = Items.get(position);

        TextView itemNameTxt = (TextView) gridView.findViewById(R.id.itemName);
        itemNameTxt.setText(currentItem.getName());

        TextView couponsAmount = (TextView) gridView.findViewById(R.id.couponsAmount);
        couponsAmount.setText("-" + currentItem.getCouponsAmount().toString());


       }
       else {
       gridView = (View) convertView;
       }

    return gridView;
}

}

我试过: - getListView().invalidateViews(); - adapter.notifyDataSetChanged();

但可能我做错了什么,因为它对我没有帮助。 你能看看我的代码吗?

您已将项目添加到数据库中,但未添加到用于创建适配器的列表中。

ITEM_DATA.add(item):
itemAdapter.notifyDataSetChanged();