2013-01-24 116 views
0

我试图以编程方式更改ListView中文本的对齐方式,而不是更改xml文件。以编程方式访问rowlistview

这里是我的row_listview.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/textViewRowList" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:layout_width="fill_parent" 
    android:textSize="20sp" 
    android:paddingLeft="6dip" 
    android:paddingRight="6dip" 
    android:gravity="left" 
    android:textColor="#330033" 
    android:textStyle="bold"/> 

而且activity_main.xml有以下ListView有定义

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:divider="@null" 
    android:dividerHeight="0dp" 
    tools:listitem="@android:layout/simple_list_item_1" 
    android:background= "#FF6600"> 
</ListView> 

所以在我mainactivity.java我想在运行时更改列表视图文本的对齐用户点击时关于我的活动的按钮

private ListView lv; 
private TextView tvRowListView; 
lv.setAdapter(new ArrayAdapter<String>(this,R.layout.row_listview, sampleStringArray)); 
tvRowListView = (TextView) findViewById(R.id.textViewRowList); 
tvRowListView.setGravity(Gravity.CENTER); 

然而,上面的最后一行是抛出nullpointerexception。我不知道为什么。有人可以在这里提出一些想法/解决方案

回答

0

您textViewRowList存在该列表中的每一个项目,如果你想改变它,你将需要做的是在AdaptergetView这意味着你将需要自定义适配器

+0

由于性能。我可以在讨论客户适配器问题的论坛中获得更多的问题。我想我现在知道我的问题的出路。 – user1938357