Android Development Tutorials ListView Display SQLite data _android

Source: Internet
Author: User
Tags sqlite stub

Copy Code code as follows:

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 () {
Click on the data to upload to the previous 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);
Click to close this activity
Finish ();
}
});
}

Private class Myadapter extends baseadapter{
private static final String TAG = "Myadapter";

/**
* Control how many entries are in the 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
/**
* Using Inflater Display
*/
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 ("Name:" +person.getname ());

TextView tv_number= (TextView) View.findviewbyid (R.id.tv_number);
Tv_number.settext ("Tel:" +person.getnumber ());

return view;
}
/**
* No Inflater
*
LOG.I (tag, "position" +position); Look at the effect
TextView tv=new TextView (Getapplicationcontext ());
Tv.settextsize (20);
Tv.settextcolor (Color.Black);
Entries at each location
Person Person=persons.get (position);
Tv.settext (Person.tostring ());
return TV;
}*/

}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.