ListView in scrollable potential workaround

I have done all the research on this. I know that Google considers it pointless and that the developers know that this is not so. I also know that there is no known workaround, but I know that I am close to creating it. DougW posted this code:

public class Utility {
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter(); 
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
}

Which almost does the job for me. But when I try, I get a NullPointer exception in the listItem.measure (0, 0) line. ListItem itself is initialized, but the method still throws an exception. Please tell me how I can fix this.

Here is my code:

public class ExpenseReportsActivity extends Activity {

    private ListView lvReports;
    private ExpenseReportListAdapter adapter;
    private Button btnSend;
    private Button btnCancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expensereports);

        lvReports = (ListView)findViewById(R.id.lv_reports);
        lvReports.setBackgroundResource(R.drawable.shape_expense_report_list);

        ColorDrawable cd = new ColorDrawable(0xFFffffff);
        lvReports.setDivider(cd);
        lvReports.setDividerHeight(1);

        adapter = new ExpenseReportListAdapter(this);
        lvReports.setAdapter(adapter);

        int totalHeight = 0;
        for (int i = 0; i < adapter.getCount(); i++) {
            View listItem = adapter.getView(i, null, lvReports);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = lvReports.getLayoutParams();
        params.height = totalHeight + (lvReports.getDividerHeight() * (adapter.getCount() - 1));
        lvReports.setLayoutParams(params);
    }
}

, , onWindowFocusChanged. . , , Activiy onCreate, Activity onWindowFocusChanged. , ( WindowFocusChanged, Activity onWindowFocusChanged).

+2
2

, , , ListView.addFooterView(View v):

http://developer.android.com/reference/android/widget/ListView.html#addFooterView (android.view.View)

+ " " .

, :

import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;

public class YourActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LayoutInflater factory = getLayoutInflater();
        LinearLayout footer = 
            (LinearLayout) factory.inflate(R.layout.your_a_few_buttons_footer, null);

        getListView().addFooterView(footer);

        String[] array = new String[50];
        for (int i = 0; i < 50;) { array[i] = "LoremIpsum " + (++i); }

        setListAdapter(
            new ArrayAdapter<String>(this, R.layout.list_item, array)
        );
    }   
}

, , addFooterView() setListAdapter().

UPDATE: View ListView.addHeaderView(View v). , , , LinearLayout View. , , , , .

+9

, RelativeLayout? , measure(0,0) NPE, LinearLayout . http://groups.google.com/group/android-developers/browse_thread/thread/5a947482d7dcb605

Linear, . , !


, , , ... , , , ListView . 10 , . 4 , , ListView , -. , ListView , .

, MergeAdapter, , ListView . ListView .

, ​​, . , ListView , , ScrollView. ( , iOS SDK, , , .)

+1

All Articles