Make a small sign-inProgram, Fill in the sign-in information in the first activity. Then it is displayed in the second activity.
As you can see, this program involves textview, edittext, spinner, radiogroup, radiobutton, button, and listview components. You can practice how to use the view. My idea is to create a personal information personinfo class. After clicking the sign-in button, a personinfo object is generated and the object is passed into the second activity, in the second avctivity, use a personinfo to store the received object and display it again.
Question 1: The intent method in activity to pass objects. For this reason, refer to the two methods mentioned in this article: 1. Implement the serializable interface; 2. Implement the parcelable interface.
The process was quite smooth, and the personinfo object was successfully received. Then, the job is displayed in listactivity.CodeIt is written in this way.
Package Com. zxl. signin; Import Java. util. arraylist; Import Java. util. hashmap; Import Android. App. listactivity; Import Android. OS. Bundle; Import Android. widget. simpleadapter; Public Class Infolistactivity Extends Listactivity { Private Bundle bundle; Private Personinfo [] personinfos; Private Simpleadapter lisadapter; Protected Void Oncreate (bundle savedinstancestate ){ Super . Oncreate (savedinstancestate ); // Obtains the person object passed in from the previous activity. Personinfos = New Personinfo [10 ]; Getperson (); Setadaptet (); } Private Void Getperson () {bundle = Getintent (). getextras (); personinfo person = (Personinfo) bundle. getserializable ("info" ); Int I = Person. GETID (); personinfos [I] = Person; system. Out. println (I );} Private Void Setadaptet () {arraylist <Hashmap <string, string> List = New Arraylist (); For ( Int I = 1; I <= personinfo. No; I ++ ) {Hashmap <String, string> map = New Hashmap <string, string> (); Map. Put ( "Name" , Personinfos [I]. getname (); map. Put ( "School" , Personinfos [I]. getschool (); map. Put ( "Sex", Personinfos [I]. getsex (); map. Put ( "Tel" , Personinfos [I]. gettelno (); list. Add (MAP);} lisadapter = New Simpleadapter ( This , List, R. layout. listitem, New String [] {"name", "school", "sex", "tel" }, New Int [] {R. Id. Name, R. Id. School, R. Id. Sex, R. Id. Tel}); setlistadapter (lisadapter );}}
It seems that there is no problem. If you do not register a person, you can put it in the array personinfos. Then, you can use a loop in setadapter to retrieve it from the array and display it in listactivity.
The display is quite normal, but when I press the return key to exit the activity and register again, a null pointer exception will be reported. After debugging for a long time, I finally found my negligence. Originally, the array personinfos in listacitifos was not stored after you press the return key. After you click Register again, a new personinfos will be created, which leads to an error in the for loop, because personinfos [1] = NULL.
A headache. How can I save the temporary storage personinfos? No solution yet.