【发布时间】:2016-10-19 05:37:27
【问题描述】:
将每条消息附加到数组中
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 中
【问题讨论】:
-
将
genre设为LinearLayout并在runtime处创建textView并将文本视图添加到genre....... -
@sushildlh 我是 android 初学者的任何例子