我正在创建一个投票应用程序,该应用程序的选民数量未知。当我知道选民的数量时,我想在ListView中添加列。这是一个示例图像:
有没有一种方法可以将列动态添加到ListView?我在过去2个小时内用谷歌搜索,似乎找不到任何好的解决方案。
我已经可以进行垂直和水平滚动,并且可以在顶部添加投票者名称,但是由于它们位于ListView中,因此无法动态添加投票点。
这是我尝试过的:
膨胀ListView的布局以添加项目(由于某种原因,如果我添加了例如“ 0”,则会在第一行中添加“ 0000”,而在其余行中添加“ 00”)
使用RecyclerView(它比ListView更好,它正确地添加了“ 0”,但是由于某种原因,宽度不是应该的宽度
最佳答案
我最终设法创建了这个,因此尽管我会在这里分享它。我用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);