简体   繁体   中英

Add TextView at runtime in Adapter

append every message in array

m.getGenre()

as new message with TextView for every message in array i want to make TextView for it and append

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

在此处输入图片说明

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

You can create a TextView programatically by,

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

<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.

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...........

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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