0

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)

enter image description here

how can i make new textView for every message like this image

enter image description here

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

sushildlh
  • 8,986
  • 4
  • 33
  • 77

2 Answers2

2

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);
}
Malith Lakshan
  • 762
  • 6
  • 12
2

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

sushildlh
  • 8,986
  • 4
  • 33
  • 77
  • how to add style to new TextView ? – Максим Зубков Jun 17 '16 at 12:10
  • 1
    have look on edit answer or go through this links http://stackoverflow.com/questions/7919173/android-set-textview-textstyle-programmatically and http://stackoverflow.com/questions/4630440/how-to-change-a-textviews-style-at-runtime – sushildlh Jun 17 '16 at 12:13
  • LinearLayout genrxe = (LinearLayout) convertView.findViewById(R.id.rrr); for(int i =0 ; i < 17;i++){ TextView text = new TextView(context); LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); text.setLayoutParams(p); text.setText("your message here"+ i); genrxe.addView(text); } no thing shown in the linearLayout !! – Максим Зубков Jun 17 '16 at 12:17
  • what is size of linear layout in xml ? – sushildlh Jun 17 '16 at 12:20
  • width 189dp and height 509dp and background red its just a test – Максим Зубков Jun 17 '16 at 12:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114939/discussion-between-sushildlh-and--). – sushildlh Jun 17 '16 at 12:24