用自定义适配器和数据库处理程序更新listview

时间:2015-02-14 12:57:55

标签: database listview updates

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

@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();

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

1 个答案:

答案 0 :(得分:0)

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

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