Android App Development Basics (3)-----ListView

Source: Internet
Author: User

Link Address: http://www.cnblogs.com/lknlfy/archive/2012/02/16/2354625.html

I. Overview

ListView is a list display control, its application is very extensive, in many applications can see its figure, such as call-through, NetEase news and so on, especially QQ. So it is very necessary to master it.

Second, the requirements

The ability to use the ListView to display some content, such as data, text, a row is clicked when there is a corresponding hint, the ability to implement a simple ListView dynamic insertion and deletion.

Third, the realization

New Project Mylistview, modify the/res/layout/main.xml file, mainly to remove the original TextView, add two buttons and a ListView, the modified Main.xml file as follows:

1 <?xml version= "1.0" encoding= "Utf-8"?>
2 <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
3 android:layout_width= "Fill_parent"
4 android:layout_height= "Fill_parent"
5 android:orientation= "Vertical" >
6
7 <button
8 android:id= "@+id/tbutton"
9 android:layout_width= "Fill_parent"
Ten android:layout_height= "Wrap_content"
android:text= "Add a row"
Android:textsize= "15px"
/>
14
<button
Android:id= "@+id/sbutton"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Delete one row"
Android:textsize= "15px"
/>
22
<listview
Android:id= "@+id/listview"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
27
>
</ListView>
30
</LinearLayout>

Under/SRC, create a file named Myadapter.java, where you create a Myadapter class that inherits from the Baseadapter class, writes a constructor with a parameter in the class, and a custom function setnumlist (int n). Complete Myadapter.java Files:

1 package Com.nan.listview;
2
3 Import Android.content.Context;
4 Import Android.graphics.Color;
5 Import Android.view.LayoutInflater;
6 Import Android.view.View;
7 Import Android.view.ViewGroup;
8 Import Android.widget.BaseAdapter;
9 Import Android.widget.TextView;
10
public class Myadapter extends Baseadapter
12 {
Private Context Mcontext;
+ private int num;
15
Myadapter (Context c)
17 {
18//Accept the context of the Mylistviewactivity class
Mcontext = C;
20}
21st
public void setnumlist (int n)
23 {
24//Set how many rows to display
num = n;
26}
27
@Override
public int GetCount ()
30 {
//TODO auto-generated method stub
32//Return number of rows
return num;
34}
35
@Override
PNS public Object getItem (int arg0) {
A//TODO auto-generated method stub
ARG0 return;
40}
41
@Override
Getitemid public Long (int position) {
/TODO auto-generated Method stub
Position return;
46}
47
@Override
Public View GetView (int position, view Convertview, ViewGroup parent)
50 {
Wuyi//TODO auto-generated method stub
52//Test.xml instantiation with layout file Convertview
Convertview = Layoutinflater.from (Mcontext). Inflate (r.layout.test, NULL);
54//Get a TextView in the Test.xml file
TextView Mtextview = (TextView) Convertview.findviewbyid (R.id.titletextview);
56//Set its text color to red
Mtextview.settextcolor (color.red);
58//Set its text content
Mtextview.settext (Mtextview.gettext () +integer.tostring (position));
60
Convertview return;
62}
63}

Where Test.xml is created by itself, the file is a layout file for each row in the ListView to display, where I only have 2 TextView text layout and, of course, other controls such as Button,imagebutton,checkbox. The complete Test.xml file is as follows:

1 <?xml version= "1.0" encoding= "Utf-8"?>
2 <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
3 android:layout_width= "Fill_parent"
4 android:layout_height= "Fill_parent"
5 android:orientation= "vertical" >


8 <textview
9 android:id= "@+id/titletextview"
Ten android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "title"
/>

<textview
android:id= "@+id/datatextview"
android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Content"
/>

</LinearLayout>

Finally is the Mylistviewactivity.java file, in the file set 2 buttons and Mlistview is clicked on the listener, more understandable, the complete content is as follows:

1 package Com.nan.listview;
2
3 Import android.app.Activity;
4 Import Android.os.Bundle;
5 Import Android.view.View;
6 Import Android.widget.AdapterView;
7 Import Android.widget.Button;
8 Import Android.widget.ListView;
9 Import Android.widget.Toast;
10
11
public class Mylistviewactivity extends Activity
13 {
+ private ListView Mlistview = null;
The private Button Mbutton = null;
Private Button Sbutton = null;
17//Define an object with a class of your own definition
Private Myadapter Mmyadapter;
19
the private int numlists;
21st
/** called when the activity is first created. */
@Override
public void OnCreate (Bundle savedinstancestate)
25 {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
28
Mlistview = (ListView) Findviewbyid (R.id.listview);
Mbutton = (Button) Findviewbyid (R.id.tbutton);
Sbutton = (Button) Findviewbyid (R.id.sbutton);
32//Instantiate Mmyadapter Object
Mmyadapter = new Myadapter (mylistviewactivity.this);
34//Initialize the number of rows to display as 5 rows
Numlists = 5;
Mmyadapter.setnumlist (numlists);
37//Add the content to be displayed for Mlistview
Mlistview.setadapter (Mmyadapter);
39
40//Set Mlistview each line of listening, when you click on one of the rows will be prompted to click on the first few lines
Mlistview.setonitemclicklistener (New Adapterview.onitemclicklistener ()
42 {
43
@Override
public void Onitemclick (adapterview<?> arg0, View arg1, int arg2,
(Arg3 Long)
47 {
//TODO auto-generated method stub
49//Show the number of clicks on the first line
Displaytoast (integer.tostring (arg2));
51}
52});
53
54//Set the monitor to add line button
Mbutton.setonclicklistener (New View.onclicklistener ()
56 {
57
@Override
The public void OnClick (View v)
60 {
I//TODO auto-generated method stub
62
63//number of rows plus 1
Numlists + = 1;
Mmyadapter.setnumlist (numlists);
66//Refresh Mlistview
Mlistview.setadapter (Mmyadapter);
68}
69});
70
71//Set the Delete line button to listen
Sbutton.setonclicklistener (New View.onclicklistener ()
73 {
74
@Override
public void OnClick (View v)
77 {
//TODO auto-generated method stub
79//Line number minus 1
Numlists-= 1;
Bayi if (numlists = = 0)
82 {
numlists = 1;
Displaytoast ("Can not be deleted again!!! ");
85}
Mmyadapter.setnumlist (numlists);
87//Refresh Mlistview
Mlistview.setadapter (Mmyadapter);
89}
90});
91
92}
93
94//Display Prompt function
Displaytoast private void (String s)
96 {
The Toast.maketext toast toast = (Mylistviewactivity.this, S, Toast.length_short);
98 toast.show ();
99}
100
101}

Run this program as shown in:

Click on the "Add a row" button, you can see more than one row, the effect is as follows:

Then click on the "Delete one row" button under two , as follows:

Next, click on line 1th (calculated from line No. 0) and you will see a prompt, as follows:

OK, it's done.

Above these are relatively simple operation, the actual application needs to use the database and so on, because this is the basic content, so do not intend to tell here, later in the high-level articles, examples of the detailed explanation.

Tags: android development

Android App Development Basics (3)-----ListView

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.