簡體   English   中英

在適配器中在運行時添加TextView

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM