【问题标题】:How to add a footer using AddFooterView with text from a String?如何使用 AddFooterView 和字符串中的文本添加页脚?
【发布时间】:2012-12-18 06:40:02
【问题描述】:

我已经使用ListView 提出了申请。我正在尝试使用addFooterView 为我的ListView(与ListView 一起滚动)添加页脚。在这个页脚中,我想添加一个TextView,其中包含一些在我的Activity 中定义为String 的文本,称为notification

我已经尝试了几件事,但它不起作用。

我的ListActivityRoosterWijzigingen.java

public class RoosterWijzigingen extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState); 

        ArrayList<HashMap<String, String>> roosterwijziginglist = new ArrayList<HashMap<String, String>>();         

        String hour = "4";
        String info = "Af 123";
        String notification = "Het is 40min-rooster!";

        // Creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();

        // Adding each child node to HashMap key => value
        map.put("hour", hour);
        map.put("info", info);

        // Adding HashList to ArrayList
        roosterwijziginglist.add(map);}

        ListAdapter adapter = new SimpleAdapter(this, roosterwijziginglist , R.layout.roosterwijzigingen, 
                            new String[] {"hour", "info"}, 
                            new int[] {R.id.hour, R.id.info});
        setListAdapter(adapter);

        setContentView(R.layout.listplaceholder);           
   }
}

我从 JSON 文件中获取字符串 hourinfonotification 的信息,但我省略了这一点以缩短代码。

listplaceholder.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >            

        <ListView
                         android:id="@+id/android:list"
                         android:layout_width="fill_parent"
                         android:layout_height="wrap_content"
                         android:drawSelectorOnTop="true" />

        <TextView
                         android:id="@id/android:empty"
                         android:layout_width="fill_parent"
                         android:layout_height="wrap_content"
                         android:text="No data" />

roosterwijzigingen.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >                      

        <TextView  
                         android:id="@+id/hour"
                         android:layout_width="wrap_content" 
                         android:layout_height="fill_parent" />

        <TextView  
                         android:id="@+id/info"
                         android:layout_width="match_parent" 
                         android:layout_height="fill_parent" />
</LinearLayout>

【问题讨论】:

    标签: android listview header footer


    【解决方案1】:

    您必须先将您的String 放入TextView 中,然后再将其添加到您的列表中,它看起来像这样:

    String footerText = "Footer text";
    TextView textView = new TextView(this);
    textView.setText(footerText);
    
    getListView().addFooterView(textView);
    

    请记住,99% 的情况下,当您想在屏幕上显示某些内容时,它必须是 View

    【讨论】:

    • 我试过了,但我的问题在于最后一行。我不知道如何调用我的 ListView。这就是它出错的地方。对不起,我会在我的问题中编辑它。
    • @user1946633 使用:getListView().addFooterView(textView); 之前调用setListAdapter()
    • 在调用 setListAdapter() 之前,我尝试将以下代码放入我的 Activity 中: String footerText = "Footer text"; TextView textView = new TextView(this); textView.setText(footerText); getListView().addFooterView(textView);我的应用程序不再崩溃,但仍然没有页脚,它看起来和以前一样。我做错了什么?
    • 试着把它放在 setContentView 之后。也许文本颜色与您的背景相同,而您看不到它。尝试像这样设置不同的文本颜色:textView.setTextColor(Color.RED);
    猜你喜欢
    • 2011-03-14
    • 1970-01-01
    • 2015-01-11
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 2015-11-06
    • 2020-10-16
    • 2019-06-24
    相关资源
    最近更新 更多