我ListView用的是BaseAdapter适配器,ListView代码如下:             <ListView
                android:id="@+id/validate01_rule1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fastScrollEnabled="true"/>
在BaseAdapter中大概有200多行数据,每个getView中是一个LinearLayout,在LinearLayout中是四个TextView,如下:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/tv_cell01"
        android:layout_width="60dip"
        android:layout_height="30dip"
        android:gravity="center"
        android:height="30dip"
        android:textColor="#505050" 
        android:background="#ffffff"/> 
    <TextView
        android:id="@+id/validate01_tv_cell02"
        android:layout_width="0dip"
        android:layout_marginLeft="1dip"
        android:layout_height="30dip"
        android:layout_weight="1"
        android:gravity="center"
android:singleLine="true"
        android:textColor="#505050" 
        android:background="#ffffff"
        android:drawableRight="@drawable/arrow_drop"/> 
    <TextView
        android:id="@+id/validate01_tv_cell03"
        android:layout_width="0dip"
        android:layout_marginLeft="1dip"
        android:layout_height="30dip"
        android:layout_weight="1"
        android:gravity="center"
android:singleLine="true"
        android:textColor="#505050" 
        android:background="#ffffff"
        android:drawableRight="@drawable/arrow_drop"/> 
    <TextView
        android:id="@+id/tv_cell04"
        android:layout_width="60dip"
        android:layout_marginLeft="1dip"
        android:layout_height="30dip"
        android:gravity="center"
        android:textColor="#505050" 
        android:background="#ffffff"/> 
</LinearLayout>现在getCount方法能得到数据,是200,但是getView方法不执行,这是怎么回事呢?listview适配器adapterBaseAdaptergetView

