Listview未在模拟器中显示

时间:2012-03-28 19:59:36

标签: android

嗨,所有对Android开发都很陌生并且我的自定义列表视图面临问题... listview没有在输出中显示...这是我的代码请帮助我...

public class CustomListFilterBaseAdapterActivity extends Activity {
int[] pincodes;

//@Override
String[] areas, districts, states;
ListViewAdapterContacts lva;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
ListView lv=(ListView)findViewById(R.id.listview);

lva = new ListViewAdapterContacts(this, pincodes, areas, districts, states);

lv.setAdapter(lva);
lv.setTextFilterEnabled(true);
}

public class ListViewAdapterContacts extends BaseAdapter{
Activity context;   
int[] pincodes={675755,757576,675766,8788686,876868,7686686,987977};
String[] areas={"ahdjhhd","sadahh","ahdhakshd","shdfkhkh","ahdkhakhd","ajsdghsd","asgdagdk"};
String[] districts={"warangal","adilabad","hyderabad","krishna","vizag","kakinada","karimnagar"};
String[] states={"Ap","orissa","kerala","gujarath","pune","banglore","thamilnadu"};

public ListViewAdapterContacts(Activity context, int[] pincodes, String[] areas, String[] districts, String[] states) {
    // TODO Auto-generated constructor stub

this.context=context;
this.pincodes=pincodes;
this.areas=areas;
this.districts=districts;
this.states=states;
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    if(pincodes==null){
        return 0;
    }
    else{
        return pincodes.length;
    }
    }

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

 public class viewHolder {
        TextView top;
        TextView bottom;
        TextView downside;
        TextView base;
    }

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
     viewHolder holder;
        if(convertView==null){

        LayoutInflater inflator=context.getLayoutInflater();
        convertView=inflator.inflate(R.layout.textviews,null);

        holder=new viewHolder();
        holder.top=(TextView)convertView.findViewById(R.id.textview1);
        holder.bottom=(TextView)convertView.findViewById(R.id.textview2);
        holder.downside=(TextView)convertView.findViewById(R.id.textview3);
        holder.base=(TextView)convertView.findViewById(R.id.textview4);
        convertView.setTag(holder);
        }else{
            holder=(viewHolder)convertView.getTag();
        }
        holder.top.setText(pincodes[position]);
        holder.bottom.setText(areas[position]);
        holder.downside.setText(districts[position]);
        holder.base.setText(states[position]);

        View v=(View)convertView;
        //return convertView;
        return v; 
}
  }
}

1 个答案:

答案 0 :(得分:0)

您的代码所需的一些mino更改会查看我的代码并使用此

更改您的代码
    public View getView(int position, View v, ViewGroup parent){
            ViewHolder holder;
        View convertView = v;
        holder = new ViewHolder();

            if (convertView == null) {
                 holder.top=(TextView)convertView.findViewById(R.id.textview1);
            holder.bottom=(TextView)convertView.findViewById(R.id.textview2);
            holder.downside=(TextView)convertView.findViewById(R.id.textview3);
            holder.base=(TextView)convertView.findViewById(R.id.textview4);
            convertView.setTag(holder);

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

并且在getCount()中只放了这个

return pincodes.length;