Android Listview不显示添加的项目

时间:2018-07-10 18:04:46

标签: android-layout android-studio listview android-arrayadapter

我的列表视图运行良好,并显示了我们拍照后添加的正在创建的项目。现在,由于某种原因,事实并非如此。 尤其奇怪的是,如果我将这些项目添加到最初创建和设置适配器的位置,那么手机会显示它们。但是当我稍后再这样做时,就不会了!

编辑-经过更多次尝试和错误之后,看来与数据库相关的命令有问题!参见下面的评论。

相关的JAVA代码

Cart _cart;
private AppDataBase _db;

@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart_review);
        init(getIntent().getStringExtra("calling activity"));
    }

private void init(String callingActivity)
    {
        _cart = new Cart();
        _pricingServices = new PricingServices(this);
        ArrayAdapter<Item> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, _cart.items);
        ListView itemListView = findViewById(R.id._dynamic_item_list);

        itemListView.setAdapter(adapter);
        _cart.items.add(new Item(5, "blame", 4)); //these work!!!!
        _cart.items.add(new Item(5, "bloom", 4)); //!!!!!!
        _db = Room.databaseBuilder(getApplicationContext(), AppDataBase.class, "class").fallbackToDestructiveMigration().allowMainThreadQueries().build();
        if(callingActivity.equals(MainActivity.class.getSimpleName()))
        {
            _cart = new Cart(); //When these are commented out
            _cart.setId((int)(_db.cartDao().insert(_cart))); //it seems to work
        }
        else
            _cart.items.addAll(_db.itemDao().getItemsByCartId(getIntent().getIntExtra("cart id", -1)));
    }

public void OnItemDescriptionDone(String description)
    {
        _description = description;
        Item item = new Item(_convertedPrice, _description, _cart.getId());
        _cart.items.add(item); //This doesn't update the UI !!!
    }

这是活动的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CartReviewActivity">

    <ListView
        android:id="@+id/_dynamic_item_list"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="24dp"
        android:layout_marginTop="24dp"
        app:layout_constraintBottom_toTopOf="@+id/button_capture_image"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textview_cart_name" />

    <ImageButton
        android:id="@+id/button_capture_image"
        android:layout_width="wrap_content"
        android:layout_height="55dp"
        android:layout_marginBottom="16dp"
        android:onClick="OnCameraButtonClick"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/camera_icon" />

    <TextView
        android:id="@+id/textview_cart_name"
        android:layout_width="0dp"
        android:layout_height="31dp"
        android:layout_marginEnd="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="16dp"
        android:text="The Cart name will be"
        android:textColor="@android:color/darker_gray"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

0 个答案:

没有答案