Android ListView Getviewtypecount and Getitemviewtype

Source: Internet
Author: User

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

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.