How the ListView is written

Source: Internet
Author: User

The most important thing to write a ListView is to write the adapter adapter,

Adapter to complete the following work

1, decide to use the child view of the ListView

2, use the list to display data to the ListView

3, overriding the GetView method, setting the content for the control of the child view

Also be prepared to write sub-view and display the data class, such as the ListView display is the fruit of information, then write a fruit class, picture name, etc., define setter getter

Initialize a few fruit classes in activity and load them into a list, then use adapter to

The code is as follows:

I'm going to write a ListView to show the prizes, the name of the prize, the name of the director, and the number of the director's work in each sub-view.

First write this item view to define four TextView.

Show_bonus_item.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Horizontal" >
<textview
Android:id= "@+id/bonus_category_text"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
<textview
Android:id= "@+id/director_name"
Android:layout_width= "0DP"
android:layout_weight= "1"
android:layout_height= "Wrap_content"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Number of works:"/>
<textview
Android:id= "@+id/bonus_count"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
</LinearLayout>

Then write three TextView display the name of the class, the third TextView because it is fixed display "number of works" so no additional processing

public class Showbonus {
Private String bonuscatrgory;
Private String Directorname;
private int moviescount;

Public String getbonuscatrgory () {
return bonuscatrgory;
}

public void Setbonuscatrgory (String bonuscatrgory) {
This.bonuscatrgory = bonuscatrgory;
}

Public String Getdirectorname () {
return directorname;
}

public void Setdirectorname (String directorname) {
This.directorname = Directorname;
}

public int Getmoviescount () {
return moviescount;
}

public void Setmoviescount (int moviescount) {
This.moviescount = Moviescount;
}
}

Write a adapter again, overriding the GetView method
public class Showbonusadapter extends arrayadapter<showbonus>{
private int ResourceID;
Public Showbonusadapter (context context, int resource, int textviewresourceid, list<showbonus> objects) {
Super (context, resource, Textviewresourceid, objects);
ResourceID = Textviewresourceid;
}
Overriding the GetView method

@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
Gets the current Showbonus instance
Showbonus Showbonus = GetItem (position);
Set content for each control of a child view
View view = Layoutinflater.from (GetContext ()). Inflate (Resourceid,null);
TextView bonuscatrgory = (TextView) View.findviewbyid (R.id.bonus_category_text);
Bonuscatrgory.settext (Showbonus.getbonuscatrgory ());
TextView directorname = (TextView) View.findviewbyid (r.id.director_name);
Directorname.settext (Showbonus.getdirectorname ());
TextView Bonuscount = (TextView) View.findviewbyid (R.id.bonus_count);
Bonuscount.settext ("" +showbonus.getmoviescount ());
return view;
}
}
Then you initialize several Showbonus instances in the activity, load them into a list, and then use the adapter, because I use fragment, which is basically the same as the activity. I used a view.findviewbyid when I was Findviewbyid.
public class Fragmentshowbonus extends Fragment {
Private context context;
Private list<showbonus> Listshowbonus = new arraylist<showbonus> ();
Private ListView Showbonuslistview;
Public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
View view = Inflater.inflate (R.layout.show_bonus_layout, container, false);
Initbonus ();
Showbonusadapter showbonusadapter = new Showbonusadapter
(This.getactivity (), R.layout.show_bous_item, Listshowbonus);
Showbonuslistview = (ListView) View.findviewbyid (R.id.bonus_listview);
Showbonuslistview.setadapter (Showbonusadapter);
return view;
}

Instantiate several Showbonus objects
private void Initbonus () {
Showbonus ShowBonus1 = new Showbonus ("1, Best Micro Film director award", "Ang Lee", 20);
Showbonus showBonus2 = new Showbonus ("2, Best Drama Director award", "Zhang Yimou", 16);
Showbonus SHOWBONUS3 = new Showbonus ("3, Best Documentary director award", "Jia Zhang Ke", 19);
Showbonus SHOWBONUS4 = new Showbonus ("4, Best children's Play director award", "Feng Xiaogang", 13);
Showbonus SHOWBONUS5 = new Showbonus ("5, Best Drama Director award", "Chen Kai", 23);
Showbonus SHOWBONUS6 = new Showbonus ("6, best corporate Advocacy film director Award", "Wang Jia Wei", 25);
Showbonus SHOWBONUS7 = new Showbonus ("7, best product film director award", "Ang Lee", 20);
Showbonus SHOWBONUS8 = new Showbonus ("7, Best Party Director Award", "Spielberg", 13);
Listshowbonus.add (SHOWBONUS1);
Listshowbonus.add (SHOWBONUS2);
Listshowbonus.add (SHOWBONUS3);
Listshowbonus.add (SHOWBONUS4);
Listshowbonus.add (SHOWBONUS5);
Listshowbonus.add (SHOWBONUS6);
Listshowbonus.add (SHOWBONUS7);
Listshowbonus.add (SHOWBONUS8);

}
}

How the ListView is written

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.