欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android开发教程之listview显示sqlite数据

程序员文章站 2023-08-29 20:22:01
复制代码 代码如下:package com.it.db; import java.util.list;import com.it.dao.persondao;import...

复制代码 代码如下:

package com.it.db;

import java.util.list;
import com.it.dao.persondao;
import com.it.domain.person;
import android.os.bundle;
import android.app.activity;
import android.content.intent;
import android.view.view;
import android.view.viewgroup;
import android.widget.adapterview;
import android.widget.adapterview.onitemclicklistener;
import android.widget.baseadapter;
import android.widget.listview;
import android.widget.textview;

public class mainactivity extends activity {
 private listview lv;
 private list<person> persons;
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);

  persondao dao=new persondao(this);
  persons=dao.findall();
  lv=(listview) findviewbyid(r.id.lv);
  lv.setadapter(new myadapter());

  lv.setonitemclicklistener(new onitemclicklistener() {
   //点击的数据 传到上一个activity
   @override
   public void onitemclick(adapterview<?> parent, view view,
     int position, long id) {
    // todo auto-generated method stub
    person mperson=persons.get(position);
    string number=mperson.getnumber();
    intent data=new intent();
    data.putextra("number", number);
    setresult(0, data);
    //点击 即关闭此activity
    finish();
   }
  });
 }

 private class myadapter extends baseadapter{
  private static final string tag = "myadapter";

  /**
   * 控制listview里有多少个条目
   */
  @override
  public int getcount() {
   // todo auto-generated method stub
   return persons.size();
  }

  @override
  public object getitem(int position) {
   // todo auto-generated method stub
   return null;
  }

  @override
  public long getitemid(int position) {
   // todo auto-generated method stub
   return 0;
  }

  @override
  public view getview(int position, view convertview, viewgroup parent) {
   // todo auto-generated method stub
   /**
    * 采用inflater显示
    */
   person person=persons.get(position);
   view view=view.inflate(mainactivity.this, r.layout.list_item, null);

   textview tv_id=(textview) view.findviewbyid(r.id.tv_id);
   tv_id.settext("id:"+person.getid());

   textview tv_name=(textview) view.findviewbyid(r.id.tv_name);
   tv_name.settext("姓名:"+person.getname());

   textview tv_number=(textview) view.findviewbyid(r.id.tv_number);
   tv_number.settext("电话:"+person.getnumber());

   return view;
   }
   /**
    * 不用inflater
    *
   log.i(tag, "位置"+position);   //看效果
   textview tv=new textview(getapplicationcontext());
   tv.settextsize(20);
   tv.settextcolor(color.black);
   //每个位置上的条目
   person person=persons.get(position);
   tv.settext(person.tostring());
   return tv;
  }*/

 }
}