解决方案 »

  1.   

    补充:getView不是每次都不调用,有时候执行,偶尔不执行。但是getCount每次都执行且有值。
      

  2.   

    我再getView中打印log没有输出,说明适配器根本没有绘制。但是getCount有返回值。
      

  3.   

    楼主你的问题在adapter里面,你却没有粘贴其代码
      

  4.   


    import android.content.Context;
    import android.graphics.Color;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ListView;
    import android.widget.PopupWindow;
    import android.widget.TextView;import com.meritit.lottery.R;
    import com.meritit.lottery.dao.BaseDao;
    import com.meritit.lottery.util.Util;
    /**
     * 数据列表适配器
     * @author
     *
     */
    public class ValidateListAdapter extends BaseAdapter{
    private LayoutInflater mInflater;      //界面载入器
    private String[][] datas;     //所有的历史区域数据                   //历史区域数量
    private Context context;
    private View popupview;
    public static PopupWindow popup;
    private ListView popupList;
    private String[] type;
    private BaseDao dao;

    /**
     * 适配器的构造器
     * @param context 上下文
     * @param datas   数据
     */
    public ValidateListAdapter(Context context, String[][] datas, String[] type, BaseDao dao){
    System.out.println("我进适配器了");
    this.datas = datas;
    System.out.println("适配器里面的datas.length = " + datas.length);
    this.context = context;
    this.type = type;
    this.dao = dao;
    mInflater = LayoutInflater.from(context);  //载入界面
    init();
    }

    public void init(){
    popupview = mInflater.inflate(R.layout.validate_popupwindow, null);
    popupList = (ListView) popupview.findViewById(R.id.popup_list);
    popup = new PopupWindow(popupview, Util.dip2px(context, 200),
    Util.dip2px(context, 240));
    }
     
    @Override
    public int getCount() {
    System.out.println("getCount方法 = " + datas.length);
    return datas.length;
    } @Override
    public Object getItem(int pos) {
    // TODO Auto-generated method stub
    return null;
    } @Override
    public long getItemId(int pos) {
    // TODO Auto-generated method stub
    return pos;
    } @Override
    public View getView(int position, View convertView, ViewGroup parent) {

    System.out.println("我进到了适配器中的getView方法");
            //获取填充对象
          convertView = mInflater.inflate(R.layout.validate01_cell, null);      
          TextView cellView1 = (TextView) convertView.findViewById(R.id.tv_cell01);   
          TextView cellView2 = (TextView) convertView.findViewById(R.id.validate01_tv_cell02);
          TextView cellView3 = (TextView) convertView.findViewById(R.id.validate01_tv_cell03);
          TextView cellView4 = (TextView) convertView.findViewById(R.id.tv_cell04); 
          String[] cell =  datas[position];
          System.out.println("适配器里的cell = " + cell);
          if("1".equals(cell[3])){
           cellView2.setBackgroundColor(Color.rgb(120, 169, 214));
          }
          if("1".equals(cell[4])){
           cellView3.setBackgroundColor(Color.rgb(120, 169, 214));
          }
    //设置文本内容
         cellView1.setText(cell[0]);
         cellView2.setText(cell[1]+"#"+cell[0]+"#"+formatValue(Util.formatLotterNo(cell[5])+""));
         cellView3.setText(cell[2]+"#"+cell[0]+"#"+formatValue(Util.formatLotterNo(cell[5])+""));
         cellView4.setText(cell[5]);
        
          cellView2.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    TextView tv = (TextView) v;
    String text = tv.getText().toString();
    String t[] = text.split("#");
    String result = dao.getAllDataByNoQs(t[1], type[0]);
    String[] res = result.split("\\|");
    int[] len = new int[res.length];
    int[] index = new int[res.length];
    for(int j=0; j<res.length; j++){
    len[j] = res[j].split(",").length;
    int sb = res[j].indexOf(t[2]);
    if(sb != -1){
    index[j] = res[j].substring(0, sb).split(",").length;
    }else{
    index[j] = -1;
    }
    }
    String[] results = new String[res.length * 2];
    for(int i=1; i<results.length; i=i+2){
    results[i] = res[(i-1)/2];
    }
    switch(res.length){
    case 3:
    if(index[2] == -1){
    results[4] = "豹子号["+len[2]+"]";
    }else{
    results[4] = "豹子号["+len[2]+"]  位置"+(index[2] + 1);
    }
    case 2:
    if(index[1] == -1){
    results[2] = "组三["+len[1]+"]";
    }else{
    results[2] = "组三["+len[1]+"]  位置" +(index[1] + 1);
    }
    case 1:
    if(index[0] == -1){
    results[0] = "组六["+len[0]+"]";
    }else{
    results[0] = "组六["+len[0]+"]  位置" +(index[0] + 1);
    }

    }

    PopupListAdapter popupAdapter =new PopupListAdapter(context, results, t[2]);
    popupList.setAdapter(popupAdapter);
    popup.showAsDropDown(v);
    popup.showAtLocation(v, Gravity.CENTER, Util.dip2px(context, 60), 0);
    }
    });
          
          cellView3.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    TextView tv = (TextView) v;
    String text = tv.getText().toString();
    String t[] = text.split("#");
    String result = dao.getAllDataByNoQs(t[1], type[1]);
    String[] res = result.split("\\|");
    int[] len = new int[res.length];
    int[] index = new int[res.length];
    for(int j=0; j<res.length; j++){
    len[j] = res[j].split(",").length;
    int sb = res[j].indexOf(t[2]);
    if(sb != -1){
    index[j] = res[j].substring(0, sb).split(",").length;
    }else{
    index[j] = -1;
    }
    }
    String[] results = new String[res.length * 2];
    for(int i=1; i<results.length; i=i+2){
    results[i] = res[(i-1)/2];
    }
    switch(res.length){
    case 3:
    if(index[2] == -1){
    results[4] = "豹子号["+len[2]+"]";
    }else{
    results[4] = "豹子号["+len[2]+"]  位置"+(index[2] + 1);
    }
    case 2:
    if(index[1] == -1){
    results[2] = "组三["+len[1]+"]";
    }else{
    results[2] = "组三["+len[1]+"]  位置" +(index[1] + 1);
    }
    case 1:
    if(index[0] == -1){
    results[0] = "组六["+len[0]+"]";
    }else{
    results[0] = "组六["+len[0]+"]  位置" +(index[0] + 1);
    }

    }
    PopupListAdapter popupAdapter =new PopupListAdapter(context, results, t[2]);
    popupList.setAdapter(popupAdapter);
    popup.showAsDropDown(v);
    popup.showAtLocation(v, Gravity.CENTER, Util.dip2px(context, 60), 0);
    }
    });
    return convertView;
    }

    public String formatValue(String value){
    if(1 == value.length()){
              value = "00" + value;
        }else if(2 == value.length()){
              value = "0" + value;
        }  
    return value;
    }}
      

  5.   

     convertView = mInflater.inflate(R.layout.validate01_cell, null);这里啊~
    convertView是移出屏幕的View对象,你一开始加载时没有移出屏幕的View对象,这里肯定是null,你返回出去也没用啊
    你要用View类去创建啊,不能用convertView
      

  6.   

    把 popupview = mInflater.inflate(R.layout.validate_popupwindow, null);换成
     popupview = mInflater.inflate(R.layout.validate_popupwindow, parent,false);
      

  7.   

    你getview中每判断一下convertView是不是为null,可以复用view,你这样效率很低。
    还有,你的代码你就不能封装一下,一团乱麻。一看就是新手写的。
      

  8.   

    请问我每次都 convertView = mInflater.inflate(R.layout.validate01_cell, null)是获取到了对象啊,convertView不可能为null,返回的怎么会是null呢?
      

  9.   

    能不能解释一下parent和false这两个参数的作用,谢谢!
      

  10.   

     @Override
        public Object getItem(int pos) {
            // TODO Auto-generated method stub
            return null;
        }改成 @Override
        public Object getItem(int pos) {
            return datas[pos];
        }
      

  11.   

    在GridView布局中各个格子的元素不同,如下图:在使用BaseAdapter时,怎么进行优化?滑动时怎么保证内存不泄露?
    如果使用下面这种方式好像不行: if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.list_item_icon_text,
                            null);
                    holder = new ViewHolder();    
                    holder.text1 = (TextView) convertView.findViewById(R.id.text1);
                    holder.icon2 = (ImageView) convertView.findViewById(R.id.icon2);
                    convertView.setTag(holder);
                }
                else{
                    holder = (ViewHolder)convertView.getTag();
                }有没有人遇到过这个问题,麻烦解决一下