android - 将 fab.onclicklistener 更改为 Intent

标签 android mysql android-intent gridview

我在更改 fab 按钮点击 Intent 时遇到问题 这是我的 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();
            }
        });
    }
}

这就是我的 Intent

 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);

            }
        });

这是我的 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;
    }
}

我想在不点击 fab 按钮的情况下显示我的 gridview,,, 因为当我删除 fab.setonclicklistener 函数时,,, 我的项目错误:无法将cardview转换为gridview,, 如何在不单击 fab 按钮的情况下获取 downloaderAlat.java 中的数据?

好的,这是我的 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>

这是我的 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>

最佳答案

根据我对您问题的理解,您希望无需单击 Fab 按钮即可进行网络调用。

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();
}
}

尝试在 AlatActivity.class 中替换上述代码

将布局文件更改为以下代码 -

<?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>

如果您需要更多帮助,请告诉我。

关于android - 将 fab.onclicklistener 更改为 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45693296/

相关文章:

android - Room (SQLite) 带有空参数的 WHERE 子句不起作用

php - 三表内联

mysql - mysql中如何将结果行改为列

android - 使用 Intent 在 Android 应用程序中共享图像将其作为文本文件而不是照片发送

android - 如何以编程方式从右到左设置 float 操作按钮重力

Android, Drawable.createFromStream(is, srcname) : what's the 2nd parameter meaning?

java - MySQL 字段类型

android - 从 NFC 标签打开时,Firebase 动态 URL 表现异常

java - 无法执行 android :onClick when trying send an image in email 的方法

android - 如何让imageview不停的旋转?