[英]Add TextView at runtime in Adapter
附加数组中的每个消息
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;
}
}
现在结果将是这样的(仅是示例)
我怎样才能为每个像这样的消息制作新的textView
这是我的代码
String genreStr = "";
for (String str : m.getGenre()) {
//append every str as new textView
}
但是我不知道如何使新的TextView和追加到CustomAdapter中
您可以通过以下方式以编程方式创建TextView
,
TextView textView=new TextView(context);
要将其添加为列表视图项,建议您在list_row
XML中创建一个持有人
<LinearLayout
android:id="@+id/holder"
android:orientation="vertical"
android:width="match_parent"
android:height="wrap_content"/>
然后将文本视图附加到它。
LinearLayout ll=(LinearLayout)convertView.findViewById(R.id.holder);
for(String genre:m.getGenre()){
TextView tv=new TextView(context);
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(params);
tv.setText(genre);
//set a dummy color to check whether views are added
tv.setBackgroundColor(Color.RED);
tv.setTextColor(Color.BLACK);
//set more styles to the text view if you want
ll.addView(tv);
}
使用这个例子
代替这个........
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);
}
享受编码...........
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.