问题
I am populating a list by extending ListActivity . I have added a header to the list to show the total count of items in the list.
I have maintained the header view in a separate layout file header.xml as shown below.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/headerTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Total number of devices :"
android:textSize="15dip"
android:layout_weight="1" />
</LinearLayout>
This is how I am adding header to the list in the ListViewActivity class,
ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header, lv, false);
lv.addHeaderView(header, null, false);
How do I set the text for the header TextView? I tried the following and it returned null for the TextView.
TextView textBox = (TextView)findViewById(R.id.);
textBox.setText("Count");
回答1:
Try this
View header = (View)inflater.inflate(R.layout.header, lv, false);
lv.addHeaderView(header, null, false);
Get your TextView in header
TextView textBox = (TextView)header.findViewById(R.id.headerTextView);
来源:https://stackoverflow.com/questions/16497465/setting-text-for-a-textview-in-listactivity