An error occurred while adding data to the adapter through the listener of the button in Android.

Source: Internet
Author: User

OriginallySource codeAs follows:

 
List <restaurant> model; // a custom list data that stores the custom lunchlistadapter; // a custom listview adapter ...... // class onsavereimplements onclicklistener {// listener for button save is omitted. After clicking it, add the data restaurant r = new restaurant () to the model (); // ② @ overridepublic void onclick (view v) {R. setname (name. gettext (). tostring (); R. setaddress (ADDR. gettext (). tostring (); Switch (types. getcheckedradiobuttonid () {case R. id. take_out: R. settype (take_out); break; case R. id. sit_down: R. settype (sit_down); break; case R. id. delivery: R. settype (delivery); break;} model. add (r); // ① }}

The problem is that after you click "Storage", if Adapter. Add (R) is used at ① );

The items displayed in the listview are all stored at the end, and the output content from the model is the same after the item is clicked,

If Model. Add (R) is used at ① );

The items displayed in the listview are correct, that is, the order of the content you store. However, after you click the item, the content read from the model is the same as that shown above, all are the last stored data,

 

I don't know how to figure it out if I want to, but I finally want to figure it out.CodeIt is defined in onclick, so that no matter which method is used, the display and output results are correct.

For the moment, I think that the data added to the R model defined in section ② may be overwritten, but it is still a bit confused. I would like to write down this impression first.

 

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.