ANDROID/JAVA Newbie having problems with classes in separate files -
i have create simple app shows listview
in turn shows contents of listarray
. have added button when clicked, adds item listview
. here's problem: after reading other people's code have noticed mainactivity
file tends default file , classes , created in separate files. trying put arrayadapter
, button
, listarray
code separate file.. make nicer! have ideas why when run app (it compiles fine/ no errors) listview
no longer populates or adds item on button click. time! here's code:
mainactivity.java
package com.example.shoppingapp; import android.app.activity; import android.os.bundle; import android.view.menu; public class mainactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
arrayadapterclass
package com.example.shoppingapp; import java.util.arraylist; import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.arrayadapter; import android.widget.button; import android.widget.listview; public class arrayadapterclass extends activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); final arraylist<string> mystringarray = new arraylist<string>(); mystringarray.add("one"); final arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_multiple_choice, mystringarray); listview listview = (listview) findviewbyid(r.id.listview); listview.setadapter(adapter); final button button = (button) findviewbyid(r.id.addnote); button.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { // perform action on click // toast textpopup = toast.maketext(getapplicationcontext(), "hello", toast.length_short); // textpopup.show(); mystringarray.add("two"); adapter.notifydatasetchanged(); } }); } }
edit: should add... when of code in single file (the mainactivity one) runs fine.
if you're using stock api classes (no methods override or implement), there isn't huge need create individual files each. typically new class files created when new define new class or extend existing one.
it seems arrayadapterclass
extends activity
. activity
individual app screen. typically move between activities using intent
. seems mainactivity
has no way start arrayadapterclass
.
Comments
Post a Comment