我正在嘗試創建一個將動態生成的卡片串列,因為卡片中有一些東西需要從 FireStore 中提取。我已經完成了,但卡片顯示了兩次,實際卡片顯示正常,但后面有卡片布局,帶有預設文本和內容
更新:經過一些建議和幫助我更改了代碼,它仍然沒有做我想要的,但我想它是朝著正確方向邁出的一步
這是我用來創建卡片的代碼(更新了完整的活動)
public class ScrollingActivity extends AppCompatActivity {
FirebaseFirestore db = FirebaseFirestore.getInstance();
FirebaseAuth fAuth = FirebaseAuth.getInstance();
LinearLayout layout;
String name;
float rating;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
CollapsingToolbarLayout toolBarLayout = findViewById(R.id.toolbar_layout);
toolBarLayout.setTitle(getTitle());
for (int i = 0; i<6; i ){
addCard(i);
}
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(view -> Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show());
}
private void addCard(int workerNum) {
layout = findViewById(R.id.thisone);
View card = getLayoutInflater().inflate(R.layout.sample_card_1, layout, false);
TextView nameWorker = findViewById(R.id.nameWorker);
RatingBar RatingBar = findViewById(R.id.ratingBar);
Button submitButton = findViewById(R.id.submitButton);
DocumentReference docRef = db.collection("workers").document(String.valueOf(workerNum));
docRef.get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d(TAG, "DocumentSnapshot data: " document.getData());
name = document.toObject(Classes.Worker.class).getName();
nameWorker.setText(name);
rating = document.toObject(Classes.Worker.class).getRating();
RatingBar.setRating(rating);
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
});
// perform click event on button
submitButton.setOnClickListener(v -> {
DocumentReference changerating = db.collection("workers").document(String.valueOf(workerNum));
changerating.update("rating", RatingBar.getRating()).addOnSuccessListener(aVoid ->
Log.d(TAG, "DocumentSnapshot successfully updated!"))
.addOnFailureListener(e -> Log.w(TAG, "Error updating document", e));
// get values and then displayed in a toast
String rating1 = "On a scale of 5 Stars\n New rating = " RatingBar.getRating();
Toast.makeText(getApplicationContext(), rating1, Toast.LENGTH_LONG).show();
});
layout.addView(card);
}
這是實際顯示卡片的布局的xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true"
tools:context=".ScrollingActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@ id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/Theme.InternshipThesis.AppBarOverlay">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@ id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="@ id/toolbar">
<androidx.appcompat.widget.Toolbar
android:id="@ id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/Theme.InternshipThesis.PopupOverlay" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".ScrollingActivity"
tools:showIn="@layout/activity_scrolling">
<LinearLayout
android:id="@ id/thisone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">
<include layout="@layout/sample_card_1" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@ id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end"
app:srcCompat="@android:drawable/ic_menu_sort_by_size"
android:contentDescription="@string/main_fab_desc" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
這是卡片的布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@ id/card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="20dp"
app:cardElevation="2dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@ id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp">
<com.google.android.material.imageview.ShapeableImageView
android:id="@ id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
app:shapeAppearanceOverlay="@style/roundedImageView"
android:contentDescription="@string/Desc_Photo_Card1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/sample_worker_1" />
<TextView
android:id="@ id/nameWorker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Text1_Card1"
android:textSize="20sp"
app:layout_constraintStart_toEndOf="@ id/imageView"
app:layout_constraintTop_toTopOf="@ id/imageView"
android:padding="10dp"/>
<RatingBar
android:id="@ id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@ id/imageView"
app:layout_constraintStart_toEndOf="@ id/imageView"
app:layout_constraintTop_toBottomOf="@ id/nameWorker" />
<com.google.android.material.button.MaterialButton
android:id="@ id/submitButton"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="10dp"
android:text="@string/rating_button"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold"
app:cornerRadius="20dp"
app:layout_constraintEnd_toEndOf="@ id/ratingBar"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@ id/ratingBar"
app:layout_constraintTop_toBottomOf="@ id/ratingBar" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
這是它當前在應用程式中的顯示方式
示例 1
如果我沒有在活動的 xml 中包含卡片的布局,則應用程式會崩潰,因為它無法在卡片中找到要填充資料的物件,但如果我顯然包含它,它會創建一張正確填充的卡片,而其他所有卡片那些只是填充了預設資料
uj5u.com熱心網友回復:
您遇到的問題取決于您的AddCard功能。您正在膨脹card_view,但不是在新card_view的(變數card)內部查看以填充資料的視圖,而是在根視圖內部(如果您只是呼叫findViewById,它將在片段的根視圖內部查看/活動默認)。
為了解決這個問題:
- 從您的主視圖中洗掉此行:
<include layout="@layout/sample_card_1" />,因為它不是必需的(您的代碼應該動態添加卡片) - 修改您的
AddCard函式,并將這三行更改如下:
TextView nameWorker = card.findViewById(R.id.nameWorker);
RatingBar RatingBar = card.findViewById(R.id.ratingBar);
Button submitButton = card.findViewById(R.id.submitButton);
通過將呼叫更改為findViewById呼叫card您膨脹的視圖,您將停止查看根視圖內部(這將解決您參考的那些崩潰),而是使用您想要的詳細資訊填寫新卡(這將解決您在填寫一張卡而其余為默認值的問題)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/447457.html
