android - Can the main activity be able to grab elements from two XML files? -
update code (sorry crappy formatting of code, reason had problems allowing me post had mess lines whole allow me save thisedit)
here idea. have app works clarifia's image recognition. generated app using google's pre built navegation bar, there xml files , code that, can ignored 2 needed activity_main.xml , content_main.xml. anyways in content_main.xml linear layout has imageview , listview. goal dynamically generate listview list of buttons. each button have settext() done give tag, example if image selected dog, , tags dog, animal, etc, many buttons generated, settext() of 1 button being dog, other button being animal, etc . since have network call, network call done in asynctask. after done, method onpostexecute() called , there tags. since got tags, want call set adapter hold array of buttons, , loop geting id each button , doing settext() on each button tags. there want set adapter list view..
problems:
way many count, think narrowed down me not knowing how "views" second xml file have elements used on first xml file, because comes out null. tried googling keep running road blocks. want name each button tag , put them listview, said, since these elements in different xml file main_activity, think problem. here code per request.
mainactivity.java
public class mainactivity extends appcompatactivity implements navigationview.onnavigationitemselectedlistener { private static final string class = mainactivity.class.getsimplename(); private button selectbutton; private toolbar toolbar; private navigationview navigationview; private clari faidata cdata = null; private imageview imageview; private listview listview; private tagadapter adapter; private list<button> data; protected context context; private googleapiclient client; protected linearlayout main; @override protected void oncreate(bundle savedinstancestate) { // attempt // http://www.java2s.com/code/android/ui/usingtwolayoutxmlfileforoneactivity.htm super.oncreate(savedinstancestate); context = mainactivity.this; main = new linearlayout (this); setcontentview(r.layout.activity_main); // auto generated stuff left out nav bar, showing line********* selectbutton = (button) findviewbyid(r.id.select_button); selectbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { final intent media_intent = new intent(intent.action_pick, mediastore.images.media.external_content_uri); // start api on net startactivityforresult(media_intent, cdata.getokcode()); } }); // stuff******************************************************** cdata = new clarifaidata(this); imageview = (imageview) findviewbyid(r.id.image_view); client = new googleapiclient.builder(this).addapi(appindex.api).build(); } @override protected void onactivityresult(int requestcode, int resultcode, intent intent) { super.onactivityresult(requestcode, resultcode, intent); if (requestcode == cdata.getokcode() && resultcode == result_ok) { uri image = intent.getdata(); if (image != null) { // left out stuff image resizing*************************** //************************************************** start looking here*************************************** new asynctask<uri, void, recognitionresult>() { @override protected recognitionresult doinbackground(uri... image) { // api call on internet, needed async return cdata.recognizebitmap(image[0]); } @override protected void onpostexecute(recognitionresult result) { super.onpostexecute(result); if (cdata.gettags(result)) { selectbutton.setenabled(true); selectbutton.settext("select photo"); // attempt // http://www.java2s.com/code/android/ui/usingtwolayoutxmlfileforoneactivity.htm layoutinflater inflate = (layoutinflater) getsystemservice(context.layout_inflater_service); linearlayout taglayout = (linearlayout) inflate.inflate(r.layout.tag_list_item_trio_item, null); linearlayout.layoutparams parm = new linearlayout.layoutparams( linearlayout.layoutparams.wrap_content, linearlayout.layoutparams.wrap_content); listview = (listview) main.findviewbyid(r.id.tagview); main.addview(taglayout, parm); // arraylist of tags hold strings list tags = cdata.getmaptags(); // data array of buttons, each button labled each value in tags data = new arraylist<button>(); (int = 0; < tags.size(); i++) { // id each button , put array settext string loc = "button_item_" + i; int id = getresources().getidentifier(loc, "id", getpackagename()); button temp = (button) main.findviewbyid(r.id.button_item_0); temp.settext("test " + i); } // here problem, need way layout stuff main activity adapter = new tagadapter(mainactivity.this, getresources().getidentifier("tag_list_item_trio_item", "id", getpackagename()), data); listview.setadapter(adapter); } else bottomtoast(cdata.getrecerror()); } }.execute(image); } else { bottomtoast(cdata.getloaderror()); } } }
tagadapter.java
import android.content.context; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.button; import android.widget.imageview; import android.widget.textview; import java.util.list; public class tagadapter extends arrayadapter<button> { private context context; private list<button> taglist; public tagadapter(context context, int resource, list<button> objects) { super(context, resource, objects); log.i("test", "constructor " ); this.context = context; this.taglist = objects; } @override public int getcount() { return taglist.size(); } getview(int, android.view.view, android.view.viewgroup) @override public view getview(final int position, view convertview, viewgroup parent) { layoutinflater layoutinflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); layoutinflater.inflate(r.layout.tag_list_item_dual_item, parent, false); final button tag = taglist.get(position); view view = null; view = layoutinflater.inflate(r.layout.tag_list_item_trio_item, parent, false); else { view = layoutinflater.inflate(r.layout.tag_list_item_dual_item, parent, false); button nametextview = (button) view.findviewbyid(r.id.first_button_dual_item); nametextview.settext("test"); button nametextview2 = (button) view.findviewbyid(r.id.second_button_dual_item); nametextview2.settext("test2"); } return view; } }
content_main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" android:gravity="center|bottom" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <imageview android:layout_width="match_parent" android:layout_height="0dp" android:adjustviewbounds="true" android:id="@+id/image_view" android:background="#653fff" android:layout_weight="0.5" android:padding="1dp" /> <listview android:id="@+id/tagview" android:layout_width="match_parent" android:layout_weight="0.35" android:layout_height="0dp" android:padding="5dp" android:background="#68343f" android:layout_margintop="10dp" android:layout_gravity="center_horizontal" android:textalignment="center" /> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageview" android:layout_gravity="center_horizontal" android:textalignment="center" /> <button android:id="@+id/select_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/sel_image" android:layout_margintop="8dp" android:layout_marginbottom="16dp" android:paddingleft="24dp" android:paddingright="24dp" android:background="#3d88ec" /> </linearlayout>
tag_list_item_trio.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_centerhorizontal="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:textalignment="center" android:layout_centerinparent="true" android:orientation="horizontal"> <button android:id="@+id/button_item_0" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:layout_margin="5dp" android:paddingleft="10dp" android:paddingright="10dp" android:background="#000000" android:layout_gravity="center" android:adjustviewbounds="true" android:text="test 1" android:textcolor="#ffffff" /> <button android:id="@+id/button_item_1" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:layout_margin="5dp" android:paddingleft="10dp" android:paddingright="10dp" android:background="#000000" android:layout_gravity="center" android:adjustviewbounds="true" android:text="test 2" android:textcolor="#ffffff" /> <button android:id="@+id/button_item_2" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:layout_margin="5dp" android:paddingleft="10dp" android:paddingright="10dp" android:background="#000000" android:layout_gravity="center" android:adjustviewbounds="true" android:text="test 3" android:textcolor="#ffffff" /> </linearlayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_centerhorizontal="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:textalignment="center" android:layout_centerinparent="true" android:orientation="horizontal"> <button android:id="@+id/button_item_0" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:layout_margin="5dp" android:paddingleft="10dp" android:paddingright="10dp" android:background="#000000" android:layout_gravity="center" android:adjustviewbounds="true" android:text="test 1" android:textcolor="#ffffff" /> <button android:id="@+id/button_item_1" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:layout_margin="5dp" android:paddingleft="10dp" android:paddingright="10dp" android:background="#000000" android:layout_gravity="center" android:adjustviewbounds="true" android:text="test 2" android:textcolor="#ffffff" /> <button android:id="@+id/button_item_2" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:layout_margin="5dp" android:paddingleft="10dp" android:paddingright="10dp" android:background="#000000" android:layout_gravity="center" android:adjustviewbounds="true" android:text="test 3" android:textcolor="#ffffff" /> </linearlayout>
one thing should know listview , items virtualized or recycled/reused or duplicated if should say. how see think approach off.
this how suggest rectify it, before want clarify way understood portion of requirement
my goal dynamically generate listview list of buttons. each button have settext() done give tag, example if image selected dog, , tags dog, animal, etc, many buttons generated, settext() of 1 button being dog
so saying want listview 4 buttons on each row.
do this, _i taking relevant portions.
private listview listview; //your listview private tagadapter adapter; // adapter //we in oncreate //i have no knowledge on cdata bare me here //now remove list<button> data; code
we have jumped tagadapter class
private context context; //private list<button> taglist; remove private arraylist<theclassthatcontainstags> mytags;//i assuming cdata or? //but list should contain settext() button text public tagadapter(context context) { //this how constructor super(context); log.i("test", "constructor " ); this.context = context; //here start async task , put async task logic here //if async task requires objects or items not in class // since separate class, can inject them, when inject // put them in constructor of tagadapter inject context //object instance, might change // public tagadapter(context context,onemoreclassifiwant omciiw) { // here aysnc task execute, when onpostexecute triggered/ //called following, remove code lines // have under onpostexecute // onpostexecute has triggered mytags = // tag items result onpostexecute //now mytags arraylist of type theclassthatcontainstags has been //instantiated }
we moving getcount still in custom adapter
@override public int getcount() { return (mytags == null) ? 0 : mytags.size(); }
we moving getview still in custom adapter
@override public view getview(final int position, view convertview, viewgroup parent) { //in posted getview, did edit too? if not give errors? //nevermind // here check if convertview null , instantiate // position here in method parameter index in mytags // list if(convertview == null){ //guess know how this. should same old //getview minus final button tag = taglist.get(position); // , line below it. } //here still in getview - , find particular button want convertview.findviewbyid() //my understanding pointer out want have 4 buttons in row //it should button b = convertview.findviewbyid(r.id.button1); b.settext(getitem(position));//getitem() instance method //class extending, , returns object of type t, in //example theclassthatcontainstags.class; // , can same next 3 buttons }
we out of getview , custom adapter class , , in oncreate . here set when need tags tagadapter = new tagadapter(context,anyifdesired_otherinjections); listview.setadatper(tagadapter);
now done. hope helps, please read listview , arraylist adapter foresight of doing , have posted here. trim down hours waste, if spend 12 hours on docs spend 5 minutes writing , next time want replicate same 5 minutes.
be sir , wish success.
Comments
Post a Comment