Livi
Livi

Reputation: 17

Header and Footer at ListView Adapter

try {           
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
      sb.append(line);
    }
    is.close();
    resultString = sb.toString();
    String[] ArrayStr = Func.split(resultString, "|*|");
    String[] arr;

    lv = new ListView(context);
    lv.setAdapter(null);        
    // populate
    ArrayList<Device> m_Devices = new ArrayList<Device>();
    Device device;
    for(int i = 0; i < ArrayStr.length; i++){
        arr = Func.split(ArrayStr[i], "|**|");
        device = new Device(arr[1], Integer.parseInt(arr[0]));
        m_Devices.add(device);
    }
    // +header n footer (must above custom adapter)

    View v1 = getLayoutInflater().inflate(R.layout.header, null);
    lv.addHeaderView(v1);


    View v = getLayoutInflater().inflate(R.layout.footer, null);
    lv.addFooterView(v);



    // custom adapter
    CustomAdapter lvAdapter =  new CustomAdapter(context, m_Devices);
    lv.setAdapter(lvAdapter);
    lv.setOnItemClickListener(new OnItemClickListener() {
    @Override

    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
    AlertDialog.Builder adb=new AlertDialog.Builder(Menu1.this);
    adb.setTitle("LVSelectedItemExample");
    adb.setMessage("Selected Item is = "+lv.getItemAtPosition(position));
    adb.setPositiveButton("Ok", null);
    adb.show();
    }

    });

How to make the header and footer become static? the header n footer become scrollable too, like the list view.. i had google n there is someone sugest 4 placing the header and footer outside the listview, but i don't know place to where ? i'm already try place it to my custom adapter n there's no good... so plz anybody can tell me how to fix this problem?

Upvotes: 1

Views: 5926

Answers (1)

Flo
Flo

Reputation: 27455

I think placing them outside of the ListView means that you should use custom view elements e.g. TextViews which you place above and below the ListView. These elements have nothing to do with the ListView anymore. So you cannot access them with lv.addHeaderView(v1); or lv.addFooterView(v); anymore. Instead you have to treat them like normal TextView elements. Of course you can also use custom Views instead of TextViews.

Update

The layout for you activity should look something like that. The first TextView is the header, followed by the ListView and below this we got another TextView as a footer. Header and footer don't need to TextViews they can be any kind of View.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView android:id="@+id/header_txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Footer Text"/>
    <ListView android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <TextView android:id="@+id/footer_txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Footer Text"/>
</LinearLayout>

So you will bind the adapter to the ListView as you already did. To set the text for the header and footer you use the following lines.

TextView header = (TextView) findViewById(R.id.header_text);
header.setText("This is my header text");
TextView footer = (TextView) findViewById(R.id.footer_text);
footer.setText("This is my footer text");

Upvotes: 2

Related Questions