ListView不膨胀

时间:2013-08-28 09:10:34

标签: android android-listview

大家好日子。

我目前正在制作一个自定义的ListView,它不会在活动中膨胀(不会变得可见)。

以下是xml和ListView适配器类。

main.xml中

<LinearLayout 
    android:id="@+id/llListView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="2dp"
    android:paddingBottom="2dp"
    android:layout_below="@+id/rlFirstRow">

    <ListView 
        android:id="@+id/lvErrorsReport"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/border"/>

</LinearLayout>

listview_row.xml

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

<LinearLayout
    android:id="@+id/llError"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_alignParentTop="true">

    <TextView
        android:id="@+id/txtErrorInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textStyle="bold"
        android:textSize="20dp"/>

</LinearLayout>

    <LinearLayout
    android:id="@+id/llStatus"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_below="@+id/llError">

    <TextView
        android:id="@+id/txtStatus"
        android:layout_width="100dp" 
        android:layout_height="wrap_content"
        android:text="Status: "/>

    <TextView
        android:id="@+id/txtStatusInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textStyle="italic"/>

</LinearLayout>

   <LinearLayout
    android:id="@+id/llMaterials"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_below="@+id/llStatus">

    <TextView
        android:id="@+id/txtMaterials"
        android:layout_width="100dp" 
        android:layout_height="wrap_content"
        android:text="Materials: "/>

    <TextView
        android:id="@+id/txtMaterialsInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textStyle="italic"/>

</LinearLayout>

   <LinearLayout
   android:id="@+id/llButton"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   android:layout_below="@+id/llMaterials">

   <Button 
       android:id="@+id/btnShowMaterials"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Show Materials"
       android:visibility="gone"/>
   </LinearLayout>

</RelativeLayout>

BaseAdapter类

public class RepairReportListViewAdapter extends BaseAdapter 
{
ArrayList<String> errors;
ArrayList<String> statuses;
ArrayList<Boolean> materials;
Context mContext;
LayoutInflater inflater;

public RepairReportListViewAdapter(Context context, ArrayList<String> err, ArrayList<String> stat, ArrayList<Boolean> mat)
{
    this.mContext = context;
    this.errors = err;
    this.statuses = stat;
    this.materials = mat;

    inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount()
{
    return 0;
}

@Override
public Object getItem(int position)
{
    return null;
}

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

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

    if(convertView == null)
    {
        holder = new ViewHolder();

        convertView = inflater.inflate(R.layout.repairreport_listview, null);

        holder.error = (TextView)convertView.findViewById(R.id.txtErrorInfo);
        holder.status = (TextView)convertView.findViewById(R.id.txtStatusInfo);
        holder.material = (TextView)convertView.findViewById(R.id.txtMaterialsInfo);
        holder.showMaterials = (Button)convertView.findViewById(R.id.btnShowMaterials);

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

    holder.error.setText(this.errors.get(position).toString());
    holder.status.setText(this.statuses.get(position).toString());

    if(this.materials.get(position) == true)
    {
        holder.material.setText("Materials where used to fix this error. Click on the button below to view the materials.");
        holder.showMaterials.setVisibility(Button.VISIBLE);
        holder.showMaterials.setOnClickListener(new View.OnClickListener()
        {       
            @Override
            public void onClick(View v) 
            {
                //LATER
            }
        });
    }
    else
    {
        holder.material.setText("No materials where used to fix this error.");
    }

    return convertView;
}

static class ViewHolder
{
    TextView error;
    TextView status;
    TextView material;
    Button showMaterials;
}

   }

我已尝试按照建议here更改布局,但仍未显示ListView

为什么它没有显示任何想法?很抱歉代码转储,但我真的无法弄清楚到底发生了什么。

P.S。我确实将适配器设置为主要活动中的ListView

    adapter = new RepairReportListViewAdapter(this, errorNames, statuses, materialsInUse);
    lvErrors.setAdapter(adapter);

1 个答案:

答案 0 :(得分:3)

由于getCount正在返回0

,因此ListView不会全部膨胀

变化

@Override
public int getCount()
{
    return 0;
}

@Override
public int getCount()
{
    return (stat == null) ? 0 : stat.size();
}

此外,您可以创建一个包含所需信息的类以及此类的ArrayList,而不是保留三个不同的ArrayList