Issue
i have problem on change fab button click to intent this is my AlatActivity.java
public class AlatActivity extends AppCompatActivity {
static String urlAddress="https://marimuncak.000webhostapp.com/getAlat.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alat_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final GridView gv= (GridView) findViewById(R.id.gv);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new DownloaderAlat(AlatActivity.this,urlAddress ,gv).execute();
}
});
}
}
and this is my intent in
ImageView favBtn = (ImageView) findViewById(R.id.favBtn);
favBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, AlatActivity.class);
startActivity(i);
}
});
and this is my DownloaderAlat.Java
public class DownloaderAlat extends AsyncTask<Void,Void,String> {
Context c;
String urlAddress;
GridView gv;
ProgressDialog pd;
public DownloaderAlat(Context c, String urlAddress, GridView gv) {
this.c = c;
this.urlAddress = urlAddress;
this.gv = gv;
}
@Override
public void onPreExecute(){
super.onPreExecute();
pd = new ProgressDialog(c);
pd.setTitle("Loading Data Alat");
pd.setMessage("Mohon Tunggu....");
pd.show();
}
@Override
protected String doInBackground(Void... params) {
return this.downloadData();
}
@Override
public void onPostExecute(String jsonData){
super.onPostExecute(jsonData);
pd.dismiss();
if (jsonData==null){
Toast.makeText(c,"gagal load data",Toast.LENGTH_SHORT).show();
}else {
ParserAlat parserAlat= new ParserAlat(c, jsonData,gv);
parserAlat.execute();
}
}
private String downloadData(){
HttpURLConnection con= KonektorAlat.connect(urlAddress);
if(con==null){
return null;
}
try {
InputStream is= new BufferedInputStream(con.getInputStream());
BufferedReader br= new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer jsonData= new StringBuffer();
while ((line=br.readLine()) !=null)
{
jsonData.append(line+"\n");
}
br.close();
is.close();
return jsonData.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
i want to show my gridview without click fab button,,, because when i remove fab.setonclicklistener function,,, my project error: cannot convert cardview into gridview,, how i can get my data in downloaderAlat.java without click fab button?
okay this is my alat_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
tools:context=".AlatActivity"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_alat"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@android:drawable/ic_dialog_email">
</android.support.design.widget.FloatingActionButton>
</android.support.design.widget.CoordinatorLayout>
and this is my content_alat.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
tools:context=".AlatActivity"
>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.CardView
android:id="@+id/gv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="2"
></android.support.v7.widget.CardView>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
Solution
From what I've understood your question, you want to make the network call without having to click on the Fab button.
public class AlatActivity extends AppCompatActivity {
static String urlAddress="https://marimuncak.000webhostapp.com/getAlat.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alat_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
GridView gv= (GridView) findViewById(R.id.gv);
new DownloaderAlat(AlatActivity.this,urlAddress ,gv).execute();
}
}
Try replacing the above code in your AlatActivity.class
Change your Layout file to below code -
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
tools:context=".AlatActivity"
>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<GridView
android:id="@+id/gv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
></GridView>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
Let me know if you need more help.
Answered By - hsm59
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.