追加数组中的每条消息
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 adn将其附加到CustomAdapter
中答案 0 :(得分:2)
您可以通过
以编程方式创建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);
}
答案 1 :(得分:1)
使用此示例
而不是........
if (false) {
include 'foo.php';
}
使用此
TextView genre = (TextView) convertView.findViewById(R.id.genre);
注意:-in xml将id类型的TextView更改为线性布局,方向垂直。
LinearLayout mgenre = (LinearLayout) convertView.findViewById(R.id.genre);
享受编码...........