Android 自定义数组适配器 getView 未被调用

Android custom array adapter getView is not called

这是我的适配器,即使数组列表有元素也不会调用 getView 方法

public class CarrierSammuryAdapter extends ArrayAdapter<CarrierSummary> {

private final Activity context;
private final ArrayList<CarrierSummary> ite;
private final int lay;


public CarrierSammuryAdapter(Activity context, ArrayList<CarrierSummary> menuLinkList,int layout) {
    super(context,layout );
    this.context = context;
    this.ite = menuLinkList;
    this.lay=layout;




}
// static to save the reference to the outer class and to avoid access to
        // any members of the containing class

    static class ViewHolder {
    public  TextView customer;
    public  TextView attempts;
    public    TextView successful;
    public    TextView  minutes;
    public    TextView ASR;
    public    TextView ACD;
    public    TextView NER;
    public    TextView PDD;

    }
    @Override
    public int getCount () {
        return ite.size();
    }

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

    @Override
    public CarrierSummary getItem (int position) {
        return ite.get(position);
    }

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // ViewHolder will buffer the assess to the individual fields of the row layout

            final ViewHolder holder;

            // Recycle existing view if passed as parameter
                    // This will save memory and time on Android
                    // This only works if the base layout for all classes are the same
                    View rowView = convertView;
                    if (rowView == null) {
            LayoutInflater inflater =  context.getLayoutInflater();//this gives error !!
            rowView=inflater.inflate(R.layout.carriersumamry_item,parent,false);

             holder = new ViewHolder();

             holder.customer=(TextView)rowView.findViewById(R.id.customer);
              holder.attempts=(TextView)rowView.findViewById(R.id.attempts);
              holder.successful=(TextView)rowView.findViewById(R.id.successful);
              holder.minutes=(TextView)rowView.findViewById(R.id.minutes);
              holder.ASR=(TextView)rowView.findViewById(R.id.asr);
              holder.ACD=(TextView)rowView.findViewById(R.id.acd);
              holder.NER=(TextView)rowView.findViewById(R.id.ner);
              holder.PDD=(TextView)rowView.findViewById(R.id.pdd);
            // ViewResizing.setListRowTextResizing(rowView, context);

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

                      holder.customer.setText(ite.get(position).getCustomer());
                       holder.attempts.setText(ite.get(position).getAttempts());
                       holder.successful.setText(ite.get(position).getSuccessful());
                       holder.minutes.setText(ite.get(position).getMinutes());
                       holder.ASR.setText(ite.get(position).getASR());
                       holder.ACD.setText(ite.get(position).getACD());
                       holder.NER.setText(ite.get(position).getNER());
                       holder.PDD.setText(ite.get(position).getPDD());
            return rowView;


}

}

我就是这样称呼它的

         carrierSummaryList =(ListView)findViewById(R.id.carrierSummary_listview);
        CarrierSammuryAdapter adapter = new CarrierSammuryAdapter(CarrierSummaryActivity.this,  carriersam,R.layout.carriersumamry_item);
        carrierSummaryList.setAdapter(adapter);

我搜索了很多来解决这个问题,但没有解决方案,从未调用 getView 方法。

这是我的XML

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
   android:layout_height="wrap_content"
  android:orientation="horizontal"
  >

 <TextView 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pdd"
android:textColor="@color/grey"
/>
 <TextView 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ner"
android:textColor="@color/grey"
/>
  <TextView 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/acd"
   android:textColor="@color/grey"
   />
   <TextView 
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/asr"
  android:textColor="@color/grey"
   />
<TextView 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/minutes"
  android:textColor="@color/grey"
   />
 <TextView 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
   android:id="@+id/successful"
   android:textColor="@color/grey"
   />
   <TextView 
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
    android:id="@+id/attempts"
     android:textColor="@color/grey"
    />
    <TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     android:id="@+id/customer"
    android:textColor="@color/grey"
     />
  </LinearLayout>

有两个错误.. 首先是在构造函数中将 Activity 更改为 Context 其次改变

rowView=inflater.inflate(R.layout.single_row,parent,false);

希望对您有所帮助

我遇到了完全相同的问题,为了解决它,我只是将 ArrayAdapter 更改为 BaseAdapter,实现了重写的方法并且它起作用了。

正如 khuskal 所说,您应该始终使用 Context 而不是 Activity

/**
 * Constructor
 *
 * @param context The current context.
 * @param resource The resource ID for a layout file containing a TextView to use when
 *                 instantiating views.
 */
public ArrayAdapter(Context context, int resource) {
    init(context, resource, 0, new ArrayList<T>());
}

当您执行 super(context,layout) 时,这是被调用的构造函数。所以适配器数据被设置为空列表。

就像德里克说的,你应该使用 super(context,layout,menuLinkList);

/**
     * Constructor
     *
     * @param context The current context.
     * @param resource The resource ID for a layout file containing a TextView to use when
     *                 instantiating views.
     * @param objects The objects to represent in the ListView.
     */
    public ArrayAdapter(Context context, int resource, List<T> objects) {
        init(context, resource, 0, objects);
    }

