android - 如何在多个 TextView 中显示数组的内容?
问题描述
我正在尝试研究如何将以下数组 recipeDetail 的内容显示到以下 TextViews 中。我已经有一个用于 recyclerview 的适配器,只是想弄清楚如何填充其他 TextView。如果需要,我可以提供其他代码,例如。完整的类代码或 recyclerView 适配器。
XML
<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">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="409dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Recipe" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Meal Plan" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shopping List" />
</com.google.android.material.tabs.TabLayout>
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<ImageView
android:id="@+id/recipeImage"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/guideline4"
app:layout_constraintEnd_toStartOf="@+id/guideline3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tabLayout" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.33" />
<TextView
android:id="@+id/descriptionText"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/guideline4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/recipeImage"
app:layout_constraintTop_toBottomOf="@+id/recipeName" />
<TextView
android:id="@+id/courseText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:text="TextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline4" />
<TextView
android:id="@+id/servingsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:text="TextView"
app:layout_constraintStart_toStartOf="@+id/guideline3"
app:layout_constraintTop_toTopOf="@+id/guideline4" />
<TextView
android:id="@+id/costText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/courseText" />
<TextView
android:id="@+id/caloriesText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintStart_toStartOf="@+id/guideline3"
app:layout_constraintTop_toBottomOf="@+id/servingsText" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/listIngredient"
android:name="com.stu54259.plan2cook.Recipe"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:layoutManager="LinearLayoutManager"
app:layout_constraintBottom_toTopOf="@+id/guideline7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/caloriesText"
tools:context=".Recipe"
tools:listitem="@layout/fragment_item">
</androidx.recyclerview.widget.RecyclerView>
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.66" />
<TextView
android:id="@+id/methodText"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/listIngredient" />
<TextView
android:id="@+id/recipeName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:text="TextView"
android:textSize="24dp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/recipeImage"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tabLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
爪哇
public void loadRecipe() {
recipeDetail.clear();
db = (new DatabaseManager(this).getWritableDatabase());
String query = "";
String LOAD_RECIPE = " SELECT recipe_name, description, course, servings, calories, preparation_time, method, category, image, cost FROM " + TABLE_RECIPE;
query = LOAD_RECIPE + " WHERE recipe_name LIKE ?";
c = db.rawQuery(query, new String[]{"%" + search_name + "%"});
if (cur.moveToFirst()) {
do {
com.stu54259.plan2cook.Model.Recipe recipe = new com.stu54259.plan2cook.Model.Recipe();
recipe.setRecipe_name(cur.getString(cur.getColumnIndex("recipe_name")));
recipe.setDescription(cur.getString(cur.getColumnIndex("description")));
recipe.setCourse(cur.getString(cur.getColumnIndex("course")));
recipe.setServings(cur.getInt(cur.getColumnIndex("servings")));
recipe.setCalories(cur.getDouble(cur.getColumnIndex("calories")));
recipe.setPreparation_time(cur.getString(cur.getColumnIndex("preparation_time")));
recipe.setMethod(cur.getString(cur.getColumnIndex("method")));
recipe.setCategory(cur.getString(cur.getColumnIndex("category")));
recipe.setImage(cur.getString(cur.getColumnIndex("image")));
recipe.setCost(cur.getDouble(cur.getColumnIndex("cost")));
recipeDetail.add(recipe);
} while (c.moveToNext());
cur.close();
}
}
更新
package com.stu54259.plan2cook;
import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.tabs.TabLayout;
import com.stu54259.plan2cook.Model.RecipeList;
import com.stu54259.plan2cook.database.DatabaseManager;
import java.util.ArrayList;
import java.util.List;
import static com.stu54259.plan2cook.database.DatabaseManager.*;
public class Recipe extends MainActivity {
TabLayout tabLayout;
ImageView recipeImage;
TextView recipeName, descriptionText, courseText, servingsText, costText, caloriesText, methodText;
RecyclerView listIngredient;
SQLiteDatabase db;
String search_name;
Cursor c, cur;
RecyclerViewAdapter adapterRecipe;
List<RecipeList> itemRecipe = new ArrayList<>();
List<com.stu54259.plan2cook.Model.Recipe> recipeDetail = new ArrayList<>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipe);
//search_name = getIntent().getStringExtra("NAME");
search_name = "Speedy chicken couscous";
loadIngredient();
//recyclerview Recipe
adapterRecipe = new RecyclerViewAdapter(this, itemRecipe);
listIngredient = findViewById(R.id.listIngredient);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this,
LinearLayoutManager.VERTICAL, false);
listIngredient.setLayoutManager(mLayoutManager);
listIngredient.setItemAnimator(new DefaultItemAnimator());
listIngredient.setAdapter(adapterRecipe);
loadRecipe();
// Code not completed, just thought something like this might work
recipeName = findViewById(R.id.recipeName);
recipeName.setText(getRecipe_name());
}
public void loadIngredient() {
itemRecipe.clear();
db = (new DatabaseManager(this).getWritableDatabase());
String RECIPE_SEARCH = " SELECT A.recipe, A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description " +
"FROM " + DatabaseManager.TABLE_QUANTITY + " AS A JOIN " + DatabaseManager.TABLE_INGREDIENTS +
" AS B ON A.ingredient = B.ingredient_name";
String selectQuery = "";
selectQuery = RECIPE_SEARCH + " WHERE A.recipe LIKE ?";
c = db.rawQuery(selectQuery, new String[]{"%" + search_name + "%"});
if (c.moveToFirst()) {
do {
RecipeList recipeList = new RecipeList();
recipeList.setRecipe(c.getString(c.getColumnIndex("recipe")));
recipeList.setIngredient_amount(c.getString(c.getColumnIndex("ingredient_quantity")));
recipeList.setMeasurement_name(c.getString(c.getColumnIndex("measurement_name")));
recipeList.setIngredient_name(c.getString(c.getColumnIndex("ingredient_name")));
recipeList.setDescription(c.getString(c.getColumnIndex("description")));
itemRecipe.add(recipeList);
} while (c.moveToNext());
c.close();
}
}
public void loadRecipe() {
recipeDetail.clear();
db = (new DatabaseManager(this).getWritableDatabase());
String query = "";
String LOAD_RECIPE = " SELECT recipe_name, description, course, servings, calories, preparation_time, method, category, image, cost FROM " + TABLE_RECIPE;
query = LOAD_RECIPE + " WHERE recipe_name LIKE ?";
c = db.rawQuery(query, new String[]{"%" + search_name + "%"});
if (cur.moveToFirst()) {
do {
com.stu54259.plan2cook.Model.Recipe recipe = new com.stu54259.plan2cook.Model.Recipe();
recipe.setRecipe_name(cur.getString(cur.getColumnIndex("recipe_name")));
recipe.setDescription(cur.getString(cur.getColumnIndex("description")));
recipe.setCourse(cur.getString(cur.getColumnIndex("course")));
recipe.setServings(cur.getInt(cur.getColumnIndex("servings")));
recipe.setCalories(cur.getDouble(cur.getColumnIndex("calories")));
recipe.setPreparation_time(cur.getString(cur.getColumnIndex("preparation_time")));
recipe.setMethod(cur.getString(cur.getColumnIndex("method")));
recipe.setCategory(cur.getString(cur.getColumnIndex("category")));
recipe.setImage(cur.getString(cur.getColumnIndex("image")));
recipe.setCost(cur.getDouble(cur.getColumnIndex("cost")));
recipeDetail.add(recipe);
} while (c.moveToNext());
cur.close();
}
}
}