gpt4 book ai didi

android - 当编译器没有给出任何错误时,为什么 RecyclerView 不工作?

转载 作者:行者123 更新时间:2023-12-03 20:04:01 24 4
gpt4 key购买 nike

通常,我不会问这样的问题;我做了我的研究,至少能够在 StackOverflow 上提问之前找出问题所在。但这一次,即使经过彻底研究和一遍又一遍地查看我的代码,我什至无法确定问题所在。因此,对于提出如此广泛的问题,我深表歉意。
我的应用程序中有一些 RecyclerViews(准确地说是 9 个)。其中一个正在工作,八个没有。我一遍又一遍地查看我的代码,但我似乎无法确定问题所在。我将粘贴一个回收器 View 及其适配器的代码。这是我对单个元素的布局:
enter image description here
这是我的单个元素布局的代码(我只是发布它让人们知道ID):

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#0099ff">

<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/left_guideline_view"
app:layout_constraintGuide_percent=".12077"
android:orientation="vertical"/>

<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/right_guideline_view"
app:layout_constraintGuide_percent=".87923"
android:orientation="vertical"/>

<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/left_guideline_image_view"
app:layout_constraintGuide_percent=".16908"
android:orientation="vertical"/>

<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/top_guideline_image_view"
app:layout_constraintGuide_percent=".14706"
android:orientation="horizontal"/>

<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/left_guideline_text_view"
app:layout_constraintGuide_percent=".37198"
android:orientation="vertical"/>

<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/right_guideline_text_view"
app:layout_constraintGuide_percent=".83"
android:orientation="vertical"/>

<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/top_guideline_text_view_order"
app:layout_constraintGuide_percent=".13235"
android:orientation="horizontal"/>

<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bottom_guideline_text_view_order"
app:layout_constraintGuide_percent=".38235"
android:orientation="horizontal"/>

<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bottom_guideline_text_view_planter"
app:layout_constraintGuide_percent=".61765"
android:orientation="horizontal"/>

<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/left_guideline_mark_fulfilled_icon"
app:layout_constraintGuide_percent=".32367"
android:orientation="vertical"/>

<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/right_guideline_mark_fulfilled_icon"
app:layout_constraintGuide_percent=".43961"
android:orientation="vertical"/>

<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/top_guideline_icon"
app:layout_constraintGuide_percent=".74265"
android:orientation="horizontal"/>

<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bottom_guideline_icon"
app:layout_constraintGuide_percent=".96324"
android:orientation="horizontal"/>

<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/left_guideline_message_icon"
app:layout_constraintGuide_percent=".58696"
android:orientation="vertical"/>

<androidx.constraintlayout.widget.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/right_guideline_message_icon"
app:layout_constraintGuide_percent=".70290"
android:orientation="vertical"/>

<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/left_border"
android:background="#0099ff"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="@id/left_guideline_view"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent">

<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099ff" />

</FrameLayout>

<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/right_border"
android:background="#0099ff"
app:layout_constraintLeft_toLeftOf="@id/right_guideline_view"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent">

<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099ff" />

</FrameLayout>

<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/middle"
android:paddingTop="8dp"
android:paddingBottom="8dp"
app:layout_constraintLeft_toLeftOf="@+id/left_guideline_view"
app:layout_constraintRight_toRightOf="@id/right_guideline_view"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent">

<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/order_planter_message_shape" />

</FrameLayout>

<com.mikhaellopez.circularimageview.CircularImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:id="@+id/image_view"
app:civ_border_color="#ffffff"
app:civ_border_width="1dp"
app:civ_shadow="false"
app:layout_constraintLeft_toLeftOf="@+id/left_guideline_image_view"
app:layout_constraintTop_toTopOf="@+id/top_guideline_image_view" />

<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/text_view_order"
android:text="# Trees"
android:fontFamily="@font/inter_semibold"
android:textSize="24sp"
android:textColor="#ffffff"
android:textAllCaps="false"
android:gravity="bottom|start"
app:layout_constraintLeft_toLeftOf="@+id/left_guideline_text_view"
app:layout_constraintRight_toRightOf="@+id/right_guideline_text_view"
app:layout_constraintTop_toTopOf="@+id/top_guideline_text_view_order"
app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline_text_view_order"/>

<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/text_view_planter"
android:text="David Malan, $20"
android:fontFamily="@font/inter_regular"
android:textSize="18sp"
android:textColor="#ffffff"
android:textAllCaps="false"
android:gravity="bottom|start"
app:layout_constraintLeft_toLeftOf="@+id/left_guideline_text_view"
app:layout_constraintRight_toRightOf="@+id/right_guideline_text_view"
app:layout_constraintTop_toTopOf="@+id/bottom_guideline_text_view_order"
app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline_text_view_planter"/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mark_fulfilled_icon"
android:src="@drawable/ic_check_mark"
app:layout_constraintLeft_toLeftOf="@+id/left_guideline_mark_fulfilled_icon"
app:layout_constraintRight_toRightOf="@+id/right_guideline_mark_fulfilled_icon"
app:layout_constraintTop_toTopOf="@+id/top_guideline_icon"
app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline_icon"/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/message_icon"
android:src="@drawable/ic_check_mark"
app:layout_constraintLeft_toLeftOf="@+id/left_guideline_message_icon"
app:layout_constraintRight_toRightOf="@+id/right_guideline_message_icon"
app:layout_constraintTop_toTopOf="@+id/top_guideline_icon"
app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline_icon"/>

