我创建了一个列表视图,每个列表视图项目中有3个项目,使用两个单独的.'xml。
1)list_view.xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ListView">
</ListView>
2)list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Text here"
android:id="@+id/textView"
android:layout_alignTop="@+id/TextView"
android:layout_alignLeft="@+id/TextView"
android:layout_centerVertical="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="-"
android:id="@+id/seperator"
android:layout_alignBottom="@+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="000"
android:id="@+id/price"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="$"
android:id="@+id/prefix"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/price"/>
</RelativeLayout>
现在,我使用'SimpleAdapter'创建了包含所有项目的列表视图。现在我想访问列表视图中的项目(textView,separator,...)并以编程方式更改颜色,大小等。
以下是我的MainActivity.java的代码
public class MainActivity extends Activity {
public RelativeLayout relativeLayout;
public RelativeLayout.LayoutParams lp;
public SimpleAdapter mList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
ListView list = (ListView) findViewById(R.id.ListView);
//background color
list.setBackgroundColor(Color.YELLOW);
TextView itemText = (TextView)findViewById(R.id.textView);
itemText.setTypeface(Typeface.SANS_SERIF);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
//Dummy data
//add all elements to map
map.put("text", "click to edit0");
map.put("seperator", "-");
map.put("price", "100");
// add map elements to list
mylist.add(map);
//add all elements to map
map = new HashMap<String, String>();
map.put("text", "click to edit1");
map.put("seperator", "-");
map.put("price", "102");
//add all elements to map
mylist.add(map);
map = new HashMap<String, String>();
map.put("text", "click to edit2");
map.put("seperator", "-");
map.put("price", "104");
mylist.add(map);
//use adapter to set list items
mList = new SimpleAdapter(this, mylist, R.layout.list_item,
new String[] {"text", "seperator", "price"}, new int[] {R.id.textView, R.id.seperator, R.id.price});
list.setAdapter(mList);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
所有工作正常,直到我尝试访问列表视图项并更改它们。 我的代码在
处崩溃并显示空指针异常TextView itemText = (TextView)findViewById(R.id.textView);
itemText.setTypeface(Typeface.SANS_SERIF);
我怀疑这可能是因为我没有setContentView到list_item.xml。 有没有办法访问元素?如果是的话怎么样?
答案 0 :(得分:0)
遵循以下代码
String text = "<font color=#8469af>By tapping Checkout, you agree to our </font> <font color=#ffffff>terms.</font>";
tvTerms.setText(Html.fromHtml(text));
答案 1 :(得分:0)
当你尝试调用findviewbyid时,你还没有创建列表项,所以它返回空指针
如果要使用标准字体设置类型面
android:typeface="serif"
但如果你坚持以编程方式进行 首先从列表中获取列表项
(TextView) list_item.findViewById(R.id.textView);
答案 2 :(得分:0)
正如Talha所指出的那样,异常来自findViewById
this
上{id}}的调用,其中你的id textView不存在,所以它返回null
如果要以编程方式修改列表项,则应实现自己的适配器并在getView方法中应用修改