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'长度的数组)!该对象仅包含两个String字段。请帮助代码。感谢名单。
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。目前代码中没有错误。
答案 0 :(得分:0)
您无需放置position++
,该位置将根据列表中的列表项数自动递增。因此,请将return 5;
中的getCount()
更改为return a.length;
通过这种方式,您可以使用动态大小的列表来验证您的代码。
@Override
public int getCount()
{
return a.length;
}
答案 1 :(得分:0)
将这两种方法更改为:
@Override
public int getCount()
{
return a.length;
}
@Override
public Object getItem(int arg0)
{
return a[arg0];
}
然后将getView更改为:
@Override
public View getView(int position, View convertview, ViewGroup parent)
{
if(convertview == null)
convertview = inflater.inflate(R.layout.list_row,null);
HotelInfo currentInfo = (HotelInfo)getItem(position);
TextView name = (TextView)convertview.findViewById(R.id.Hname);
TextView stname = (TextView)convertview.findViewById(R.id.Stname);
name.setText(currentInfo.HHname);
stname.setText(currentInfo.STname);
return convertview;
}
答案 2 :(得分:0)
试试这个..
public class HotelListAdapter extends BaseAdapter
{
LayoutInflater inflater;
HotelInfo a[] = new HotelInfo[5];
private class ViewHolder {
public TextView name;
public TextView stname;
}
public HotelListAdapter(Activity context, HotelInfo[] b)
{
super();
this.a=b;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount()
{
return a.length;
}
@Override
public Object getItem(int arg0)
{
return arg0;
}
@Override
public long getItemId(int arg0)
{
return arg0;
}
@Override
public View getView(int position, View convertview, ViewGroup parent)
{
View vi=convertview;
final ViewHolder holder;
if(convertview==null)
{
vi=inflater.inflate(R.layout.list_row,null);
holder = new ViewHolder(); //change is here..
holder.name = (TextView)vi.findViewById(R.id.Hname);
holder.stname = (TextView)vi.findViewById(R.id.Stname);
vi.setTag(holder);
}else {
holder = (ViewHolder) vi.getTag();
}
holder.name.setText(a[position].HHname);
holder.stname.setText(a[position].STname);
return vi;
}
}