问题出在我的 drawerLayout 中,我将 listView 放入 RelativeLayout 中,但是当我更改为 LinearLayout 时它起作用了,这是代码

  <android.support.v4.widget.DrawerLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
  android:layout_width="fill_parent"
   android:layout_height="fill_parent" 
   >
    <LinearLayout 
    android:layout_width="fill_parent"
     android:layout_height="fill_parent"
    android:background="@android:color/white"

     android:orientation="vertical">

      <include 

    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/mainbar" 

    layout="@layout/second_topmain"/>

          <ListView
        android:id="@+id/carrierSummary_listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"



  android:textColor="@android:color/black"


       ></ListView>


       <include 

    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
  android:layout_marginBottom="0dp"
    android:id="@+id/mainbar"
    layout="@layout/bottom_bar"/>
     </LinearLayout>
      <LinearLayout 
    android:id="@+id/linearSlider"
     android:layout_width="wrap_content"
      android:layout_height="fill_parent"
         android:layout_gravity="right"
      android:background="#7e7e7e"
         android:orientation ="vertical">
     <TextView

         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
       android:text="Carrier Type"
       android:textColor="@android:color/white"
       android:padding="5dp"
       android:background="@color/darkgrey"


       />
  <LinearLayout 
      android:padding="3dp"
       android:layout_width="wrap_content"
       android:orientation="horizontal"
       android:background="@color/darkgrey"
      android:layout_height="wrap_content">
      <Button
    android:id="@+id/customerButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
       android:textColor="@color/grey"
    android:onClick="toggleClicked"
    android:background="@drawable/toggleon"

    android:text="Customer"

     />

           <Button
    android:id="@+id/SupplierButton"
    android:layout_width="wrap_content"
       android:layout_height="wrap_content"
     android:onClick="toggleClicked"

    android:textColor="@color/grey"

          android:background="@drawable/toggleoff"


    android:text="Supplier"

    />
        </LinearLayout>
             <RelativeLayout 
      android:padding="3dp"

       android:layout_width="wrap_content"
       android:orientation="horizontal"
      android:layout_height="wrap_content">
             <TextView

         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
       android:text="Carrier"
       android:layout_alignParentLeft="true"

         android:layout_centerHorizontal="true"

       />
             <Spinner
                    android:layout_width="wrap_content"
         android:layout_height="wrap_content"
                 android:id="@+id/spinnerCarrier"
                  android:layout_alignParentRight="true"
                 android:background="@android:color/transparent"
                 android:layout_centerHorizontal="true"

                 />
             </RelativeLayout>
           <RelativeLayout 
      android:padding="3dp"
         android:background="@color/darkgrey"
       android:layout_width="wrap_content"

      android:layout_height="wrap_content">
             <TextView

         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
       android:text="Top"
       android:layout_alignParentLeft="true"

         android:layout_centerHorizontal="true"

       />
             <Spinner
                    android:layout_width="wrap_content"
         android:layout_height="wrap_content"
                 android:id="@+id/SpinnerTop"
                  android:layout_alignParentRight="true"
                 android:background="@android:color/transparent"
                 android:layout_centerHorizontal="true"

                 />
             </RelativeLayout>

                       <RelativeLayout 
      android:padding="3dp"

       android:layout_width="wrap_content"
       android:onClick="showDateTimePickerFrom"
      android:layout_height="wrap_content">
             <TextView

         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
       android:text="From Date"
       android:layout_alignParentLeft="true"

         android:layout_centerHorizontal="true"

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

                  android:layout_alignParentRight="true"
                 android:background="@android:color/transparent"
                 android:layout_centerHorizontal="true"

                 />
             </RelativeLayout>


         <RelativeLayout 
      android:padding="3dp"
              android:background="@color/darkgrey"
       android:layout_width="wrap_content"
       android:onClick="showDateTimePickeTo"
      android:layout_height="wrap_content">
             <TextView

         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
       android:text="To Date"
       android:layout_alignParentLeft="true"

         android:layout_centerHorizontal="true"

       />
             <TextView 

           android:layout_width="wrap_content"
         android:layout_height="wrap_content"
                 android:id="@+id/toDate"
                  android:layout_alignParentRight="true"
                 android:background="@android:color/transparent"
                 android:layout_centerHorizontal="true"

                 />
             </RelativeLayout>


              <RelativeLayout 
      android:padding="3dp"

       android:layout_width="wrap_content"

      android:layout_height="wrap_content">
             <TextView

         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
       android:text="Group By Profile"
       android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
         android:layout_centerHorizontal="true"

       />
             <CheckBox 
           android:layout_width="wrap_content"
         android:layout_height="wrap_content"
                 android:id="@+id/byprofilecheck"
                  android:layout_alignParentRight="true"
                 android:background="@android:color/transparent"

                 android:layout_centerHorizontal="true"

                 />
             </RelativeLayout>
        </LinearLayout>


        </android.support.v4.widget.DrawerLayout>