Android Development Tutorial Using ListView display QQ Contact List _android

Source: Internet
Author: User
Tags stub

First or the XML layout file, where you add the ListView control:

Master Layout Layout_main.xml

Copy Code code as follows:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Android:background= "#00aaff"
Tools:context= ". Mainactivity ">

<textview
Android:id= "@+id/mytext"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:text= "Contacts"
Android:textsize= "7pt"
Android:layout_centerhorizontal= "true"
Android:textcolor= "#ffffff"
android:textstyle= "Bold"/>
<listview
Android:id= "@+id/qq_list"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:layout_below= "@id/mytext"/>
</RelativeLayout>

Then the layout of each line ListItem, with Linerlayout layout, some points of attention are inside:

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:background= "#efefef" >
<!--Linerlayout has a strange property: When the layout of the control can exceed the size of the layout, so here's a line of ruled changed by several internal controls
Control, and Linerlayout's layout_height changed into Wrap_content. -->
<imagebutton
Android:id= "@+id/ct_photo"
android:layout_height= "70dip"
Android:layout_width= "70dip"
Android:layout_margin= "5dip"
android:background= "@drawable/contact_0"/>
<textview
Android:id= "@+id/ct_name"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:padding= "5dip"
android:layout_torightof= "@id/ct_photo"
android:layout_aligntop= "@id/ct_photo"
Android:text= "For you and me in the cold wind"
Android:textsize= "8pt"
Android:textstyle= "Bold"
Android:maxlength= "7"/>
<textview
Android:id= "@+id/ct_sign"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:padding= "5dip"
android:layout_torightof= "@id/ct_photo"
android:layout_alignbottom= "@id/ct_photo"
Android:text= "Why does it always hurt me?"
Android:textcolor= "#888888"/>
<!--attention is not layout_padding-->
</RelativeLayout>

Because here is the definition of the Myadapter class, you can more flexible implementation of some of the list functions, such as with the database, dynamic Update data, add button control and so on, in this example imitation QQ list for avatar set to ImageButton, The following figure in a toast information is clicked on the image to make the corresponding, of course, click on a line can also make corresponding, this follow-up may be to the QQ program to do some expansion, such as the addition of network modules, chat windows and so on. We'll discuss it further later.

Here is the Myadapter class, which is best placed in the same package as the Mainactivity class.

Copy Code code as follows:

Package com.example.android_qqlist;

Import java.util.*;

Import Android.annotation.SuppressLint;
Import Android.content.Context;
Import android.graphics.drawable.Drawable;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.ViewGroup;
Import android.widget.*;

public class Myadapter extends baseadapter{
Private context Context=null;
private int resources;
Private arraylistPrivate string[] from;
Private int[] to;
/**
* Here is the Simpleadapter parameter list.
* @param context
* @param Resources
* @param list
* @param from
* @param to
*/

public Myadapter, int,
arraylistSuper ();
This.context = context;
This.resources = resources;
This.list = list;
This.from = from;
This.to = to;
}

/**
* The remaining problem is to implement the baseadapter in sequence
*/

