I'm new to android development and I'm stuck with the following issue:
I have objects in a listView. When an item in the list is clicked a detailed page with the information appears. This worked fine until at some point the text was displayed only sometimes. When I go back and click on the very same item the text might get displayed correctly again (or not). I have done a textView.getText() and it displays the correct text in the logcat but the user can't actually see this text displayed in the app (at least not always). I wasn't able to pinpoint the mistake and I cannot reproduce the mistake regularly.
FYI: The genre gets displayed always. But title and author only sometimes.
I am grateful for any help! If you have any codestyle/bestpractice remarks please add those to your answers.
Below you can find the relevant code.
Best, Marc
Activity passing the data
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();
}
}
Activity receiving the data
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();
}
}
Layout of sending activity
<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>
Layout of receiving activity
<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" />
</androidx.constraintlayout.widget.ConstraintLayout>
Edit
I restructured the layout of the receiving activity by changing it from a constraint layout to a relative. I don't know why this worked but it fixed my problem.