Package Newdemo.jeno.listviewdemo;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.ImageView;
Import Android.widget.ListView;
Import Android.widget.TextView;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Map;
public class Testandroidactivity extends Activity {
EditText msgcontent;
ListView ListView;
Baseadapter adapter;
arraylist<map<string, string>> data = new arraylist<map<string, string>> ();
/**
* Called when the activity is first created.
*/
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Initviews ();
}
private void Initviews () {
Msgcontent = (EditText) Findviewbyid (r.id.content);
Button Btnsend = (button) Findviewbyid (r.id.send);
Btnsend.setonclicklistener (Onclicklistener);
Button btnreceive = (button) Findviewbyid (r.id.receive);
Btnreceive.setonclicklistener (Onclicklistener);
Button Btnpic = (button) Findviewbyid (r.id.pic);
Btnpic.setonclicklistener (Onclicklistener);
ListView = (ListView) Findviewbyid (R.id.list);
adapter = new Testadapter ();
Listview.setadapter (adapter);
}
Private View.onclicklistener Onclicklistener = new View.onclicklistener () {
public void OnClick (View v) {
Map<string, string> item;
item = new hashmap<string, string> ();
Switch (V.getid ()) {
Case R.ID.PIC:
Item.put ("type", Type_pic + "");
Item.put ("Content", "picture");
Break
Case R.id.send:
Item.put ("type", Type_send + "");
Item.put ("Content", "send Message" + Msgcontent.gettext (). toString (). Trim ());
Break
Case R.id.receive:
Item.put ("type", Type_receive + "");
Item.put ("Content", "received message" + Msgcontent.gettext (). toString (). Trim ());
Break
}
Data.add (item);
Adapter.notifydatasetchanged ();
Msgcontent.settext ("");
}
};
Class Testadapter extends Baseadapter {
public int GetCount () {
return Data.size ();
}
Public Object getItem (int position) {
if (Position < GetCount ()) {
return Data.get (position);
}
return null;
}
public long getitemid (int position) {
return 0;
}
Public View GetView (int position, View Convertview, ViewGroup parent) {
System.out.println ("GetView::" + position);
int type = Type_send;
try {
Type = Integer.parseint (Data.get (position). Get ("type"));
} catch (Exception e) {
E.printstacktrace ();
}
Viewholder holder = null;
if (Convertview = = null) {
System.out.println ("Getview::convertview is null");
Holder = new Viewholder ();
Switch (type) {
Case Type_send:
Convertview = View.inflate (Getbasecontext (),
R.layout.listitem_send, NULL);
Holder.text = (TextView) convertview
. Findviewbyid (R.id.message);
Break
Case Type_receive:
Convertview = View.inflate (Getbasecontext (),
R.layout.listitem_receive, NULL);
Holder.text = (TextView) convertview
. Findviewbyid (R.id.message);
Break
Case TYPE_PIC:
Convertview = new ImageView (Getbasecontext ());
((ImageView) convertview). Setimageresource (R.drawable.icon);
Break
}
Convertview.settag (holder);
} else {
System.out.println ("Getview::convertview not NULL");
Holder = (viewholder) convertview.gettag ();
}
if (type! = Type_pic) {
String msg = data.get (position). Get ("content");
Holder.text.setText (msg);
}
return convertview;
}
public int Getitemviewtype (int position) {
int type = Super.getitemviewtype (position);
try {
Type = Integer.parseint (Data.get (position). Get ("type"));
} catch (Exception e) {
E.printstacktrace ();
}
System.out.println ("Getitemviewtype::" + position + "is" + type);
return type;
}
public int Getviewtypecount () {
System.out.println ("Getviewtypecount is" + 3);
return 3;
}
Class Viewholder {
TextView text;
}
}
/**
* Messages Sent
*/
private static final int type_send = 0;
/**
* Received Message
*/
private static final int type_receive = type_send + 1;
/**
* Picture
*/
private static final int type_pic = type_receive + 1;
}
A project description problem
Note that the following are:
In
public int Getitemviewtype (int position) {
int type = Super.getitemviewtype (position);
try {
Type = Integer.parseint (Data.get (position). Get ("type"));
} catch (Exception e) {
E.printstacktrace ();
}
System.out.println ("Getitemviewtype::" + position + "is" + type);
return type;
The type of secondary place return must be more than the following
Getviewtypecount value is small, such as the following count returns is 3 how this place the largest is 2 is the return is the array of the corner label
}
public int Getviewtypecount () {
System.out.println ("Getviewtypecount is" + 3);
return 3;
}
Android ListView Getviewtypecount and Getitemviewtype