android - Fragment with network operation and recycler view doesnt load in mainactivity -


i have 2 problems: 1) when tried load fragment2 mainactivity directly during oncreate () not display 2) when created button go fragment2 , clicked button go fragment2, goes fragment2 goes after second click or double click on same button. have tried using setfocusableintouchmode= true , false both, getting same results, button works after same 2 clicks.so whats network operations? how can display results of fragment2 network operations shown in mainactivity ? , button? heres code:

my androidmanifest:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"    >     <uses-permission android:name="android.permission.internet" />      <application         android:allowbackup="true"         android:icon="@mipmap/ic_launcher"         android:label="@string/app_name"         android:roundicon="@mipmap/ic_launcher_round"         android:supportsrtl="true"         android:theme="@style/apptheme">         <activity android:name=".mainactivity">             <intent-filter>                 <action android:name="android.intent.action.main" />                  <category android:name="android.intent.category.launcher" />             </intent-filter>         </activity>     </application> </manifest> 

heres mainactivity:

import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.button; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmenttransaction;  public class mainactivity extends appcompatactivity {      button b1,b2;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);           fragment frag2 = new fragment2();         fragmenttransaction transaction =getsupportfragmentmanager().begintransaction();         transaction.replace(r.id.fl, frag2);         transaction.addtobackstack(null);         transaction.commit();         log.i("reaktion","commited");            b1=(button)findviewbyid(r.id.f1);         b2=(button)findviewbyid(r.id.f2);         b2.setfocusableintouchmode(false);  // //        fragment2 frag2= new fragment2(); //        fragmentmanager manager = getsupportfragmentmanager(); //        fragmenttransaction transaction = manager.begintransaction();  //// tried this, said, nothing on oncreate () //        transaction.replace(r.id.fl,frag2); //        transaction.commit(); //      }     public void frag1(view view){           fragment newfragment = new fragment1();          fragmenttransaction transaction =getsupportfragmentmanager().begintransaction();         transaction.replace(r.id.fl, newfragment);         transaction.addtobackstack(null);         transaction.commit();        }     public void frag2(view view){           fragment2 frag2= new fragment2();         fragmentmanager manager = getsupportfragmentmanager();         fragmenttransaction transaction = manager.begintransaction();         transaction.replace(r.id.fl,frag2);         transaction.commit();       }   } 

my fragment1:

