gpt4 book ai didi

java - 在运行时在Adapter中添加TextView

转载 作者:行者123 更新时间:2023-12-01 21:27:25 24 4
gpt4 key购买 nike

追加数组中的每条消息

m.getGenre()

作为数组中每条消息的 TextView 的新消息,我想为其创建 TextView 并附加

这是其中的 CustomListAdapter,我想在数组中循环并将数组中的每个项目附加为新的 TextView

public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<notofication> movieItems;
private final Context context;


public CustomListAdapter(Activity activity, List<notofication> movieItems,Context c) {
this.activity = activity;
this.movieItems = movieItems;
this.context = c;
}

@Override
public int getCount() {
return movieItems.size();
}

@Override
public Object getItem(int location) {
return movieItems.get(location);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);


TextView title = (TextView) convertView.findViewById(R.id.title);
TextView rating = (TextView) convertView.findViewById(R.id.rating);
TextView genre = (TextView) convertView.findViewById(R.id.genre);
TextView year = (TextView) convertView.findViewById(R.id.releaseYear);

// getting movie data for the row
final notofication m = movieItems.get(position);







// title
title.setText(m.getTitle());



// rating
rating.setText(String.valueOf(m.getRating()));


String genreStr = "";
for (String str : m.getGenre()) {
//append every str as new textView
}


// release year
year.setText(m.getYear());

return convertView;
}

}

现在结果将是这样的(这只是示例)

enter image description here

我怎样才能为每条消息创建新的textView,就像这张图片

enter image description here

这是我的代码

    String genreStr = "";
for (String str : m.getGenre()) {
//append every str as new textView
}

但我不知道如何创建新的 TextView 并将其附加到 CustomAdapter 中

最佳答案

使用这个例子

而不是这个......

TextView genre = (TextView) convertView.findViewById(R.id.genre);

用这个

LinearLayout mgenre = (LinearLayout) convertView.findViewById(R.id.genre);

注意:-在xml中将id类型的TextView更改为垂直方向的线性布局。

for(String genre:m.getGenre()){
TextView text = new TextView(context);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
text.setLayoutParams(p);
text.setText(genre);
text.setTextAppearance(R.style.boldText);

mgenre.addView(text);
}

享受编码............

关于java - 在运行时在Adapter中添加TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37880596/

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