现在我正在开发Android Studio的Android应用程序。 我在动态分配TextView时遇到了一些问题。 我正在使用RecyclerView来实现显示项目列表。 RecyclerView的每个ViewHolder项都应该具有TextView的ArrayList。 每个ViewHolder项中ArrayList的大小可以不同,因为 它从node.js服务器接收数据。所以我想在ViewHolder中分配TextView 通过动态分配。问题是当我在onBindViewHolder中创建TextView并将addView添加到LinearLayout时。它不起作用。但除了动态TextViews之外的其他视图分配工作。 如果你能帮助我,我会很高兴。
因为我从服务器中接收了JSON数组类型的数据, RecyclerView的适配器数据类型是ArrayList的ArrayList
这里是我的代码
public class PlayUpSitterListAdapter extends RecyclerView.Adapter<PlayUpSitterListAdapter.PlayUpListViewHolder> {
private ArrayList<ArrayList<PlayupSitter>> playUpList = new ArrayList<>();
ArrayList<PlayupSitter> playUp = new ArrayList<>();
private Context ctx;
public PlayUpSitterListAdapter(Context ctx, ArrayList<ArrayList<PlayupSitter>> playUpList) {
this.ctx = ctx;
for(int i=0; i < playUpList.size(); i++)
{
if(playUpList.get(i).get(0).play_paid == 0)
{
this.playUpList.add(playUpList.get(i));
}
else
{
this.playUpList.add(playUpList.get(i));
}
}
/*
for(int i =0; i<playUpList.size(); i++) {
DynamicPlayUpTextView dynamicPlayUpTextView = new DynamicPlayUpTextView(ctx, "");
holder.dynamictextViewArrayList.add(dynamicPlayUpTextView);
}
*/
}
public void setPlayUpList(ArrayList<ArrayList<PlayupSitter>> playUpList) {
for(int i=0; i < playUpList.size(); i++)
{
if(playUpList.get(i).get(0).play_paid ==0)
{
this.playUpList.add(playUpList.get(i));
}
else
{
this.playUpList.add(playUpList.get(i));
}
}
this.notifyDataSetChanged();
}
public ArrayList<PlayupSitter> getItem(int position) {
return (playUpList != null && (0 <= position && position < playUpList.size())) ? playUpList.get(position) : null;
}
@Override
public PlayUpListViewHolder onCreateViewHolder(ViewGroup sitter, int viewType) {
View itemView = LayoutInflater.
from(sitter.getContext()).
inflate(R.layout.item_play_up_sitter, sitter, false);
return new PlayUpListViewHolder(itemView);
}
@Override
public void onBindViewHolder(PlayUpListViewHolder holder, int position) {
ArrayList<PlayupSitter> playUp = playUpList.get(position);
String temp;
int vh_paytotal=0;
for(int i=0; i< playUp.size(); i++) {
temp = getmessage(playUp.get(i).play_date, playUp.get(i).sitter_rank, playUp.get(i).play_hour);
DynamicPlayUpTextView dynamicPlayUpTextView = new DynamicPlayUpTextView(ctx, temp);
holder.dynamictextViewArrayList.add(i, dynamicPlayUpTextView);
holder.dynamicLL.addView(holder.dynamictextViewArrayList.get(i).buildDynamicPlayUpTextView());
if(playUp.get(i).sitter_rank == "A")
{
vh_paytotal += 13000 * playUp.get(i).play_hour;
}
else if (playUp.get(i).sitter_rank == "B")
{
vh_paytotal += 10000 * playUp.get(i).play_hour;
}
else{
vh_paytotal += 8000 * playUp.get(i).play_hour;
}
}
if (playUpList.get(position).get(0).play_paid == 1) {
holder.imgGrayBackground.setVisibility(View.VISIBLE);
holder.imgGrayBackground.setClickable(false);
} else {
holder.imgGrayBackground.setVisibility(View.GONE);
holder.imgGrayBackground.setClickable(true);
}
holder.pay_total.setText(vh_paytotal + "원");
}
private String getmessage(String play_date, String sitter_rank, int play_hour) {
Toast.makeText(ctx, play_date + " 12000(" + sitter_rank +"등급) X"+play_hour + " = "+ play_hour * 12000+"원", Toast.LENGTH_SHORT ).show();
return play_date + " 12000(" + sitter_rank +"등급) X"+play_hour + " = "+ play_hour * 12000+"원";
}
@Override
public int getItemCount() {
return (playUpList != null) ? playUpList.size() : 0;
}
public static class PlayUpListViewHolder extends RecyclerView.ViewHolder {
ArrayList<DynamicPlayUpTextView> dynamictextViewArrayList = new ArrayList<>();
ImageView imgGrayBackground;
ImageView imgLine;
LinearLayout dynamicLL;
TextView txtPlayUp_TP;
TextView pay_total;
DynamicPlayUpTextView ddd;
public PlayUpListViewHolder(View itemView) {
super(itemView);
dynamicLL = (LinearLayout) itemView.findViewById(R.id.layout_dynamic_play_up_sitter);
imgGrayBackground = (ImageView) itemView.findViewById(R.id.grayBackground_playUp_Sitter);
imgLine = (ImageView) itemView.findViewById(R.id.img_line_play_up_sitter);
pay_total = (TextView) itemView.findViewById(R.id.txtPlayUp_TotalPay);
txtPlayUp_TP = (TextView) itemView.findViewById(R.id.txtPlayUp_TP);
}
}
}
答案 0 :(得分:0)
首先,您必须在添加ArrayList之前设置LayoutParams。 所以我们在下面添加:
dynamicPlayUpTextView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
之间:
DynamicPlayUpTextView dynamicPlayUpTextView = new DynamicPlayUpTextView(ctx, temp);
holder.dynamictextViewArrayList.add(i, dynamicPlayUpTextView);
答案 1 :(得分:0)
我没有设置我的TextColor。它实际上工作但我很困惑,因为它的默认颜色是白色,与背景颜色相同。这都怪我。关闭此问题