Мне наконец удалось создать это, так что я решил поделиться этим здесь. Я использовал RecyclerView для этого, но если кто-нибудь знает, как это сделать с ListView, я с радостью воспользуюсь этим. Возможно, возможно сделать то же самое с помощью держателей представления в ListView? Я опубликую свои результаты, если это сработает.
// Adapter for creating the RecyclerView
public class ResultsAdapter extends RecyclerView.Adapter<ResultsAdapter.MyViewHolder> {
private Context context;
private List<ListItem> itemsList;
private ArrayList<Profile> profiles;
public class MyViewHolder extends RecyclerView.ViewHolder {
public View view;
public TextView name, extra, votes;
public TextView[] votePoints;
public MyViewHolder(View view) {
super(view);
this.view = view;
name = view.findViewById(R.id.resultsName);
extra = view.findViewById(R.id.resultsBonus);
votes = view.findViewById(R.id.resultsVotePoints);
votePoints = new TextView[profiles.size()];
}
}
public ResultsAdapter(Context context, List<ListItem> itemsList, ArrayList<Profile> profiles) {
this.itemsList = itemsList;
this.profiles = profiles;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.result_show_votes_item, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
ListItem item = itemsList.get(position);
holder.name.setText(item.getName());
holder.extra.setText(String.valueOf(item.getTotal()));
holder.votes.setText(String.valueOf(item.getVotePoints()));
// Add vote points to layout
LinearLayout layout = holder.view.findViewById(R.id.resultItemLayout);
LayoutInflater inflater = LayoutInflater.from(context);
for (int i = 0; i < profiles.size(); i++) {
ListItem voteItem = itemsList.get(position);
Profile profile = profiles.get(i);
int voteAmount = profile.votePointsOfItem(voteItem);
// Use "layout" and "false" as parameters to get the width and height from result_voter_points
TextView vote = (TextView) inflater.inflate(R.layout.result_voter_points, layout, false);
vote.setText(voteAmount == 0 ? "" : String.valueOf(voteAmount));
layout.addView(vote);
holder.votePoints[i] = vote;
}
}
@Override
public int getItemCount() {
return itemsList.size();
}
}
// Add profile names to topics
LinearLayout topicLayout = findViewById(R.id.topicLayout);
for (Profile p : selectedProfiles) {
TextView name = (TextView) getLayoutInflater().inflate(R.layout.result_voter_name, topicLayout, false);
name.setText(p.getName());
topicLayout.addView(name);
}
RecyclerView recyclerView = findViewById(R.id.recycler_view);
ResultsAdapter adapter = new ResultsAdapter(this, selectedList.getItems(), selectedProfiles);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);