The application of the ListView in the Simulation Micro-blog
Use the ListView to do a mock-up microblogging (--------------should be done by teacher
The job requirements are implemented as follows:
Here are the effects of my own program:
Analyze the problem:
Use of 1.ListView and Listview_item
2. Entity classes
3.ListView Writing of custom adapters
4.ListView of bound data sources and controls
5. inheritance: Baseadapter
The following is my own code: (Custom adapter)
1 Package Cn.edu.bzu.adapter;2 3 import java.util.List;4 5 import cn.edu.bzu.entity.Information;6 import cn.edu.bzu.weibo_looking. R;7 import Android.content.Context;8 import Android.view.LayoutInflater;9 import Android.view.View;Ten import Android.view.ViewGroup; One import Android.widget.BaseAdapter; A import Android.widget.ImageView; - import Android.widget.TextView; - the /** - * Custom Adapter to implement ListView display - * @author Monster - * @date: 2015-04-28 + */ - Public classMyadapter extends Baseadapter { + PrivateList<information>list; A PrivateContext Mcontext; at PrivateLayoutinflater Minflater; - //Defining construction Methods - PublicMyadapter (list<information>list, Context mcontext) { - super (); - This. List =list; - This. Mcontext =Mcontext; in } - to @Override + Public intGetCount () { - returnlist.size (); the } * $ @OverridePanax Notoginseng PublicObject GetItem (intposition) { - returnList.Get(position); the } + A @Override the Public LongGetitemid (intposition) { + returnposition; - } $ $ @Override - PublicView GetView (intposition, View Convertview, ViewGroup parent) { -Viewholder holder=NULL; the if(convertview==NULL){ -Convertview=minflater. from(Mcontext). Inflate (R.layout.listview_item,NULL);//layout into viewWuyiHolder=NewViewholder (); the //instantiating a control -Holder. Image=(ImageView) Convertview.findviewbyid (R.ID.IV); WuHolder. Name=(TextView) Convertview.findviewbyid (r.id.name); -Holder. Time=(TextView) Convertview.findviewbyid (r.id.time); AboutHolder. contents=(TextView) Convertview.findviewbyid (r.id.contents); $Convertview.settag (holder);//setting Settag to optimize programs -}Else{ -Holder=(Viewholder) Convertview.gettag (); - } A //Assigning a value to a control +Holder. Image.setbackgroundresource (list.Get(position). Getimgid ()); theHolder. Name.settext (list.Get(position). GetName ()); -Holder. Time.settext (list.Get(position). GetDate ()); $Holder. Contents.settext (list.Get(position). GetContent ()); the returnConvertview; the } the the classviewholder{ - ImageView Image; in TextView Name; the TextView time; the TextView Contents; About } the}
Analyze the problem:
In the above custom adapter Myadapter code, the GetView method is the most important, but also requires methods such as Getcount,getitem,getitemid.
The following is the Mainactivity code:
1 Package cn.edu.bzu.weibo_looking;2 3 import java.util.ArrayList;4 import java.util.List;5 6 import Cn.edu.bzu.adapter.MyAdapter;7 import cn.edu.bzu.entity.Information;8 import Android.os.Bundle;9 import Android.widget.ListView;Ten import android.app.Activity; One A Public classMainactivity extends Activity { - PrivateListView ListView; - PrivateMyadapter Adapter; the PrivateList<information>list; - @Override - protected voidonCreate (Bundle savedinstancestate) { - super.oncreate (savedinstancestate); + Setcontentview (r.layout.activity_main); -SetData ();//Data Source + Initview (); A } at Private voidInitview () { - //Initializing the control -listview=(ListView) Findviewbyid (r.id.lv); -Listview.setadapter (adapter);//Binding Adapter - } - Private voidSetData () { in //set up a data source -list=NewArraylist<information>(); toList.add (NewInformation (R.DRAWABLE.IMG1,"Wang","2015-05-08","Moonset Wu Ti Frost all over the sky")); +List.add (NewInformation (R.DRAWABLE.IMG2,"Dufu","2015-05-08","Jiangfeng Isaribi to worry about sleep")); -List.add (NewInformation (R.DRAWABLE.IMG3,"Libai","2015-05-08","Gusu City outside Hanshan Temple")); theList.add (NewInformation (R.DRAWABLE.IMG4,"Hades","2015-05-08","Midnight Bell to the passenger ship")); *Adapter=NewMyadapter (list, mainactivity. This); $ } Panax Notoginseng}
Analyze the problem:
1. Establishment and setting of data sources
2. Control use (Initialize)
3. Bindings for adapters
Simulate Weibo to see the application of--listview