@Override
public int GetCount () {//This method returns the number of ListView rows
TODO auto-generated Method Stub
return List.size ();
}
@Override
Public Object getitem (int arg0) {//This method is not necessary to use, you can use Getitemid instead
TODO auto-generated Method Stub
return null;
}
@Override
public long getitemid (int itemId) {//The method is invoked when a row is clicked, and its parameters are provided by the Android system
TODO auto-generated Method Stub
return itemId;
}
/**
* The GetView method is called when the system is drawing each row, in which you want to set the text that you want to display, the picture,
* and set the listener for the button.
*
* Formal parameter meaning:
* Position: The position (ID) of the item currently drawn;
* Convertview, when the system is drawing ListView, if it is drawing the first item (that is, the first row), the Convertview is null, when
* To draw the second and subsequent item Convertview is not empty, you can directly use this Convertview Gettag () method to obtain the control
* and make the appropriate settings so that you can speed up the drawing.
*
* To set additional information for Convertview tag, create an internal class viewholder that holds references to all the controls in a row
* Instantiated as additional information for Convertview.
*/
Class viewholder{
Public ImageButton Ctphoto=null;
Public TextView Ctname=null,ctsign=null;

/*
* It can be seen from here that the elements between the from and to arrays should correspond one by one, while the order between from and to respective elements is different, and finally ListView
* The location of the presentation will also be different!
*/
Public Viewholder (View Convertview) {
Ctphoto= (ImageButton) Convertview.findviewbyid (to[0]);
/* Note that both view and activity belong to the container class, you need to set the layout file, the interior contains child controls, and all have Findviewbyid ()
* There's no obvious inheritance between them
*/
Ctname= (TextView) Convertview.findviewbyid (to[1]);
Ctsign= (TextView) Convertview.findviewbyid (to[2]);

}

}
Class Imagelistener implements onclicklistener{

private int position;

public Imagelistener (int position) {
This.position=position;
}//constructor has no return value

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
String str=list.get (position). Get (From[1]). toString ();
Toast.maketext (context,str+ "is clicked", Toast.length_long). Show ();

}

}
@Override
Public View GetView (int position, View Convertview, ViewGroup arg2) {
TODO auto-generated Method Stub

/**
* First of all, it is not the first time to create the item, if so, create Convertview instances and Viewholder objects, and pass the Fandviewbyid () method
* Get an instance of all the spaces in each row in the Viewholder object, and then set the label on Convertview
*/
Viewholder Viewholder=null;

Note that Convertview is not created randomly and requires layoutinflater to be created according to List_item layout file
if (convertview==null) {
Layoutinflater Inflater=layoutinflater.from (context);
Convertview=inflater.inflate (Resources,null); The null here is a viewgroup parameter and is not used in the basic
Viewholder=new Viewholder (Convertview);
Convertview.settag (Viewholder);
}
else{
Viewholder= (Viewholder) Convertview.gettag (); Obtaining additional information through the Gettag () method
}
/**
* This is the appropriate setting for each control in the Viewholder
*/
/**
* @author DRAGONGN
* Here's a problem: when you draw the current row's ListItem, you only need to set the control for the current row, so you can't add a for
* Loop through each of the elements in each list, depending on the position of the ListItem line that is currently created, and then
* Access to the corresponding location in the database map data, the control settings!
*/
/**
* Note that this must be setbackgrounddrawable () instead of SetBackground (), which will have an error, although the former is expired but is as available
*/
ViewHolder.ctPhoto.setBackgroundDrawable ((drawable) (List.get (position). Get (From[0)));
Map to add a Drawable object, where the elements in from and to should correspond one by one, and the order should correspond to the order of the controls in the Viewholder construction method

ViewHolder.ctName.setText ((String) (List.get (position). Get (from[1)));
ViewHolder.ctSign.setText ((String) (List.get (position). Get (from[2)));
ViewHolder.ctPhoto.setOnClickListener (new Imagelistener (position));
return convertview; To return this view object for each row
}
}

The last is the Mainactivity class, and because the Myadapter package is the same as the simpleadpter is the same amount, so here mainactivity operation is basically unchanged.

Copy Code code as follows:

Package com.example.android_qqlist;

Import java.util.*;

Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.widget.ListView;

public class Mainactivity extends activity {

The column name for each column/map the key name and the ID of its corresponding view child control
String[] from={"Userphoto", "UserName", "usersign"}; The content here corresponds to the keys in the back HashMap.
Int[] To={r.id.ct_photo,r.id.ct_name,r.id.ct_sign};

An array of all the information and resources displayed by the entire ListView
Int[] Photores={r.drawable.contact_0,r.drawable.contact_1,r.drawable.contact_2,r.drawable.contact_3};
String[] strname={"The Night of the war", "Street corner of Happiness", "quiet", "Angry little fat"};
String[] strsign={"Where is I love ...", "some things finally want to open", "one day will find their own happiness", "who called me small fat I with who anxious ..."};

Data link list and map container
ArraylistHashmap<string,object> Map=null;

ListView Listview=null;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
listview= (ListView) Findviewbyid (r.id.qq_list);
List=new arraylistfor (int i=0; i<4; i++) {
Map=new hashmap<string,object> (); Map calls the Put method to add a key value pair
Map.put ("Userphoto", Getresources (). getdrawable (photores[i));
Map.put ("UserName", Strname[i]);
Map.put ("Usersign", Strsign[i]);
List.add (map);
}
Create a custom Myadapter object
Myadapter adapter=new Myadapter (this,r.layout.list_item,list,from,to);

Call the ListView Setadapter () method to set the adapter
Listview.setadapter (adapter);
}

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
}



These are the pictures I downloaded myself, and the corresponding resource address is represented in a photores array in Mainactivity ~

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.