2013-08-28 30 views
1

美好的一天。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; 
} 

    } 

我已经试图改变布局的建议hereListView仍不透露。

任何想法为什么它没有显示?抱歉代码转储,但我真的无法弄清楚到底是怎么回事。

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

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

回答

3

您的ListView不夸大任何操作,因为getCount将返回0

变化

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

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

此外,而不是保持3型动物ArrayList中,您可以创建一个拥有你需要的所有信息的类以及这个类的一个ArrayList

+0

太好了。我不知道它是需要为了显示'ListView'。非常感谢你! – ClaireG