将Android sql元素添加到listview

将Android sql元素添加到listview,android,sql,listview,Android,Sql,Listview,我在sql数据库中有一个表,其中包含值:id、name、age、weight,并可以将它们放入一个值列表中。 列表应如下所示: 名称 年龄:体重: 我怎样才能做到这一点?我已经试着把名单传给ArrayAdapter,但我不知道具体怎么做。当我使用此选项时: ArrayAdapter adapter=新的ArrayAdapter(这个,R.layout.list\u示例\u条目,值) 我得到的android.widget.FrameLayout无法转换为android.widget.TextVie

我在sql数据库中有一个表,其中包含值:id、name、age、weight,并可以将它们放入一个值列表中。 列表应如下所示: 名称 年龄:体重:

我怎样才能做到这一点?我已经试着把名单传给ArrayAdapter,但我不知道具体怎么做。当我使用此选项时:
ArrayAdapter adapter=新的ArrayAdapter(这个,R.layout.list\u示例\u条目,值)

我得到的
android.widget.FrameLayout无法转换为android.widget.TextView
错误。

为此,您需要一个
光标或适配器。这里有一个很好的指南:

要实现这一点,您必须构建一个自定义适配器并为自定义行布局充气。使用ArrayAdapter不起作用

因此,您的自定义适配器类可能类似于:

    public class CustomAdapter extends BaseAdapter {
        private final Activity activity;
        private final List list;

        public CustomAdapter(Activity activity, ArrayList<Person> list) {
            this.activity = activity;
            this.list = list;
        }

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

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

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

            if(rowView == null)
            {
                // Get a new instance of the row layout view
                LayoutInflater inflater = activity.getLayoutInflater();
                rowView = inflater.inflate(R.layout.rowlayout, null);

                // Hold the view objects in an object, that way the don't need to be "re-  finded"
                view = new ViewHolder();
                view.person_name= (TextView) rowView.findViewById(R.id.name);
                view.person_address= (TextView) rowView.findViewById(R.id.address);

                rowView.setTag(view);
            } else {
                view = (ViewHolder) rowView.getTag();
            }

            /** Set data to your Views. */
            Person item = list.get(position);
            view.person_name.setText(item.getTickerSymbol());
            view.person_address.setText(item.getQuote().toString());

            return rowView;
        }

        protected static class ViewHolder{
            protected TextView person_name;
            protected TextView person_address;
        }
    }
现在,在您的主要活动中,只需使用一些数据绑定您的列表,如

/** Declare and initialize list of Person. */
ArrayList<Person> list = new ArrayList<Person>();

/** Add some restaurants to the list. */
list.add(new Person("name1", "address1"));
list.add(new Person("name2", "address2"));
list.add(new Person("name3", "address3"));
list.add(new Person("name4", "address4"));
list.add(new Person("name5", "address5"));
list.add(new Person("name6", "address6"));
At this point you're able to set the custom adapter to your list

ListView lv = (ListView) findViewById(R.id.mylist);

CustomAdapter adapter = new CustomAdapter(YourMainActivityName.this, list);
lv.setAdapter(adapter);
/**声明并初始化人员列表*/
ArrayList=新建ArrayList();
/**将一些餐馆添加到列表中*/
列表。添加(新人员(“姓名1”、“地址1”);
列表。添加(新人员(“姓名2”、“地址2”);
添加(新人员(“姓名3”、“地址3”);
添加(新人员(“姓名4”、“地址4”);
添加(新人员(“姓名5”、“地址5”);
列表。添加(新人员(“姓名6”、“地址6”);
此时,您可以将自定义适配器设置为您的列表
ListView lv=(ListView)findViewById(R.id.mylist);
CustomAdapter=新的CustomAdapter(YourMainActivityName.this,列表);
低压设置适配器(适配器);

可能是一个愚蠢的错误,但我甚至不能创建
公共CustomAdapter(Activity-Activity,ArrayList-list){this.Activity=Activity;this.list=list;}
它说ArrayAdaptError中没有可用的默认构造函数:(23,73)错误:找不到适合ArrayAdapter的构造函数尝试使用BaseAdapter更改ArrayAdapter,但我必须将其声明为抽象的,或者在“Adapter”中实现抽象方法“getItemId(int)”,对此我一无所知
/** Declare and initialize list of Person. */
ArrayList<Person> list = new ArrayList<Person>();

/** Add some restaurants to the list. */
list.add(new Person("name1", "address1"));
list.add(new Person("name2", "address2"));
list.add(new Person("name3", "address3"));
list.add(new Person("name4", "address4"));
list.add(new Person("name5", "address5"));
list.add(new Person("name6", "address6"));
At this point you're able to set the custom adapter to your list

ListView lv = (ListView) findViewById(R.id.mylist);

CustomAdapter adapter = new CustomAdapter(YourMainActivityName.this, list);
lv.setAdapter(adapter);