【问题标题】:Add TextView at runtime in Adapter运行时在 Adapter 中添加 TextView
【发布时间】: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 初学者的任何例子

标签: java android


【解决方案1】:

使用这个例子

而不是这个.......

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

使用这个

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

注意:-在xml中将id流派的TextView改为垂直方向的LinearLayout。

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);
}

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

【讨论】:

【解决方案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);
}

【讨论】:

  • 我现在就试试 :)
  • 你必须像这样设置 LayoutParams:LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); text.setLayoutParams(p);
  • 更新了答案
猜你喜欢
  • 2020-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-09
  • 2010-10-22
  • 1970-01-01
  • 2011-08-07
  • 1970-01-01
相关资源
最近更新 更多