动态添加textview到列表视图

问题描述:

我想动态地添加textview到listview。加入IM设置文本,但在ListView文本看起来像之前“[email protected] ......”动态添加textview到列表视图

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布局文件:

<?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" 

/> 

我试图改变的TextView在asd布局文件中具有linearlayout的元素,但它没有工作。

我无法弄清楚。我需要做什么?

谢谢..

+0

你确定你不想添加简单的项目布局,其中包含一个文本视图... – 2013-03-11 15:49:56

这是因为正常执行ArrayAdapter电话toString()的对象是持有,以便它可以确保它们可以显示。

由于ArrayAdapter已经使用TextView来显示数据,所以我建议您更改适配器,使其为ArrayAdapter<String>,然后添加要显示的字符串。

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

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

如果要更改布局,扩展ArrayAdapter并重写getView()方法。这tutorial进一步深入。

通过StringList A S实施一些Spannable小号支持的ArrayAdapter的一个小例子:

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

结果:

enter image description here

为了它的实例(假设你”从一个活动重做),我做了什么:

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(); 
+0

其实我试图设置添加的图标和文字一起使用图标和文本,我用这个spannable字符串,但在结束出了问题(我认为是因为toString方法),它没有工作http://stackoverflow.com/questions/15273771/android-spannablestring-arrayadapter-and-listview所以我改变了方式,并试图这个,但没有改变。 – tlgtkn 2013-03-12 08:39:41

+0

@tlgtkn然后,您可能在自定义Adapter中的getView()方法有问题。 – 2013-03-12 15:56:45

+0

@tlgtkn你可能想要发布你的'getView()'方法来帮助你。请记住,您必须分配要显示的spannables和其他UI元素。 – 2013-03-13 15:19:10