Android - Adding a header to a listview
<p>I can add a header to a listview when it's on the main activity without issue, but when I try to add a header to the listview when it's in an Async task, I get a Cast error.  Here's the main code (it's in a fragment):</p>

<pre><code>private List&lt;IssueInfo&gt; lIssueInfoList = null;
private ListView lvIssues;
private View v = null;
TextView tvHeader;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    try
    {
        v = inflater.inflate(R.layout.open_issues, container, false);

        //Set listview of Issues.
        lvIssues = (ListView) v.findViewById(R.id.lvIssues);
        lvIssues.setOnItemClickListener(viewIssuesListener);

        Context c = getActivity().getApplicationContext();
        LayoutInflater li = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ViewGroup vgHeader = (ViewGroup)li.inflate(R.layout.right_layout_header, null);
        tvHeader = (TextView)vgHeader.findViewById(R.id.tvRightHeaderText);

        //Call Async Task to run in the background.
        new LoadIssuesTask().execute();

    }
    catch (Exception e)
    {
        Errors.LogError(e);
    }
    return v;           
}
</code></pre>

<p>and the ASync Task:</p>

<pre><code>private class LoadIssuesTask extends AsyncTask&lt;Void, Void, Void&gt; {

    ProgressDialog pdDialog = null;

    protected void onPreExecute(){
        try
       {
           pdDialog = new ProgressDialog(v.getContext());
           pdDialog.setMessage("Loading Issues and Activities, please wait...");
           pdDialog.show();
       }
       catch (Exception e)
       {
           Errors.LogError(e);
       }
    }

    @Override
    protected Void doInBackground(Void... params) {
        LoadIssues();
        return null;
    }

    @Override
    protected void onPostExecute(Void v) {
        lvIssues.addHeaderView(tvHeader);
        lvIssues.setAdapter(new IssueInfoAdapter(OpenIssues.this.getView().getContext(), lIssueInfoList));
        pdDialog.dismiss();
        pdDialog = null;
    }
}
</code></pre>

<p>Finally, the LoadIssues method:</p>

<pre><code>private void LoadIssues(){
    IssueInfoCreator IssueCreator = new IssueInfoCreator(OpenIssues.this.getView().getContext(), Integer.parseInt(AppPreferences.mDBVersion));
    IssueCreator.open();
    tvHeader.setText(OpenIssues.this.getView().getContext().getString(R.string.OpenIssues) + " (" + String.valueOf(IssueCreator.fetchIssueCount()) + ")");
    lIssueInfoList = IssueCreator.queryAll();        
    IssueCreator.close();

}
</code></pre>

<p>Here's the error I'm getting:</p>

<pre><code>03-23 16:46:42.741: E/AndroidRuntime(12805): FATAL EXCEPTION: main
03-23 16:46:42.741: E/AndroidRuntime(12805): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
03-23 16:46:42.741: E/AndroidRuntime(12805):    at android.widget.ListView.clearRecycledState(ListView.java:513)
03-23 16:46:42.741: E/AndroidRuntime(12805):    at android.widget.ListView.resetList(ListView.java:499)
03-23 16:46:42.741: E/AndroidRuntime(12805):    at android.widget.ListView.setAdapter(ListView.java:442)
03-23 16:46:42.741: E/AndroidRuntime(12805):    at com.adacel.SimCareDashboard.OpenIssues$LoadIssuesTask.onPostExecute(OpenIssues.java:107)
03-23 16:46:42.741: E/AndroidRuntime(12805):    at com.adacel.SimCareDashboard.OpenIssues$LoadIssuesTask.onPostExecute(OpenIssues.java:1)
03-23 16:46:42.741: E/AndroidRuntime(12805):    at android.os.AsyncTask.finish(AsyncTask.java:602)
03-23 16:46:42.741: E/AndroidRuntime(12805):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
03-23 16:46:42.741: E/AndroidRuntime(12805):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
03-23 16:46:42.741: E/AndroidRuntime(12805):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-23 16:46:42.741: E/AndroidRuntime(12805):    at android.os.Looper.loop(Looper.java:137)
03-23 16:46:42.741: E/AndroidRuntime(12805):    at android.app.ActivityThread.main(ActivityThread.java:4424)
03-23 16:46:42.741: E/AndroidRuntime(12805):    at java.lang.reflect.Method.invokeNative(Native Method)
03-23 16:46:42.741: E/AndroidRuntime(12805):    at java.lang.reflect.Method.invoke(Method.java:511)
03-23 16:46:42.741: E/AndroidRuntime(12805):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-23 16:46:42.741: E/AndroidRuntime(12805):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-23 16:46:42.741: E/AndroidRuntime(12805):    at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<p>It's happening at this line:</p>

<pre><code>lvIssues.setAdapter(new IssueInfoAdapter(OpenIssues.this.getView().getContext(), lIssueInfoList));
</code></pre>

<p>Any ideas?</p>

<p><strong>UPDATE</strong></p>

<p>@MisterSquonk</p>

<p>I'm pretty new to Androind programming (only been at it 1.5 months) so I appreciate the tips on where and how things should and shouldn't be done.  I believe I implemented your suggestion correctly but I'm getting a Windows Leaked error now....here's the modified LoadIssues and Async Task:</p>

<pre><code>private String LoadIssues(){
    IssueCreator = new IssueInfoCreator(OpenIssues.this.getView().getContext(), Integer.parseInt(AppPreferences.mDBVersion));
    IssueCreator.open();
    lIssueInfoList = IssueCreator.queryAll();
    return OpenIssues.this.getView().getContext().getString(R.string.OpenIssues) + " (" + String.valueOf(IssueCreator.fetchIssueCount()) + ")";
}

private class LoadIssuesTask extends AsyncTask&lt;Void, String, String&gt; {

    ProgressDialog pdDialog = null;

    protected void onPreExecute(){
        try
       {
           pdDialog = new ProgressDialog(v.getContext());
           pdDialog.setMessage("Loading Issues and Activities, please wait...");
           pdDialog.show();
       }
       catch (Exception e)
       {
           Errors.LogError(e);
       }
    }

    @Override
    protected String doInBackground(Void... params) {
        return LoadIssues();
    }

    @Override
    protected void onPostExecute(String v) {
        IssueCreator.close();
        tvHeader.setText(v);
        lvIssues.addHeaderView(tvHeader);
        lvIssues.setAdapter(new IssueInfoAdapter(OpenIssues.this.getView().getContext(), lIssueInfoList));
        pdDialog.dismiss();
        pdDialog = null;
    }
}
</code></pre>

<p>Is that correct?</p>
