tlgtkn
tlgtkn

Reputation: 13

Dynamically adding textview to listview

I'm trying to add textview to listview dynamically. Before adding im setting the text but in the listview text seems something like 'android.widget.TextView@45f...'

private ArrayAdapter<TextView> dizi;
private ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

list = (ListView)findViewById(R.id.listview);
dizi = new ArrayAdapter<TextView>(this, R.layout.asd);
list.setAdapter(dizi);

TextView qwe = new TextView(getApplicationContext());
qwe.setText("txt");
dizi.add(qwe);

} 

asd layout file:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:padding="5dp"

/>

I tried to change the textview element with linearlayout in asd layout file but it didnt work.

i cant figure it out. what do i have to do?

Thanks..

Upvotes: 1

Views: 8349

Answers (1)

A--C
A--C

Reputation: 36449

This is because the normal implementation of ArrayAdapter calls toString() on the Objects it is holding so that it can be sure they can be displayed.

Since the ArrayAdapter already uses TextViews to display the data, I suggest you change your Adapter so it is an ArrayAdapter<String>, then just add the String you want to display.

dizi = new ArrayAdapter<String>(this, R.layout.asd);
list.setAdapter(dizi);

dizi.add("txt");
dizi.notifyDataSetChanged();

If you want to change the layout, Extend ArrayAdapter and override the getView() method. This tutorial goes into a bit more depth.

A small example of an ArrayAdapter backed by a List of Strings implementing some Spannables:

public class ExampleAdapter extends ArrayAdapter<String> {

  LayoutInflater inflater;
  int resId;
  int layoutId;

  public ExampleAdapter(Context context,int layoutId, int textViewResourceId,
                        List<String> objects) {
    super(context, layoutId, textViewResourceId, objects);
    this.inflater = LayoutInflater.from(context);
    this.layoutId = layoutId;
    this.resId = textViewResourceId; 
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent)
  {
    if (convertView == null)
      convertView = inflater.inflate(layoutId, parent,false);

    String text = getItem(position);
    Spannable s = Spannable.Factory.getInstance().newSpannable(text);
    s.setSpan(new ForegroundColorSpan(Color.RED), 0, text.length()/2, 0);
    s.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, text.length()/2, 0);
    s.setSpan(new ForegroundColorSpan(Color.DKGRAY), text.length()/2, text.length(), 0);
    s.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), text.length()/2, text.length(), 0);

    ((TextView)convertView.findViewById(resId)).setText(s, TextView.BufferType.SPANNABLE);

    return convertView;
  }
}

Result:

enter image description here

To make an instance of it (assuming you're doing this from an Activity), what I did:

ArrayList <String> items = new ArrayList <String> ();
items.add ("Array Adapter");

ExampleAdapter dizi = new ExampleAdapter (YourActivity.this,android.R.layout.simple_list_item_1,android.R.id.text1,items);
list.setAdapter(dizi);

dizi.add ("your text");
dizi.notifyDataSetChanged();

Upvotes: 3

Related Questions