0
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。目前代码中没有错误。

4

3 回答 3

0

您不需要放置position++,位置将根据列表中列表项的数量自动增加。所以改变return 5;_getCount()return a.length;

通过这种方式,您可以针对动态大小的列表对代码进行未来验证。

@Override
public int getCount() 
{       
    return a.length;
}
于 2013-09-06T12:15:37.757 回答
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;
}
于 2013-09-06T12:16:48.050 回答
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;
    }

}
于 2013-09-06T12:18:51.217 回答