getView()方法中自定义适配器ListView出错

时间:2015-10-29 19:19:31

标签: android listview android-listview

我在申请中面临一种非常奇怪的行为 问题是我的活动中有一个导航抽屉,但我需要在导航栏中显示不同的ListView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="top">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height"
        android:background="@drawable/side_nav_bar"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:theme="@style/ThemeOverlay.AppCompat.Dark" android:orientation="vertical"
        android:gravity="bottom">
        <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:src="@drawable/ic_logo" android:id="@+id/imageView" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="@string/lb_dashboard_menu"
            android:id="@+id/textView"
            android:textColor="@color/colorPrimaryDark" />
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/menu_gateways"
        android:textColor="@color/text_color"
        android:gravity="center_vertical"
        android:paddingLeft="16dp"
        android:background="@color/background_clear" />
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lv_menu_gateway"
        android:minHeight="90dp"
        android:background="@color/background_accent" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/menu_options"
        android:textColor="@color/text_color"
        android:gravity="center_vertical"
        android:paddingLeft="16dp"
        android:background="@color/background_clear"
        android:paddingRight="16dp" />
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv_menu_options"
        android:background="@color/background_accent" />
</LinearLayout>

正如您在上面的代码中看到的,问题是当我为第一个ListView加载适配器时,我试图调试它,方法getView()无限期地重复,没有任何明显的理由。< / p>

在第二个ListView中,我确定我传递了5个项目的列表,但是在调试时我发现位置始终为0。 这就是为什么我只获得活动中列表的第一项。

public class ListGatewayAdapter extends BaseAdapter {

    protected List<Device> devices;
    private static LayoutInflater inflater;
    AppManager manager;

    public ListGatewayAdapter(Activity activity, List<Device> devices){
        this.devices = devices;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        manager = AppManager.getManager(activity.getApplication());
    }

    @Override
    public int getCount() {
        return devices.size();
    }

    @Override
    public Device getItem(int position) {
        return devices.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if(convertView == null)
            view = inflater.inflate(R.layout.gateway_cell, parent, false);
        TextView name = (TextView)view.findViewById(R.id.lb_gateway_name);
        TextView number = (TextView)view.findViewById(R.id.lb_gateway_number);
        ImageView status = (ImageView)view.findViewById(R.id.im_gateway_active);
        Device device = getItem(position);
        name.setText(device.getDeviceName());
        number.setText(device.getDeviceNumber());
        String lastDeviceConnected = manager.RetrieveSharedLastDeviceConnect();
        if(lastDeviceConnected != null){
            if(lastDeviceConnected.equals(device.getDeviceNumber()) ) {
                status.setColorFilter(Color.argb(0, 0, 0, 255));
            }else{
                status.setColorFilter(Color.argb(0, 0, 0, 0));
            }
        }
        return view;
    }
}

上面是第一个适配器的代码,第二个是与此非常相似的代码。

我希望有些人可以帮我找到解决这个问题的方法。

更新我在显示器中注意到我得到了这个:

10-29 15:23:07.330 8785-8785/com.realstatediary.jperera.rapidsentrymaster W/View: requestLayout() improperly called by android.widget.ListView{11445886 VFED.VC. .F....ID 0,540-888,810 #7f0c007c app:id/lv_menu_gateway} during layout: running second layout pass
10-29 15:23:07.370 8785-8785/com.realstatediary.jperera.rapidsentrymaster W/View: requestLayout() improperly called by android.widget.ListView{11445886 VFED.VC. ........ 0,540-888,810 #7f0c007c app:id/lv_menu_gateway} during second layout pass: posting in next frame

1 个答案:

答案 0 :(得分:0)

尝试将第一个ListView的布局宽度和高度更改为“match_parent”。