O meu código está funcionando. Porém está com um bug. Quando um novo item é adicionado à lista, ele não aparece prontamente. Somente quando saio do aplicativo, usando o botão voltar, e abro o app novamente o item adicionado anteriormente na Activity de formulário aparece na listagem. Caso eu adicione um item e tente editar um outro outro qualquer já existente na listagem, sem sair do app primeiro, recebo essa exceção:
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131230848, class android.widget.ListView) with Adapter(class com.mithril.mobilegoldenleaf.adapters.ProductListAdapter)]
at android.widget.ListView.layoutChildren(ListView.java:1572)
at android.widget.AbsListView$CheckForTap.run(AbsListView.java:3287)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Segue o código da Activity de listagem e do BaseAdapter, respectivamente:
public class ProductsListActivity extends AppCompatActivity {
public static final String TITLE = "Lista de Produtos";
private ProductDAO dao = new ProductDAO();
private ProductListAdapter adapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_products_list);
setTitle(TITLE);
configureList();
FloatingActionButton new_product_fba = findViewById(R.id.activity_products_list_fab_new_product);
new_product_fba.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(ProductsListActivity.this, FormProductActivity.class));
}
});
}
@Override
protected void onResume() {
super.onResume();
updateProducts();
}
private void updateProducts() {
adapter.clear();
adapter.addAll(dao.get_products());
}
private void configureList() {
ListView product_list = findViewById(R.id.product_list_listview);
configureAdapter(product_list);
product_list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public class ProductListAdapter extends BaseAdapter {
private final List<Product> products = new ArrayList<>();
private Context context;
public ProductListAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return products.size();
}
@Override
public Product getItem(int position) {
return products.get(position);
}
@Override
public long getItemId(int i) {
return products.get(i).getId();
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
View createdView = LayoutInflater
.from(context)
.inflate(R.layout.item_product, viewGroup, false);
Product p = products.get(position);
TextView brand = createdView.findViewById(R.id.item_product_brand);
brand.setText(p.getBrand());
TextView description = createdView.findViewById(R.id.item_product_description);
description.setText(p.getDescription());
TextView value = createdView.findViewById(R.id.item_product_value);
value.setText(String.valueOf(p.getUnit_cost()));
TextView code = createdView.findViewById(R.id.item_product_code);
code.setText(p.getCode());
return createdView;
}
public void clear() {
products.clear();
}
public void addAll(List<Product> products) {
this.products.addAll(products);
}
}
Antes e depois de adicionar um item
Depois que saio do app e o abro novamente
Grato pela atenção.