Android - ListView仅显示一行

时间:2013-05-01 11:26:21

标签: android android-layout android-linearlayout

我遇到了ListView的问题,它在ListView中只显示了一行。我认为问题出在布局行xml中,但我无法弄清楚问题出在哪里。谢谢你的帮助,我会贬低它。

这是我的row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/fondgris"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/imageTypeWorkitem"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:paddingTop="10dp"
                android:src="@drawable/bugicon" />

            <TextView
                android:id="@+id/typeWorkitem"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:paddingTop="10dp"
                android:textSize="14sp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/imagePriority"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:paddingTop="10dp"
                android:src="@drawable/priorityhigh" />

            <TextView
                android:id="@+id/priority"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:paddingTop="10dp"
                android:textSize="14sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/key"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:paddingTop="10dp"
                android:textSize="14sp"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/imageStatus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:paddingTop="10dp"
                android:src="@drawable/statusopen" />

            <TextView
                android:id="@+id/status"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/priority"
                android:layout_marginLeft="10dp"
                android:paddingTop="10dp"
                android:textSize="14sp"
                android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

这是我的适配器:

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.client.alm.model.WorkItem;
import com.client.alm.model.WorkItemContainer;

public class WorkItemAdapter extends ArrayAdapter<WorkItem> {

    private WorkItemContainer container;
    private Context context;
    int layoutResourceId;

    public WorkItemAdapter(Context context, int layoutResourceId,
            WorkItemContainer container) {
        super(context, layoutResourceId, container.getWorkitems());
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.container = container;
    }

    static class ViewHolder {
        ImageView imageType;
        TextView type;
        TextView priority;
        ImageView imagePriority;
        TextView key;
        ImageView imageStatus;
        TextView status;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new ViewHolder();
            holder.imageType = (ImageView) row
                    .findViewById(R.id.imageTypeWorkitem);
            holder.type = (TextView) row.findViewById(R.id.typeWorkitem);
            holder.imagePriority = (ImageView) row
                    .findViewById(R.id.imagePriority);
            holder.priority=(TextView) row.findViewById(R.id.priority);
            holder.key = (TextView) row.findViewById(R.id.key);
            holder.imageStatus = (ImageView) row.findViewById(R.id.imageStatus);
            holder.status = (TextView) row.findViewById(R.id.status);

            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        WorkItem workitem = container.getWorkitems().get(position);
        holder.type.setText(workitem.getType());
        // holder.imageType.setImageResource(workitem.getIcon());
        holder.priority.setText(workitem.getPriority());
        holder.key.setText(workitem.getKey());
        holder.status.setText(workitem.getStatus());

        return row;
    }

}

这是我加载列表的代码:

WorkItemContainer container = gson.fromJson(resultat,
                    WorkItemContainer.class);

            /**
             * Updating parsed JSON data into ListView
             * */
            WorkItemAdapter adaptateur = new WorkItemAdapter(
                    WorkItemActivity.this, R.layout.ligne_workitem, container);
            setListAdapter(adaptateur);

2 个答案:

答案 0 :(得分:1)

  1. 您可以尝试使用RelativeLayout设计单行。
  2. 摆脱最外面的LinearLayout。您正在使用这么多线性布局来设计一个简单的行。这可能会影响UI的性能。
  3. 休息,如果不查看实例化ListView且设置Adapter的代码,我不能说太多。祝你好运。

答案 1 :(得分:0)

我认为问题是你的listview高度。

将listview height设为match_parent