public class HotelListAdapter extends BaseAdapter
{
LayoutInflater inflater;
HotelInfo a[] = new HotelInfo[5];
public HotelListAdapter(Activity context, HotelInfo[] b)
{
super();
this.a=b;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount()
{
return 5; //currently hardcoded to 5
}
@Override
public Object getItem(int arg0)
{
return null;
}
@Override
public long getItemId(int arg0)
{
return 0;
}
@Override
public View getView(int position, View convertview, ViewGroup parent)
{
View vi=convertview;
if(convertview==null)
{
vi=inflater.inflate(R.layout.list_row,null);
TextView name = (TextView)vi.findViewById(R.id.Hname);
TextView stname = (TextView)vi.findViewById(R.id.Stname);
name.setText(a[position].HHname);
stname.setText(a[position].STname);
position++; // how to loop till 5 or x number.... ?
}
return vi;
}
}
这是我用于填充列表的适配器。该列表应该显示一个 MAINTEXT 和一个小的 SUBTEXT。我需要用一个对象数组填充列表(当前包含 5 个对象的数组,但想知道数组的“x”长度)!该对象仅包含两个字符串字段。请帮助提供代码。谢谢。
public class ListHotels extends ListActivity
{
HotelInfo[] b = new HotelInfo[5];
void HotelInfo()
{
b[0].setvalues("AAA","xxx");
b[1].setvalues("BBB","yyy");
b[2].setvalues("CCC","zzz");
b[3].setvalues("DDD","www");
b[4].setvalues("EEE","ppp");
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.listhotels);
HotelListAdapter adapter = new HotelListAdapter(this,b);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
}
}
这是ListActivity。目前代码中没有错误。