将进度条作为页脚添加到listview会导致setAdapter调用

时间:2015-09-15 01:13:39

标签: android android-layout listview android-arrayadapter

我想在listview的末尾添加一个进度条,该进度条在发现蓝牙设备时填充。以下是活动的相关代码:

public class BluetoothScanner extends CheckBoxInListViewActivity {

    protected void onCreate(Bundle savedInstanceState) {

        ListView listView = (ListView) findViewById(R.id.new_discovered_device_list);

        if (progressBar == null) {
            progressBar = (ProgressBar) findViewById(R.id.scan_progress);
        }
        listView.addFooterView(progressBar);   //Creates a problem

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            ...
        });
        setCheckBoxInListViewAdapter(new CheckBoxInListViewAdapter(this, R.layout.paired_device_info, new ArrayList<BluetoothDevice>()));
        getCheckBoxInListViewAdapter().setNotifyOnChange(true);
        listView.setAdapter(getCheckBoxInListViewAdapter());    //line 113
    }
}

活动的布局是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:orientation="vertical"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="edu.unm.twin_cities.graphit.activity.PairedDeviceSelection">

    <ListView
        android:id="@+id/new_discovered_device_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:minHeight="?android:attr/listPreferredItemHeight" />

    <ProgressBar
        android:id="@+id/scan_progress"
        android:visibility="invisible"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

CheckBoxInListViewActivity是一个抽象活动,它包含适配器的代码,因为我在多个地方使用相同的适配器。

public abstract class CheckBoxInListViewActivity extends Activity {

    private CheckBoxInListViewAdapter checkBoxInListViewAdapter;

    private static class ViewHolder {
        ...
    }

    protected class CheckBoxInListViewAdapter extends ArrayAdapter<BluetoothDevice> {
        // Stanard pattern implemented. Works fine before I added addFooterView 
        // call to the code.
    }
}

将addFooterView调用添加到代码后,应用程序开始崩溃。我收到错误:java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

完成堆栈跟踪:

   java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.unm.twin_cities.graphit/edu.unm.twin_cities.graphit.activity.BluetoothScanner}: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5371)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
            at android.widget.ListView.clearRecycledState(ListView.java:519)
            at android.widget.ListView.resetList(ListView.java:506)
            at android.widget.ListView.setAdapter(ListView.java:448)
            at edu.unm.twin_cities.graphit.activity.BluetoothScanner.onCreate(BluetoothScanner.java:113)
            at android.app.Activity.performCreate(Activity.java:5122)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1084)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
            at android.app.ActivityThread.access$600(ActivityThread.java:162)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5371)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您需要从主布局中删除ProgressBar,然后创建一个具有ProgressBar的不同布局文件,然后使用LayoutInflater对其进行充气,最后将其添加到ListView。请查看以下代码:

public class MainActivity extends Activity
{

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

        ListView listView = (ListView) findViewById(R.id.new_discovered_device_list);

        ProgressBar progressBar = (ProgressBar) LayoutInflater.from(this).inflate(R.layout.progress_bar, null);

        listView.addFooterView(progressBar);

        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[]{"a", "b"}));
    }
}

R.layout.progress_bar文件如下:

<ProgressBar
    android:id="@+id/scan_progress"
    android:visibility="invisible"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"/>

最后,您的主要布局应如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
          android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
          android:orientation="vertical"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin"
          android:paddingBottom="@dimen/activity_vertical_margin"
          tools:context="edu.unm.twin_cities.graphit.activity.PairedDeviceSelection">

<ListView
        android:id="@+id/new_discovered_device_list"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:minHeight="?android:attr/listPreferredItemHeight" />
</LinearLayout>