zoukankan      html  css  js  c++  java
  • 用ListView实现对数据库的内容显示

    用ListView实现对数据库的内容显示

    1. 创建一个触发机制 ---------(作用)将数据读入ArrayList集合中

    MyBase base = new MyBase();

           SQLiteDatabase db = mySQLhelpes.getReadableDatabase();

           Cursor cursor = db.query("inof", null, null, null, null, null, null);

           list = new ArrayList<Fuwu>();

           while(cursor.moveToNext()){   //将数据写入ArrayList中

           Fuwu fuwu = new Fuwu();

               String name =  cursor.getString(cursor.getColumnIndex("name") );

               String num = cursor.getString(cursor.getColumnIndex("number"));

               fuwu.setName(name);

               fuwu.setNum(num);

               list.add(fuwu);

               fuwu  = null;

           }

           db.close();

           MyBase myBase = new MyBase();  //加载适配器

           myBase.notifyDataSetChanged();//刷新适配器

        lv.setAdapter(myBase);

    1. 定义一个ListView-----(作用)把数据从集合中放入ListView中
    2. 找到ListView
    3. 定义一个复杂的BaseAdapeter

    a)      实现方法

    public int getCount() //返回数据的个数

    public View getView(int arg0, View arg1, ViewGroup arg2) //返回view值

    View view = View.inflate(MainActivity.this, R.layout.item, null); //将xml转化为view

        TextView tv_name =  (TextView) view.findViewById(R.id.item_text_1);//得到子孩子

        TextView tv_num =  (TextView)view.findViewById(R.id.item_text_2);

         tv_name.setText(list.get(arg0).getName());//数据载入

         tv_num.setText(list.get(arg0).getNum());

    1. 给ListView设置Adapter
  • 相关阅读:
    C++笔记-智能指针 shared_ptr
    Linux笔记-性能调优工具perf
    git submodule 如何push代码
    性能测试工具gperftools使用
    Linux信号使用及自定义信号
    DNN在推荐系统中的应用参考资料
    vscode远程代码同步
    感知机模型到DNN模型
    c++笔记-libcurl多线程并发时的core【转载】
    go笔记-熔断器
  • 原文地址:https://www.cnblogs.com/tangwanzun/p/5702271.html
Copyright © 2011-2022 走看看