android - How to add EditText value in arraylist in Custom ListView -
i have created 2 textview , 1 edittext in first custom listview,i want add/store edittext value in arraylist because want show edittext value in second custom listview 1 one,so how please give hint or code solve problem..
public class mmenu extends activity { arraylist<candy> myarrlist; arraylist<string> edittextvalues; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); myarrlist = new arraylist<candy>(); edittextvalues = new arraylist<string>(); final listview lisview1 = (listview)findviewbyid(r.id.listview1); final listview lisview2 = (listview)findviewbyid(r.id.listview2); myarrlist.add(new candy("butterscotch", "rs 10")); myarrlist.add(new candy("birthday cake", "rs 100")); myarrlist.add(new candy("black crunch", "rs 102")); myarrlist.add(new candy("industrial chocolate", "rs 200")); myarrlist.add(new candy("coffee molasses chip", "rs 500")); lisview1.setadapter(new countryadapter(this)); button btngetitem = (button) findviewbyid(r.id.btngetitem); btngetitem.setonclicklistener(new onclicklistener() { public void onclick(view v) { int count = lisview1.getadapter().getcount(); for(int i=0;i<count;i++) { linearlayout itemlayout = (linearlayout)lisview1.getchildat(i); edittext text = (edittext)findviewbyid(r.id.txtinput); string value = text.gettext().tostring(); edittextvalues.add(value); toast.maketext(getapplicationcontext(),"10"+value, toast.length_long).show(); } lisview2.setadapter(new countryadapter2(getapplicationcontext())); } }); } public class countryadapter extends baseadapter { private context context; public countryadapter(context c) { //super( c, r.layout.activity_column, r.id.rowtextview, ); // todo auto-generated method stub context = c; } public int getcount() { // todo auto-generated method stub return myarrlist.size(); } public object getitem(int position) { // todo auto-generated method stub return position; } public long getitemid(int position) { // todo auto-generated method stub return position; } public view getview(final int position, view convertview, viewgroup parent) { // todo auto-generated method stub layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); if (convertview == null) { convertview = inflater.inflate(r.layout.views, null); } // colid textview txtid = (textview) convertview.findviewbyid(r.id.nm); txtid.settext(myarrlist.get(position).getid() +"."); // colcode textview txtcode = (textview) convertview.findviewbyid(r.id.rat); txtcode.settext(myarrlist.get(position).getcode()); return convertview; } } public class countryadapter2 extends baseadapter { private context context; public countryadapter2(context c) { context = c; } public int getcount() { // todo auto-generated method stub return myarrlist.size(); } public object getitem(int position) { // todo auto-generated method stub return position; } public long getitemid(int position) { // todo auto-generated method stub return position; } public view getview(final int position, view convertview, viewgroup parent) { // todo auto-generated method stub layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); if (convertview == null) {convertview = inflater.inflate(r.layout.newviews, null); } // colid textview txtid = (textview) convertview.findviewbyid(r.id.nm); txtid.settext(myarrlist.get(position).getid() +"."); // colcode textview txtcode = (textview) convertview.findviewbyid(r.id.rat); txtcode.settext(myarrlist.get(position).getcode()); return convertview; }
first of all, wasting lot of space creating new hashmap<string, string>
every time. not sure why have arraylist
of hashmaps
. having 1 or other should sufficient.
you can create class called candy such:
class candy { private string id; private string code; candy(string id, string code) { this.id = id; this.code = code; } public string getid() { return this.id; } public string getcode() { return this.code; } }
then can define arraylist
, instantiate in beginning of oncreate()
:
// don't use starting capital letters methods, classes have first capital letter // renamed myarrlist myarrlist arraylist<candy> myarrlist; // ... void oncreate() { // ... myarrlist = new arraylist<candy>(); // ... }
and populate like:
myarrlist.add(new candy("butterscotch", "rs 10")); myarrlist.add(new candy("birthday cake", "rs 100")); myarrlist.add(new candy("black crunch", "rs 102")); myarrlist.add(new candy("industrial chocolate", "rs 200")); myarrlist.add(new candy("coffee molasses chip", "rs 500"));
now can use getter methods of candy
class called getid()
, getcode()
.
change:
// colid textview txtid = (textview) convertview.findviewbyid(r.id.nm); txtid.settext(myarrlist.get(position).get("id") +"."); // colcode textview txtcode = (textview) convertview.findviewbyid(r.id.rat); txtcode.settext(myarrlist.get(position).get("code"));
to:
// colid textview txtid = (textview) convertview.findviewbyid(r.id.nm); txtid.settext(myarrlist.get(position).getid() +"."); // colcode textview txtcode = (textview) convertview.findviewbyid(r.id.rat); txtcode.settext(myarrlist.get(position).getcode());
now answer question, assume want store text within edittext
field arraylist
.
you can create class arraylist
object holds string
objects , instantiate in oncreate()
.
arraylist<string> edittextvalues; // ... void oncreate() { // ... edittextvalues = new arraylist<string>(); // ... }
now can add text edittext
arraylist
:
edittext text = (edittext)findviewbyid(r.id.your_id); string value = text.gettext().tostring(); edittextvalues.add(value);
and use edittextvalues
need them.
Comments
Post a Comment