RecyclerView 只显示 ArrayList 中的最后一项,显示次数与列表中的项相同

问题描述 投票:0回答:1

我真的不明白出了什么问题,我已经尝试弄清楚了很长一段时间。我通常会避免发布有关它的帖子,但无论我搜索什么,我似乎都无法在 Google 上找到任何相关问题。我什至找不到一个遇到过这个问题的人。

当我第一次制作 RecyclerView 和适配器时,它们工作正常(手动实现每个对象,如下所示)。我并没有真正改变任何东西,但是当我尝试更改以对其进行编程以返回一个 ArrayList(来自 SQLite 数据库)并将该 ArrayList 传递到适配器时,它停止工作了。那么回到手动创建对象和ArrayList,还是不行?

任何帮助将不胜感激:)

我的 RecyclerView 适配器:

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);
        }
    }
}

my用户类:

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; }
}

活动代码:

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);

控制台输出(来自 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

最后的结果:

我完全不知道出了什么问题,它正在工作。现在不是了。

java android arraylist android-recyclerview
1个回答
1
投票

根据您的

myUser
类代码,可以清楚地看到问题出在 static 上,因为您已将每个变量声明为 static 。我不知道你为什么这样做,因为你有 getter 和 setter 方法,这是建议的方法。所以只需从变量中删除 static 关键字,因为它在内存中制作单个副本并更新每个对象变量值。

© www.soinside.com 2019 - 2024. All rights reserved.