RecyclerView only displaying last item from ArrayList, showing same amount of times as there are items in the list

huangapple go评论78阅读模式
英文:

RecyclerView only displaying last item from ArrayList, showing same amount of times as there are items in the list

问题

I don't really understand what is going wrong, I have been trying to figure it out for quite some time. I would usually avoid making a post about it but I can't seem to find any relating problem on Google, no matter what I search. I cannot find even one person that has experienced this problem.

When I first made the RecyclerView and adapter, they worked fine (implementing each object manually like shown below). I didn't really change anything but when I tried to change to program it to return an ArrayList (from SQLite database) and pass that ArrayList into the adapter, it stopped working. So going back to manually creating objects and the ArrayList, it still does not work?

Any help would be much, much appreciated RecyclerView only displaying last item from ArrayList, showing same amount of times as there are items in the list

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 (from System.out.println statements within onBindViewHolder):

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

And the final result: (Image not available in text format)

I just have absolutely no clue what is wrong, it was working. Now it isn't.

英文:

I don't really understand what is going wrong, I have been trying to figure it out for quite some time. I would usually avoid making a post about it but I can't seem to find any relating problem on Google, no matter what I search. I cannot find even one person that has experienced this problem.

When I first made the RecyclerView and adapter, they worked fine (implementing each object manually like shown below). I didn't really change anything but when I tried to change to program it to return an ArrayList (from SQLite database) and pass that ArrayList into the adapter, it stopped working. So going back to manually creating objects and the ArrayList, it still does not work?

Any help would be much, much appreciated RecyclerView only displaying last item from ArrayList, showing same amount of times as there are items in the list

My RecyclerView adapter:

public class RecyclerViewAdapter extends RecyclerView.Adapter&lt;RecyclerViewAdapter.MyViewHolder&gt;{
Context context;
ArrayList&lt;myUser&gt; listings;
public RecyclerViewAdapter(Context context, ArrayList&lt;myUser&gt; 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&#39;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, &quot;Sid&quot;, null, null, -1, null);
myUser user2 = new myUser(13, &quot;George&quot;, null, null, -1, null);
myUser user3 = new myUser(14, &quot;Hello&quot;, null, null, -1, null);
ArrayList&lt;myUser&gt; newArray = new ArrayList&lt;&gt;();
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 (from System.out.println statements within onBindViewHolder):

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:

RecyclerView only displaying last item from ArrayList, showing same amount of times as there are items in the list

I just have absolutely no clue what is wrong, it was working. Now it isn't.

答案1

得分: 1

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

英文:

As per your code of myUser class , It is clearly visible that issue is with static as you have declared each variable static . I don't know why you did it as you have getter and setter methods and that is the suggested way to do it . So just remove static keyword from variables as it is making single copy in memory and updating each object variable value .

huangapple
  • 本文由 发表于 2023年5月6日 20:28:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188890.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定