Android - TextView setText有时只更新文本

0 人关注

我是安卓开发的新手,我被以下问题困扰。

我在一个listView中拥有对象。当列表中的一个项目被点击时,会出现一个包含信息的详细页面。这一切都很好,直到有一天,文本只显示在某些地方。当我回去点击同一个项目时,文本可能会再次正确显示(或不显示)。我做了一个textView.getText(),它在logcat中显示了正确的文本,但用户实际上不能看到这个文本在应用程序中显示(至少不总是)。我没能指出这个错误,我也不能定期重现这个错误。

提示:流派总是被显示出来。但有时只显示标题和作者。

我很感谢任何帮助!如果你有任何代码风格/最佳实践的意见,请在你的答案中加入这些。

下面你可以找到相关代码。

传递数据的活动

public class BooksActivity extends AppCompatActivity {
    private SQLiteDatabase db;
    private final BooksDBOpenHelper dbHelper = new BooksDBOpenHelper(this, DBConstants.DB_NAME, null, 1);
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_books);
        TextView header = this.findViewById(R.id.tv_header);
        header.setText(R.string.books_list);
        ImageButton backButton = this.findViewById(R.id.toolbar_back_button);
        backButton.setOnClickListener(view -> finish());
    @Override
    protected void onStart() {
        super.onStart();
        db = dbHelper.getWritableDatabase();
        ArrayList<CustomBookItem> bookItems = new ArrayList<>();
        String table_name = DBConstants.Books.TABLE_NAME;
        String[] columns = {
                DBConstants.Books.COLUMN_NAME_TITLE,
                DBConstants.Books.COLUMN_NAME_AUTHOR,
                DBConstants.Books.COLUMN_NAME_GENRE,
                DBConstants.Books.COLUMN_NAME_ON_LOAN,
                DBConstants.ID
        String where = null;
        String[] where_args = null;
        String group_by = null;
        String having = null;
        String order_by = null;
        Cursor cursor = db.query(table_name, columns, where, where_args, group_by, having, order_by);
        while (cursor.moveToNext()) {
            bookItems.add(new CustomBookItem(
                    cursor.getString(0),
                    cursor.getString(1),
                    cursor.getString(2),
                    cursor.getString(3),
                    cursor.getString(4)
        cursor.close();
        ListView lvMainList = findViewById(R.id.lv_books_list);
        CustomArrayAdapter customArrayAdapter = new CustomArrayAdapter(this, bookItems);
        lvMainList.setAdapter(customArrayAdapter);
        lvMainList.setOnItemClickListener((adapterView, view, pos, id) -> {
            Log.i("BooksActivity", "item was clicked");
            CustomBookItem bookItem = (CustomBookItem) adapterView.getAdapter().getItem(pos);
            Intent intent = new Intent(this, BookActivity.class);
            intent.putExtra(DBConstants.Books.COLUMN_NAME_TITLE, bookItem.getTitle());
            intent.putExtra(DBConstants.Books.COLUMN_NAME_AUTHOR, bookItem.getAuthor());
            intent.putExtra(DBConstants.Books.COLUMN_NAME_GENRE, bookItem.getGenre());
            intent.putExtra(DBConstants.Books.COLUMN_NAME_ON_LOAN, bookItem.getOnLoan());
            intent.putExtra(DBConstants.ID, bookItem.getId());
            startActivity(intent);
        customArrayAdapter.notifyDataSetChanged();
    @Override
    protected void onDestroy() {
        super.onDestroy();
        db.close();

接收数据的活动

public class BookActivity extends AppCompatActivity {
    private Resources resources;
    private final BooksDBOpenHelper dbHelper = new BooksDBOpenHelper(this, DBConstants.DB_NAME, null, 1);
    private SQLiteDatabase db;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_book);
        this.resources = this.getResources();
        TextView header = this.findViewById(R.id.tv_header);
        header.setText(R.string.book);
        ImageButton backButton = this.findViewById(R.id.toolbar_back_button);
        backButton.setOnClickListener(view -> finish());
    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = this.getIntent();
        String id = intent.getStringExtra(DBConstants.ID);
        TextView tvTitle = this.findViewById(R.id.book_title);
        tvTitle.setText(intent.getStringExtra(DBConstants.Books.COLUMN_NAME_TITLE));
        Log.d("BookActivity", tvTitle.getText().toString());
        TextView tvAuthor = this.findViewById(R.id.book_author);
        tvAuthor.setText(intent.getStringExtra(DBConstants.Books.COLUMN_NAME_AUTHOR));
        Log.d("BookActivity", tvAuthor.getText().toString());
        TextView tvGenre = this.findViewById(R.id.book_genre);
        tvGenre.setText(intent.getStringExtra(DBConstants.Books.COLUMN_NAME_GENRE));
        Log.d("BookActivity", tvGenre.getText().toString());
        View loanBookButton = this.findViewById(R.id.loanBookButton);
        View returnBookButton = this.findViewById(R.id.returnBookButton);
        if (intent.getStringExtra(DBConstants.Books.COLUMN_NAME_ON_LOAN).equals("0")) {
            returnBookButton.setBackgroundColor(this.resources.getColor(R.color.grayed_out));
            returnBookButton.setEnabled(false);
        } else {
            loanBookButton.setBackgroundColor(this.resources.getColor(R.color.grayed_out));
            loanBookButton.setEnabled(false);
        loanBookButton.setOnClickListener(view -> loanBook(id));
        returnBookButton.setOnClickListener(view -> returnBook(id));
    public void returnBook(String id) {
        this.db = this.dbHelper.getReadableDatabase();
        this.db.execSQL("UPDATE " + DBConstants.Books.TABLE_NAME +
                " SET " + DBConstants.Books.COLUMN_NAME_ON_LOAN + "='0' " +
                "WHERE id=" + id);
        this.finish();
    public void loanBook(String id) {
        this.db = this.dbHelper.getReadableDatabase();
        this.db.execSQL("UPDATE " + DBConstants.Books.TABLE_NAME +
                " SET " + DBConstants.Books.COLUMN_NAME_ON_LOAN + "='1' " +
                "WHERE id=" + id);
        this.finish();

发送活动的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <include
        android:id="@+id/container_header_lyt"
        layout="@layout/toolbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"/>
    <ListView
        android:id="@+id/lv_books_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/container_header_lyt"/>
</RelativeLayout>

接收活动的布局

<androidx.constraintlayout.widget.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">
    <include
        android:id="@+id/container_header_lyt"
        layout="@layout/toolbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"/>
    <Button
        android:id="@+id/loanBookButton"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:layout_marginStart="64dp"
        android:layout_marginTop="580dp"
        android:layout_marginEnd="229dp"
        android:layout_marginBottom="103dp"
        android:text="@string/loan_book_button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:id="@+id/returnBookButton"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:layout_marginStart="229dp"
        android:layout_marginTop="580dp"
        android:layout_marginEnd="64dp"
        android:layout_marginBottom="103dp"
        android:text="@string/return_book_button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <TextView
        android:id="@+id/book_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="176dp"
        android:layout_marginTop="174dp"
        android:layout_marginEnd="176dp"
        android:layout_marginBottom="487dp"
        android:textAlignment="center"
        android:textSize="32sp"
        app:layout_constraintBottom_toTopOf="@+id/loanBookButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <TextView
        android:id="@+id/book_author"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="176dp"
        android:layout_marginTop="69dp"
        android:layout_marginEnd="176dp"
        android:layout_marginBottom="399dp"
        android:textSize="28sp"
        android:textAlignment="center"
        app:layout_constraintBottom_toTopOf="@+id/loanBookButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/book_title" />
    <TextView
        android:id="@+id/book_genre"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="176dp"
        android:layout_marginTop="69dp"
        android:layout_marginEnd="176dp"
        android:layout_marginBottom="311dp"
        android:textSize="28sp"
        android:textAlignment="center"
        app:layout_constraintBottom_toTopOf="@+id/loanBookButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/book_author" />