我想从Web服务器显示我的Pizza&Drink Objects,但我的RecyclerView只能直接加载图像,而Scroll上的TextViews。我为每个适配器调用notifyDataSetChanged(),甚至通过迭代通知适配器每个项目。我正在使用Glide来改变我的图像。
为什么我的TextViews没有加载?
main activity.Java
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
PizzaDataAdapter pizzaDataAdapter;
DrinkDataAdapter drinkDataAdapter;
CartDataAdapter cartDataAdapter;
ImageView emptyListImageView;
TextView emptyListTextView;
Toolbar mActionBarToolbar;
ConstraintLayout cartIntLayout;
RecyclerView product_recyclerview;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_food:
mActionBarToolbar.setTitle("Pizzas");
FrameLayout pizzas_layout = findViewById(R.id.pizzas_layout);
FrameLayout drinks_layout = findViewById(R.id.drinks_layout);
ConstraintLayout cart_layout = findViewById(R.id.cart_layout);
drinks_layout.setVisibility(GONE);
cart_layout.setVisibility(GONE);
pizzas_layout.setVisibility(View.VISIBLE);
return true;
case R.id.navigation_drinks:
mActionBarToolbar.setTitle("Drinks");
drinks_layout = findViewById(R.id.drinks_layout);
pizzas_layout = findViewById(R.id.pizzas_layout);
cart_layout = findViewById(R.id.cart_layout);
pizzas_layout.setVisibility(GONE);
cart_layout.setVisibility(GONE);
drinks_layout.setVisibility(View.VISIBLE);
return true;
case R.id.navigation_cart:
mActionBarToolbar.setTitle("Cart");
pizzas_layout = findViewById(R.id.pizzas_layout);
drinks_layout = findViewById(R.id.drinks_layout);
cart_layout = findViewById(R.id.cart_layout);
initCartViews();
pizzas_layout.setVisibility(GONE);
drinks_layout.setVisibility(GONE);
cart_layout.setVisibility(View.VISIBLE);
return true;
}
return false;
}
};
public static void start(Context context) {
Intent starter = new Intent(context, MainActivity.class);
context.startActivity(starter);
}
@Override
protected void onCreate(Bundle savedInstanceState) throws NullPointerException {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cartIntLayout = findViewById(R.id.cart_int_layout);
cartIntLayout.bringToFront();
mActionBarToolbar = findViewById(R.id.my_toolbar);
mActionBarToolbar.setTitle("Pizzas");
mActionBarToolbar.setTitleTextColor(getResources().getColor(R.color.white, this.getTheme()));
setSupportActionBar(mActionBarToolbar);
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
PizzaLoadingTask.TaskListener pizzaListener = new PizzaLoadingTask.TaskListener() {
@Override
public void onComplete(List<Pizza> pizzas) {
if (pizzas.isEmpty()) {
initViewsWithEmptyList();
}
initPizzaViews(pizzas);
}
};
new PizzaLoadingTask(pizzaListener, APIClient.getInstance()).execute();
DrinkLoadingTask.TaskListener drinkListener = new DrinkLoadingTask.TaskListener() {
@Override
public void onComplete(List<Drink> drinks) {
if (drinks.isEmpty()) {
initViewsWithEmptyList();
}
initDrinkViews(drinks);
}
};
new DrinkLoadingTask(drinkListener, APIClient.getInstance()).execute();
}
private void initPizzaViews(List<Pizza> pizzas) {
pizzaDataAdapter = new PizzaDataAdapter(pizzas);
recyclerView = findViewById(R.id.card_recycler_view_pizzas);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new DividerItemDecoration(getBaseContext(),
DividerItemDecoration.VERTICAL));
pizzaDataAdapter.notifyDataSetChanged();
recyclerView.setAdapter(pizzaDataAdapter);
}
private void initDrinkViews(List<Drink> drinks) {
recyclerView = findViewById(R.id.card_recycler_view_drinks);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new DividerItemDecoration(getBaseContext(),
DividerItemDecoration.VERTICAL));
drinkDataAdapter = new DrinkDataAdapter(drinks);
drinkDataAdapter.notifyDataSetChanged();
recyclerView.setAdapter(drinkDataAdapter);
}
private void initCartViews() {
product_recyclerview = findViewById(R.id.cart_products_recyclerview);
product_recyclerview.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
product_recyclerview.setLayoutManager(layoutManager);
cartDataAdapter = new CartDataAdapter(pizzaDataAdapter.getCartPizzas(), drinkDataAdapter.getCartDrinks());
product_recyclerview.setAdapter(cartDataAdapter);
}
private void initViewsWithEmptyList() {
recyclerView = findViewById(R.id.card_recycler_view_drinks);
recyclerView.setVisibility(GONE);
emptyListImageView = findViewById(R.id.pizza_image_empty_list);
emptyListTextView = findViewById(R.id.pizza_empty_textview);
emptyListImageView.setVisibility(View.VISIBLE);
emptyListTextView.setVisibility(View.VISIBLE);
}
@Override
public void onBackPressed() {
super.onBackPressed();
SharedPreferences preferences = getSharedPreferences("loginRecognizer", MODE_PRIVATE);
if (preferences.contains("loggedIn")) {
Intent i = new Intent(this, LoginActivity.class); // Your list's Intent
i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY); // Adds the FLAG_ACTIVITY_NO_HISTORY flag
startActivity(i);
}
}
}
PizzaDataAdapter.java(DrinkDataAdapter相同,但只是与Drink-Objects一起)
public class PizzaDataAdapter extends RecyclerView.Adapter<PizzaDataAdapter.ViewHolder> {
private List<Pizza> pizzas;
//TODO implement cartDrinks LinkedList
private List<Pizza> cartPizzas = new LinkedList<>();
PizzaDataAdapter(List<Pizza> pizzas) {
this.pizzas = pizzas;
}
private static BigDecimal round(float d) {
BigDecimal bd = new BigDecimal(Float.toString(d));
bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
return bd;
}
@Override
public PizzaDataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.pizza_card_row, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(PizzaDataAdapter.ViewHolder viewHolder, int position) {
final Pizza pizza = pizzas.get(position);
// viewHolder.setIsRecyclable(false);
//viewHolder.pizza_image.setText(pizzas.get(i).getId().toString());
viewHolder.pizza_name.setText(pizza.getName());
BigDecimal result = round(pizza.getPrice());
String price = "Price: " + result.toString();
viewHolder.pizza_price.setText(price);
GlideApp.with(viewHolder.pizza_image.getContext())
.load(pizzas.get(position).getImagepath())
.apply(RequestOptions.circleCropTransform())
.into(viewHolder.pizza_image);
viewHolder.pizza_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(viewHolder.pizza_layout.getContext(), PizzaDetailActivity.class);
intent.putExtra("Name", pizzas.get(position).getName());
intent.putExtra("Price", pizzas.get(position).getPrice());
intent.putExtra("Ingredients", pizzas.get(position).getIngredients());
viewHolder.pizza_layout.getContext().startActivity(intent);
}
});
viewHolder.plus_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cartPizzas.add(pizzas.get(position));
}
});
}
@Override
public int getItemCount() {
return pizzas.size();
}
public List<Pizza> getCartPizzas() {
return cartPizzas;
}
class ViewHolder extends RecyclerView.ViewHolder {
private ImageView pizza_image;
private TextView pizza_name, pizza_price;
private ImageButton plus_button;
private ConstraintLayout pizza_layout;
ViewHolder(View view) {
super(view);
pizza_image = view.findViewById(R.id.pizza_image);
pizza_name = view.findViewById(R.id.pizza_name);
pizza_price = view.findViewById(R.id.pizza_price);
plus_button = view.findViewById(R.id.pizza_plus_button);
pizza_layout = view.findViewById(R.id.pizza_card_row_layout);
}
}
}
pizza loading task.Java
public class PizzaLoadingTask extends AsyncTask<Void, Void, List<Pizza>> {
private APIClient apiClient;
private TaskListener mListener;
public PizzaLoadingTask(TaskListener listener, APIClient apiClient){
this.mListener=listener;
this.apiClient = apiClient;
}
@Override
protected List<Pizza> doInBackground(Void... voids) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(APIInterface.baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
APIInterface request = retrofit.create(APIInterface.class);
Call<List<Pizza>> call = request.getPizzas();
try {
Response<List<Pizza>> jsonResponse = call.execute();
return jsonResponse.body();
// return Collections.emptyList();
} catch (IOException | NullPointerException e) {
e.printStackTrace();
return Collections.emptyList();
}
}
@Override
protected void onPostExecute(List<Pizza> pizzas) {
mListener.onComplete(pizzas);
}
public interface TaskListener {
void onComplete(List<Pizza> pizzas);
}
}
activity_main.xml中
<?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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".screens.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.constraint.ConstraintLayout
android:id="@+id/navigation_layout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/my_toolbar"
app:layout_constraintVertical_bias="0.0">
<include
android:id="@+id/pizzas_layout"
layout="@layout/pizza_products"
android:visibility="visible" />
<include
android:id="@+id/drinks_layout"
layout="@layout/drink_products"
android:visibility="invisible" />
<include
android:id="@id/cart_layout"
layout="@layout/product_cart"
android:visibility="invisible" />
</android.support.constraint.ConstraintLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation">
</android.support.design.widget.BottomNavigationView>
<android.support.constraint.ConstraintLayout
android:id="@+id/cart_int_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="57dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/navigation">
<TextView
android:id="@+id/cart_int_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textColor="#000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
pizza_products.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pizza_products_layout"
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="com.apprentice.ti8m.myfirstrestclient.screens.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/card_recycler_view_pizzas"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp"
tools:listitem="@layout/pizza_card_row">
</android.support.v7.widget.RecyclerView>
<android.support.constraint.ConstraintLayout
android:id="@+id/pizza_image_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<ImageView
android:id="@+id/pizza_image_empty_list"
android:layout_width="256dp"
android:layout_height="256dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="@+id/pizza_empty_textview"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.176"
app:layout_constraintVertical_chainStyle="spread"
app:srcCompat="@drawable/ic_pizza_empty_list"
tools:visibility="visible"
android:contentDescription="@string/image_to_show_up_if_pizza_list_is_empty" />
<TextView
android:id="@+id/pizza_empty_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pizza_list_empty"
android:visibility="gone"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pizza_image_empty_list"
app:layout_constraintVertical_bias="0.567"
tools:visibility="visible" />
</android.support.constraint.ConstraintLayout>
</FrameLayout>
pizza_card_row.xml
<android.support.constraint.ConstraintLayout
android:id="@+id/pizza_card_row_layout"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="72dp">
<ImageView
android:id="@+id/pizza_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/pizza_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="0dp"
android:fontFamily="sans-serif"
android:textSize="16sp"
android:textColor="@color/colorPizzaName"
app:layout_constraintHorizontal_bias="0.1"
app:layout_constraintLeft_toRightOf="@+id/pizza_image"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/pizza_image" />
<TextView
android:id="@+id/pizza_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="-1dp"
app:layout_constraintBottom_toBottomOf="@+id/pizza_image"
app:layout_constraintLeft_toRightOf="@+id/pizza_image"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:fontFamily="sans-serif"
android:textSize="14sp"
app:layout_constraintRight_toRightOf="parent" />
<ImageButton
android:id="@+id/pizza_plus_button"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="72dp"
android:layout_height="0dp"
android:layout_gravity="center"
android:src="@drawable/ic_add_black_24dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:contentDescription="@string/plus_button_for_each_recyclerview_card" />
</android.support.constraint.ConstraintLayout>
尝试删除这个不必要的代码:
for (int i = 0; i< pizzas.size(); i++){
pizzaDataAdapter.notifyItemInserted(i);
}
因为在适配器中数据完成/加载后设置了recyclerview适配器