gpt4 book ai didi

java - ListView 以编程方式添加列

转载 作者:行者123 更新时间:2023-12-01 16:56:00 25 4
gpt4 key购买 nike

我正在创建一个投票应用程序,其选民数量未知。当我知道选民数量时,我想向 ListView 添加列。这是一个示例图像: enter image description here

有没有办法动态添加列到ListView?我在过去的 2 个小时里用谷歌搜索了这个问题,似乎找不到任何好的解决方案。

我已经可以进行垂直和水平滚动,并且可以在顶部添加选民姓名,但我无法动态添加投票点,因为它们位于 ListView 内。

这是我尝试过的:

  1. 膨胀ListView的布局以添加项目(由于某种原因,如果我添加例如“0”,它会向第一行添加“0000”,向其余行添加“00”)
  2. 使用RecyclerView(它比ListView效果更好,它正确地添加了“0”,但由于某种原因宽度不是它应该的宽度

最佳答案

我终于成功创建了这个,所以我想我会在这里分享它。我使用 RecyclerView 来完成此操作,但如果有人知道如何使用 ListView 执行此操作,我会很乐意使用它。通过在 ListView 中使用 View 持有者可能可以做同样的事情吗?如果有效的话我会发布我的结果。

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

关于java - ListView 以编程方式添加列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61590432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com