我真的不知道出了什么问题,我一直在努力找出问题所在.我通常会避免发表关于它的帖子,但无论我搜索什么,我似乎都无法在谷歌上找到任何相关的问题.我甚至找不到一个人经历过这个问题.

当我第一次创建RecclerView和适配器时,它们工作得很好(手动实现每个对象,如下所示).我实际上没有更改任何内容,但是当我try 将其编程为返回一个ArrayList(从SQLite数据库)并将该ArrayList传递到适配器时,它停止了工作.那么,回到手动创建对象和ArrayList的过程中,它仍然不起作用吗?

任何帮助都将非常、非常感激:)

My RecyclerView adapter:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder>{

    Context context;
    ArrayList<myUser> listings;

    public RecyclerViewAdapter(Context context, ArrayList<myUser> arrayList) {
        this.context = context;
        this.listings = arrayList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.recyclerview_row, parent, false);
        return new MyViewHolder(view);
    }


    /**
     * This basically controls the customisation of each item in the RecyclerView
     * @param holder The ViewHolder which should be updated to represent the contents of the
     *        item at the given position in the data set.
     * @param position The position of the item within the adapter's data set.
     */
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        System.out.println(position);
        myUser item = listings.get(position);

        holder.teamName.setText(item.getUsername());
        System.out.println(item.getUsername());
        //holder.teamLevel.setText(item.getTeamLevel());
        //holder.requiredRole.setText(item.getRoleRequired());
    }

    /**
     * This gets the length of your dataset to determine how many items will be in the RecyclerView
     * So the length of the ArrayList in this case.
     * @return
     */
    @Override
    public int getItemCount() {
        return listings.size();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView teamName, teamLevel, requiredRole;
        Button btnApply;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            teamName = itemView.findViewById(R.id.tvTeamName);
            teamLevel = itemView.findViewById(R.id.tvTeamLevel);
            requiredRole = itemView.findViewById(R.id.tvRoleRequired);
        }
    }
}

myUser class:

public class myUser {

    private static int id;
    private static String username;
    private static String nationality;
    private static String role;
    private static boolean teamStatus;
    private static int teamID;
    private static String teamName;

    public myUser(int id, String username, @Nullable String nationality, @Nullable String role, @Nullable int teamID, @Nullable String teamName) {
        this.id = id;
        this.username = username;
        this.nationality = nationality;
        this.role = role;
        this.teamID = teamID;
        this.teamName = teamName;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public static String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }

    public static String getNationality() {
        return nationality;
    }
    public void setNationality(String nationality) {
        this.nationality = nationality;
    }

    public static String getRole() {
        return role;
    }
    public void setRole(String role) {
        this.role = role;
    }

    public boolean isTeamStatus() {
        return teamStatus;
    }
    public void setTeamStatus(boolean teamStatus) {
        this.teamStatus = teamStatus;
    }

    public static int getTeamID() { return teamID; }
    public static void setTeamID(int teamID) { myUser.teamID = teamID; }

    public static String getTeamName() { return teamName; }
    public static void setTeamName(String teamName) { myUser.teamName = teamName; }
}

The activity code:

myUser user1 = new myUser(12, "Sid", null, null, -1, null);
myUser user2 = new myUser(13, "George", null, null, -1, null);
myUser user3 = new myUser(14, "Hello", null, null, -1, null);

ArrayList<myUser> newArray = new ArrayList<>();
newArray.add(user1);
newArray.add(user2);
newArray.add(user3);

/* Recycler View */
RecyclerView rv = findViewById(R.id.recyclerView_teamFind);
rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, newArray);
rv.setAdapter(adapter);

The console output(来自onBindViewHolder内的System.out.println语句):

I/System.out: 0
I/System.out: Hello
I/System.out: 1
I/System.out: Hello
I/System.out: 2
I/System.out: Hello

And the final result:

The final result of the code

我完全不知道出了什么问题,它起作用了.现在不是了.

推荐答案

根据您的myUser个类的代码,很明显这个问题是静态的,因为您已经声明了每个变量都是静态的.我不知道您为什么要这样做,因为您有getter和setter方法,这是建议的方法.所以只需从变量中删除静态关键字,因为它在内存中创建单一副本并更新每个对象变量值.

Java相关问答推荐

android Document File. isDirector()返回意外结果

在Java中使用带限制的回归打印星形三角形

如何为具有多对多关系的实体的给定SQL查询构建JPA规范?

当一个链表中间有一个循环时,它的松散部分会发生什么?

在现代操作系统/硬件上按块访问数据值得吗?

为什么BasicComboBoxRenderer在文本不存在或文本为空的情况下设置两次文本?

Kubernetes的Java客户端检索状态.处于终止状态的Pod的阶段';正在运行';

如何从JNI方法正确调用NSOpenPanel以在正确的线程上运行?

Log4j与jdk21兼容吗?

如何仅使用键/ID的一部分(组合)高效地返回映射值?

使用Class.this.field=Value初始化构造函数中的最后一个字段会产生错误,而使用this.field=Value则不会

将关闭拍卖的TimerService

Dijkstra搜索算法的实现

A.ForEach与For(类型a:集合)

try 使用预准备语句占位符获取信息时出现Try-With-Resources错误

如何在ImageIO或十二只猴子中旋转TIFF CMYK图像?

在Eclipse中可以使用外部字体吗?

RestTemplate Bean提供OkHttp3ClientHttpRequestFactory不支持Spring Boot 3中的请求正文缓冲

Bash数组的单引号元素并使用空格连接

ResponseEntity.控制器截断响应的JSON部分