简体   繁体   English

在适配器中在运行时添加TextView

[英]Add TextView at runtime in Adapter

append every message in array 附加数组中的每个消息

m.getGenre() m.getGenre()

as new message with TextView for every message in array i want to make TextView for it and append 作为带有TextView的新消息,用于数组中的每个消息,我想为其创建TextView并追加

this is CustomListAdapter inside it i want loop in array and append every item in array as new 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;
}

}

now the result will be something like this (its just example) 现在结果将是这样的(仅是示例)

在此处输入图片说明

how can i make new textView for every message like this image 我怎样才能为每个像这样的消息制作新的textView

在此处输入图片说明

this is my code 这是我的代码

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

but i dont know how to make new TextView adn append it inside CustomAdapter 但是我不知道如何使新的TextView和追加到CustomAdapter中

You can create a TextView programatically by, 您可以通过以下方式以编程方式创建TextView

TextView textView=new TextView(context);

To add it as a list view item I would suggest you to create a holder in your list_row XML 要将其添加为列表视图项,建议您在list_row XML中创建一个持有人

<LinearLayout
     android:id="@+id/holder"
     android:orientation="vertical"
     android:width="match_parent"
     android:height="wrap_content"/>

Then attach the text views to it. 然后将文本视图附加到它。

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

use this example 使用这个例子

instead of this........ 代替这个........

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

use this 用这个

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

Note:-in xml change TextView of id genre into Linear Layout with orientation vertical. 注意:-在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);
}

enjoy coding........... 享受编码...........

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM