0

我想从 ormlite 返回的 arraylist 填充列表视图

这是我从 ormlite 检索数据的代码

我如何用我的表的所有 colymns 填充列表视图

public class dinnerList extends Activity {
ArrayList<CanteenTagEntry> dinnerlist;
ListView lv;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview_item_row);

    Context con = getApplicationContext();
    DatabaseHelper dbHelper = new DatabaseHelper(con);
    ICanteenLogRepository canteenrepo = dbHelper.getCanteenLogRepository();
    try {
        dinnerlist = (ArrayList<CanteenTagEntry>) canteenrepo.Retrieve();
        System.out.print(dinnerlist);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    lv = (ListView) findViewById(R.id.list);
    lv.setAdapter(new CanteenAdapter(this, dinnerlist));

我的适配器类在下面

public class CanteenAdapter extends BaseAdapter {
    private Context context;
    private List<CanteenTagEntry> lis;

    public CanteenAdapter(Context c, List<CanteenTagEntry> li) {
        // TODO Auto-generated method stub
        context = c;
        lis = li;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return lis.size();
    }

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = inflater
                    .inflate(R.layout.listview_item_row, null);
        }

        TextView textview1 = (TextView) convertView
                .findViewById(R.id.Title1);
        TextView textview2 = (TextView) convertView
                .findViewById(R.id.Title2);
        TextView textview3 = (TextView) convertView
                .findViewById(R.id.Title3);
        TextView textview4 = (TextView) convertView
                .findViewById(R.id.Title4);

        return convertView;

    }

}

提前谢谢

4

1 回答 1

2

您必须在适配器 getView() 方法中从列表中获取这些值

CanteenTagEntry entry = new CanteenTagEntry();
entry = lis.get(position);

看看这个教程自定义适配器列表视图它会解决你的问题..

于 2012-08-23T05:07:50.273 回答