Android Fragment向另一个Activity传值

1.Fragment内:

Intent intent=new Intent(getActivity(),ShowDataActivity.class); //参数1:Fragment所依存的Activity,参数2:要跳转的Activity
Bundle bundle = new Bundle();
bundle.putString("queryresult",queryResultStr); //放入所需要传递的值
intent.putExtras(bundle);
getActivity().startActivity(intent); //这里一定要获取到所在Activity再startActivity();

2.ShowDataActivity.class

package com.example.lucky.zjzyhq3;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.bin.david.form.core.SmartTable;
import com.bin.david.form.data.table.MapTableData;

public class ShowDataActivity extends AppCompatActivity {
    SmartTable table;
    Button btSD_exit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_data);
        //获取从MyFragment_basicMessage中传过来的值
        Intent intent=getIntent();
        String queryResultStr=intent.getStringExtra("queryresult");

        table =findViewById(R.id.table);
        btSD_exit=findViewById(R.id.btSD_exit);
        if(queryResultStr.equals("[]")){
            Toast.makeText(ShowDataActivity.this,"无匹配查询结果",Toast.LENGTH_LONG).show();
        }else {
            MapTableData tableData = MapTableData.create("表格名",JsonHelper.jsonToMapList(queryResultStr));
            table.setTableData(tableData);
        }
        btSD_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1=new Intent(ShowDataActivity.this,MainScreenActivity.class);
                startActivity(intent1);
            }
        });
        System.out.println(queryResultStr);
    }
}

 

Android Fragment向另一个Activity传值

上一篇:利用httpclient和多线程刷访问量代码


下一篇:Android OpenCV集成摄像头图片动态识别车牌号