</androidx.constraintlayout.widget.ConstraintLayout>
这是我的 RecyclerView 适配器代码:
    package com.example.treeapp;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mikhaellopez.circularimageview.CircularImageView;

import java.util.List;

public class DonorOrdersAdapter extends RecyclerView.Adapter<DonorOrdersAdapter.DonorOrdersViewHolder> {
public String email;
public static DownloadImageTask downloadImageTask;

public static class DonorOrdersViewHolder extends RecyclerView.ViewHolder {
public CircularImageView imageView;
public TextView orderTextView;
public TextView planterTextView;
public ImageView markFulfilledIcon;
public ImageView messageIcon;

DonorOrdersViewHolder(View view) {
super(view);

imageView = view.findViewById(R.id.image_view);
orderTextView = view.findViewById(R.id.text_view_order);
planterTextView = view.findViewById(R.id.text_view_planter);
markFulfilledIcon = view.findViewById(R.id.mark_fulfilled_icon);
messageIcon = view.findViewById(R.id.message_icon);
downloadImageTask = new DownloadImageTask(imageView);
}
}

DonorOrdersAdapter(String email) {
this.email = email;
}

List<Integer> orders = MainActivity.database.ordersDao().getOrdersByDonor(email);

@NonNull
@Override
public DonorOrdersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.donor_order, parent, false);

return new DonorOrdersViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull DonorOrdersViewHolder holder, int position) {
final int current = orders.get(position);
final String planterEmail = MainActivity.database.ordersDao().getPlanter(current);

if (MainActivity.database.plantersDao().getImageUrl(planterEmail) != null) {
downloadImageTask.execute(MainActivity.database.plantersDao().getImageUrl(planterEmail));
}

holder.orderTextView.setText(MainActivity.database.ordersDao( ).getTrees(current) + " trees");
holder.planterTextView.setText(MainActivity.database.plantersDao().getName(planterEmail) + ", $" +
MainActivity.database.ordersDao().getPayment(current));

holder.markFulfilledIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent fulfillIntent = new Intent(view.getContext(), ConfirmPaymentActivity.class);
fulfillIntent.putExtra("email", email);
fulfillIntent.putExtra("orderId", current);

view.getContext().startActivity(fulfillIntent);
}
});

holder.messageIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent messageIntent = new Intent(view.getContext(), MessageActivity.class);
messageIntent.putExtra("email", email);
messageIntent.putExtra("peerEmail", planterEmail);
messageIntent.putExtra("archived", 0);

view.getContext().startActivity(messageIntent);
}
});
}

@Override
public int getItemCount() {
return orders.size();
}
}

我正在使用使用房间库的 SQLite 表。这是表格行的代码:
    package com.example.treeapp;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;

@Entity(tableName = "Orders")
public class Order {
@PrimaryKey(autoGenerate = true)
public int id;

@ColumnInfo(name = "Donor")
public String donorEmail;

@ColumnInfo(name = "Planter")
public String planterEmail;

@ColumnInfo(name = "Trees")
public int trees;

@ColumnInfo(name = "Payment")
public int payment;

@ColumnInfo(name = "Fulfilled")
public int fulfilled;
}

这是数据访问对象的代码:
    package com.example.treeapp;

import androidx.room.Dao;
import androidx.room.Query;

import java.util.List;

@Dao
public interface OrdersDao {
@Query("INSERT INTO Orders (Donor, Planter, Trees, Payment, Fulfilled) VALUES (:donorEmail, :planterEmail, :trees, :payment, 0)")
void create(String donorEmail, String planterEmail, int trees, int payment);

@Query("SELECT Donor FROM Orders WHERE id = :id")
String getDonor(int id);

@Query("SELECT Planter FROM Orders WHERE id = :id")
String getPlanter(int id);

@Query("SELECT Trees FROM Orders WHERE id = :id")
int getTrees(int id);

@Query("SELECT Payment FROM Orders WHERE id = :id")
int getPayment(int id);

@Query("SELECT Fulfilled FROM Orders WHERE id = :id")
int getFulfilled(int id);

@Query("UPDATE Orders SET Fulfilled = 1 WHERE id = :id")
void markFulfilled(int id);

@Query("SELECT id FROM Orders WHERE Donor = :donorEmail AND Fulfilled = 0")
List<Integer> getOrdersByDonor(String donorEmail);

@Query("SELECT id FROM Orders WHERE Planter = :planterEmail AND Fulfilled = 0")
List<Integer> getOrdersToPlanter(String planterEmail);

@Query("SELECT id FROM Orders WHERE Fulfilled = 0")
List<Integer> getUnfulfilledOrders();
}

这是我使用回收器 View 的 Activity 的代码:
    package com.example.treeapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

public class DonorOrdersActivity extends AppCompatActivity {
private String email;
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_donor_orders);

email = getIntent().getStringExtra("email");
recyclerView = findViewById(R.id.recycler_view);
adapter = new DonorOrdersAdapter(email);
layoutManager = new LinearLayoutManager(this);

recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(layoutManager);
}
}
如果有人能够弄清楚我的 RecyclerViews 出了什么问题,请告诉我。
**编辑:** 我已经通过记录订单表中的数据测试了我的数据库实际上包含订单。

最佳答案

如果你看到你的 logcat,你可以看到 E/RecyclerView: No adapter attached; skipping layout .所以解决方案是改变这个:

recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(layoutManager);
对此:
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);

关于android - 当编译器没有给出任何错误时,为什么 RecyclerView 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63549805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com