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;
}*/
}
}