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

时间:2015-09-14 12:16:23

标签: android listview android-arrayadapter

这是我的适配器,即使数组列表中包含元素

,也不会调用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>

4 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,为了解决这个问题,我只是将ArrayAdapter更改为BaseAdapter,实现了覆盖方法并且有效。

正如khuskal所说,你应该始终使用Context代替Activity

答案 1 :(得分:1)

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

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

希望这会对你有所帮助

答案 2 :(得分:0)

/**
 * 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>());
}

当你做超级(上下文,布局)时,这是被调用的构造函数。所以适配器数据设置为空列表。

就像derek所说的那样,你应该使用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);
    }

答案 3 :(得分:0)

问题出在我的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>