使用自定义类的ListView填充 [英] ListView Populating using Custom Class

查看:124
本文介绍了使用自定义类的ListView填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写$ C $下一个Android应用程序。我有一类叫做类,其中有一个ID和名称。我已经保存了数据转换为SQLite数据库,当我读了所有的行从数据库中,我已经将它们保存到一个列表类型列表。

I am writing code for an Android application. I have a class called Categories, which has an ID and a name. I have saved the data into a SQLite Database and when I read all rows from the database, i have saved them into a List type list.

现在,我该如何填充列表视图显示我的身份证和类别上的列表视图。

Now, how do I populate the listview to show my ID and category on the Listview.

P.S。我有一个线性布局一个列表视图和有在顶部标题部分

P.S. I have a Listview in a Linear Layout and there is a header section on the top.

推荐答案

使用下面ListAdapter您的ListView
ListAdapter.java

use below ListAdapter for your listView
ListAdapter.java

public class ListAdapter extends BaseAdapter {


private LayoutInflater myInflater;
private List<YOURDATATYPE> list;

public ListAdapter(Context context) {
    myInflater = LayoutInflater.from(context);

}

public void setData(List<YOURDATATYPE> list) {
    this.list = list;


}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;        

    convertView     = myInflater.inflate(R.layout.row, null);
    holder          = new ViewHolder();
    holder.id   = (TextView) convertView.findViewById(R.id.id);
    holder.category     = (TextView) convertView.findViewById(R.id.category);

    convertView.setTag(holder);


    holder.id.setText(list.get(position).id);
    holder.category.setText(list.get(position).category);



    return convertView;
}

static class ViewHolder {
    TextView id;
    TextView category ;

}

}

在OnCreate中的ListActivity()

in your ListActivity in OnCreate()

adapter   = new ListAdapter();

listView = getListView();
adapter.setData(YOUR LIST FROM DATABASE);
listView.setAdapter(adapter);

这篇关于使用自定义类的ListView填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