import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmenttransaction; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.button;   public class fragment1 extends fragment {  button btn;     public fragment1() {         // required empty public constructor     }       @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         container.removeallviewsinlayout();         view v= inflater.inflate(r.layout.fragment_fragment1, container, false);         btn=(button)v.findviewbyid(r.id.btn);          btn.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                   fragment2 frag2= new fragment2();                 fragmentmanager manager = getfragmentmanager();                 fragmenttransaction transaction = manager.begintransaction();                 transaction.replace(r.id.fl,frag2);                 transaction.commit();                    }         });         // inflate layout fragment         return v;     }  } 

my fragment2:

import android.os.bundle; import android.support.v7.widget.gridlayoutmanager; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview;  import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject;  import java.util.arraylist; import java.util.list;  import android.support.v4.app.fragment; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup;     public class fragment2 extends fragment {      static string   j="";     recyclerview rv;     private gridlayoutmanager gridlayoutmanager;     private myadapter adapter;      arraylist<string> company_namelist;     arraylist<string> company_websitelist;  string jr;     public fragment2() {         // required empty public constructor     }       @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         container.removeallviewsinlayout();          // inflate layout fragment         view view= inflater.inflate(r.layout.fragment_fragment2, container, false);          company_namelist =new arraylist<>();         company_websitelist=new arraylist<>();           loaddata(); log.i("loadhua","connection request made");         rv = (recyclerview)view.findviewbyid(r.id.recycler_view);          linearlayoutmanager linearlayoutmanager= new linearlayoutmanager(getactivity());         adapter = new myadapter(company_namelist,company_websitelist);          rv.setlayoutmanager(linearlayoutmanager);         rv.setfocusableintouchmode(false);         log.i("jojojo","recycler has setted adapter");         rv.setadapter(adapter);           return view;     }  public void loaddata(){      companyfromserver obj = new companyfromserver();     jsonobject = new jsonobject();      try {         a.put("nothing","nothing" );        } catch (jsonexception e) {         e.printstacktrace();     }     obj.execute(string.valueof(a));     jr=obj.getjr(); log.i("nacho",jr);   try{     jsonobject jsonobject = new jsonobject(jr);     jsonarray jsonarray = jsonobject.getjsonarray("data");      for(int i=0;i<jsonarray.length();i++){          jsonobject jsondata = jsonarray.getjsonobject(i);         string company_name= jsondata.getstring("company_name");         string company_website=jsondata.getstring("company_website");           company_namelist.add(company_name);         company_websitelist.add(company_website);      }    }catch (jsonexception e){}          }     } 

companyfromserver:

import android.os.asynctask; import android.util.log;  import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.outputstreamwriter; import java.io.writer; import java.net.httpurlconnection; import java.net.url;  class companyfromserver extends asynctask<string, string, string> {       public static string   j="";     @override     protected string doinbackground(string... params) {          string jsonresponse = "";         string jsondata = params[0];         httpurlconnection urlconnection = null;         bufferedreader reader = null;         try {             url url = new url("example.com");             urlconnection = (httpurlconnection) url.openconnection();             urlconnection.setdooutput(true);             // output buffer writter             urlconnection.setrequestmethod("post");             urlconnection.setrequestproperty("content-type", "application/json");             urlconnection.setrequestproperty("accept", "application/json"); //set headers , method             writer writer = new bufferedwriter(new outputstreamwriter(urlconnection.getoutputstream(), "utf-8"));             writer.write(jsondata); // json data             writer.close();             inputstream inputstream = urlconnection.getinputstream(); //input stream             stringbuffer buffer = new stringbuffer();             if (inputstream == null) {                 // nothing do.                 return null;             }             reader = new bufferedreader(new inputstreamreader(inputstream));              string inputline;             while ((inputline = reader.readline()) != null)                 buffer.append(inputline + "\n");             if (buffer.length() == 0) {                 // stream empty. no point in parsing.                 return null;             }             jsonresponse = buffer.tostring();              j=j+jsonresponse;              log.i("xyz",j); //response data             log.i("o/p:", jsonresponse);    //send post execute           }          catch (ioexception e) {             e.printstacktrace();         } {             if (urlconnection != null) {                 urlconnection.disconnect();             }             if (reader != null) {                 try {                     reader.close();                 } catch (final ioexception e) {                     log.e("wtf", "error closing stream", e);                 }             }         }         return  j;     }           public string getjr(){         log.i("klopp",j);         return j; }   } 

mainactivity.xml:

<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"   >     <relativelayout         android:id="@+id/rl"         android:layout_width="0dp"         android:layout_height="0dp"         tools:layout_constrainttop_creator="1"         tools:layout_constraintright_creator="1"         tools:layout_constraintbottom_creator="1"         android:layout_marginstart="8dp"         app:layout_constraintbottom_tobottomof="parent"         android:layout_marginend="8dp"         app:layout_constraintright_torightof="parent"         android:layout_margintop="8dp"         tools:layout_constraintleft_creator="1"         android:layout_marginbottom="8dp"         app:layout_constraintleft_toleftof="parent"         app:layout_constrainttop_totopof="parent"         android:layout_marginleft="8dp"         android:layout_marginright="8dp">         <framelayout             android:id="@+id/fl"             android:layout_margintop="60sp"             android:layout_width="match_parent"             android:layout_height="match_parent">          </framelayout>         <button             android:id="@+id/f1"              android:layout_width="100sp"             android:layout_height="50sp"             android:text="f1"             android:onclick="frag1"/>          <button             android:layout_torightof="@+id/f1"             android:id="@+id/f2"             android:layout_marginleft="30sp"             android:layout_width="100sp"             android:layout_height="50sp"             android:text="f2"            />       </relativelayout>    </android.support.constraint.constraintlayout> 

fragment1.xml:

<framelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"    >      <!-- todo: update blank fragment layout -->     <textview         android:layout_gravity="center"         android:gravity="center"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:text="fragment 1" />     <button         android:id="@+id/btn"         android:text="go"         android:layout_margin="80sp"         android:layout_width="140sp"         android:layout_height="60sp" />  </framelayout> 

fragment2.xml:

<framelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     >       <android.support.v7.widget.recyclerview         android:id="@+id/recycler_view"         android:focusable="true"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:cliptopadding="false"         android:scrollbars="vertical" /> </framelayout> 

either bind data in onpostexecute or use callbacks.

obj.execute(string.valueof(a));  /* here wont getting data */ jr=obj.getjr(); log.i("nacho",jr); 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -