您必须在android listview中提供layout_height属性错误

时间:2014-09-22 18:45:41

标签: android listview

我有一个适用于列表视图的适配器。它中的每一行都有两个textview,我编译项目的问题这个错误在log cat中出现给我

java.lang.RuntimeException:二进制XML文件行#8:您必须提供layout_height属性。

这是我的适配器

 public class ArticlesAdapter extends BaseAdapter {

    private ArrayList<ArticlesBean> arr;
    private LayoutInflater inflater;
    private ImageLoader mImageLoader;
    private String mytitle, myldesc;


    public ArticlesAdapter(Context context, ArrayList<ArticlesBean> arraylist) {
        this.arr = arraylist;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mImageLoader = new ImageLoader(context);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return arr.size();

    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return arr.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View v, ViewGroup parent) {

        ViewHolder holder = null;
        if (v == null) {

            v = inflater.inflate(R.layout.list_items, null);
            holder = new ViewHolder();

            holder.title = (TextView) v.findViewById(R.id.ltitle);
            holder.ldesc = (TextView) v.findViewById(R.id.ldesc);

            v.setTag(holder);

        } else {
            holder = (ViewHolder) v.getTag();
        }

        v = inflater.inflate(R.layout.list_items, null);
        holder.title = (TextView) v.findViewById(R.id.ltitle);
        holder.ldesc = (TextView) v.findViewById(R.id.ldesc);

        mytitle = arr.get(position).getTitle();
        myldesc = arr.get(position).getBody().substring(0, 45)
                .replace("\n", " ");

        Log.e("myldesc", myldesc);
        Log.e("mytitle", mytitle);

        holder.title.setText(mytitle);
        holder.ldesc.setText(myldesc);

        return v;
    }

    static class ViewHolder {

        public TextView title;
        public TextView ldesc;

    }

    public void refresh(ArrayList<ArticlesBean> items) {
        // TODO Auto-generated method stub
        arr.addAll(items);
        notifyDataSetChanged();
        // notifyDataSetChanged();
    }

}

这是我的xml文件

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

    <TextView
        android:id="@+id/ltitle"
        android:layout_width="match_parent" />

    <TextView
        android:id="@+id/ldesc"
        android:layout_width="match_parent" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

试试这个:

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

    <TextView
        android:id="@+id/ltitle"
        android:layout_width="match_parent"
        android:layout_height ="wrap_content" />

    <TextView
        android:id="@+id/ldesc"
        android:layout_width="match_parent"
        android:layout_height ="wrap_content" />

</LinearLayout>

您忘了在xml中添加layout_height。