Setting text for a TextView in ListActivity

巧了我就是萌 提交于 2019-12-24 21:33:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!