自定义ListView没有填充

时间:2013-08-20 14:47:21

标签: java android listview

我已经处理了几个星期了。我已经看过所有其他相关问题而且没有用。我正在尝试填充自定义ListView并且不能让我的生活让它工作。我会发布所有相关信息,希望有人会注意到我的错误。

活动布局:

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivity"
                android:background="#000000">

    <ListView
    android:id="@+id/deviceListView"
    android:layout_width="400px"
    android:layout_height="fill_parent"
    android:textColor="#ffffff"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"/>
</LinearLayout>

自定义行布局:

<?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="horizontal"
              android:background="#000000">
    <TextView android:id="@+id/textId"
              android:textSize="50sp"
              android:text="ID"
              android:textColor="#FFFFFF"
              android:textStyle="bold"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"/>
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
            android:orientation="vertical">
    <TextView android:id="@+id/textTimestamp"
              android:textSize="16sp"
              android:text="Timestamp"
              android:textColor="#FFFFFF"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"/>
              android:layout_height="wrap_content"/>

    <TextView android:id="@+id/textCoordinates"
              android:textSize="16sp"
              android:text="Coordinates"
              android:textColor="#FFFFFF"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"/>

    <TextView android:id="@+id/textDistance"
              android:textSize="16sp"
              android:text="Distance"
              android:textColor="#FFFFFF"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

适配器型号:

public class DeviceList {
    public static ArrayList<Device> list;

    public static void loadModel(int quantity) {
        ArrayList<Device> listTmp = new ArrayList<Device>();
        for (int i = 0; i < quantity; i++) {
            listTmp.add(new Device(System.currentTimeMillis(), i, new float[]{0, 0}, System.currentTimeMillis() / (i + 1)));
        }
        list = listTmp;
    }

    public static Device GetById(int id){
        Device tmp = null;
        for(Device device : list){
            if (device.getID() == id){
                tmp = device;
            }
        }
        return tmp;
    }
}

适配器:

public class DeviceArrayAdapter extends ArrayAdapter<Device> {
    private Context context;
    private int rowResourceId;
    private String[] ids;

    public DeviceArrayAdapter(Context context, int rowResourceId, String[] devices) {
        super(context, rowResourceId);
        this.context = context;
        this.ids = devices;
        this.rowResourceId = rowResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.device_row, parent, false);
        TextView idTextview = (TextView) rowView.findViewById(R.id.textId);
        TextView timeStampTextview = (TextView) rowView.findViewById(R.id.textTimestamp);
        TextView coordinatesTextview = (TextView) rowView.findViewById(R.id.textCoordinates);
        TextView distanceTextview = (TextView) rowView.findViewById(R.id.textDistance);
        int id = Integer.parseInt(ids[position]);
        timeStampTextview.setText(Long.toString(DeviceList.GetById(id).getTimestamp()));
        idTextview.setText(DeviceList.GetById(id).getID());
        coordinatesTextview.setText(DeviceList.GetById(id).getCoordinates()[0] + "," + DeviceList.GetById(id).getCoordinates()[1]);
        distanceTextview.setText(DeviceList.GetById(id).getDistance() + "");
        return rowView;
    }
}

最后是Activity的onCreate方法:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        setContentView(R.layout.activity_main);

//        ArrayList<Device> list = createDummyDeviceList(5);
        DeviceList.loadModel(5);
        String[] ids = new String[DeviceList.list.size()];
        for (int i= 0; i < ids.length; i++){

            ids[i] = Integer.toString(i+1);
        }
        ListView listView = (ListView) findViewById(R.id.deviceListView);
        DeviceArrayAdapter adapter = new DeviceArrayAdapter(this, R.layout.device_row, ids);
        listView.setAdapter(adapter);
}

注意:很抱歉这样发帖但是我很绝望。我已经将它调试到最后一行,所有内容都已加载,并且没有例外。但它不会填充。

[编辑]

与选定的答案一起,我也有不同的编号数组和ID。我生成的ID从1开始而不是0。

2 个答案:

答案 0 :(得分:2)

您永远不会将数据集传递给超类。变化

super(context, rowResourceId);

super(context, rowResourceId, devices);

这样,ArrayAdapter的getCount返回设备长度,并调用getView。另外,为了避免浪费内存和提高性能,您应该只对convertView进行一次充气。

编辑。 super期待Device对象的Array或ArrayList。而是将您的设备放在String数组中。您的选择应该是一致的。你可以改变

extends ArrayAdapter<Device>

extends ArrayAdapter<String>

或将Device[]作为参数传递给自定义适配器。例如

public DeviceArrayAdapter(Context context, int rowResourceId, Device[] devices) {

答案 1 :(得分:0)