如何将数据从活动传递到Android上的其他活动

时间:2016-06-29 00:16:09

标签: java android

我想将数据从我的登录页面传递到我的详细信息页面。但是,详细信息页面无法接受数据,因为登录页面中发送的字符串在详细信息页面上具有空值。

这是我的登录页码:

public class MainActivity extends AppCompatActivity {

EditText editText, editText1;
Button button;
int success = 0;
ProgressDialog progressDialog;
JSONObject jsonObject;
HTTPURLConnection service;
String strname ="", strpass="";
String response;
String path = "http://sumbanggagasan.890m.com/select2.php";
Intent intent;
DetailGagasanku detailGagasanku;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editText = (EditText) findViewById(R.id.nik);
    editText1 = (EditText) findViewById(R.id.pass);
    button = (Button) findViewById(R.id.signin);

    service = new HTTPURLConnection();

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!editText.getText().toString().equals("") && !editText1.getText().toString().equals("")){
                strname = editText.getText().toString();
                strpass = editText1.getText().toString();
                response = null;
                new PostDataTOServer().execute();
            } else{
                Toast.makeText(getApplicationContext(), "Please Enter all fields", Toast.LENGTH_LONG).show();
            }
        }
    });
}

private class PostDataTOServer extends AsyncTask<Void, Void, Void> {
    //Create hashmap Object to send parameters to web service
    HashMap<String, String> postDataParams;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setMessage("Please wait...");
        progressDialog.setCancelable(false);
        progressDialog.show();
    }
    @Override
    protected Void doInBackground(Void... arg0) {
        postDataParams=new HashMap<String, String>();
        postDataParams.put("NIK", strname);
        postDataParams.put("pass", strpass);
        //Call ServerData() method to call webservice and store result in response
        response= service.ServerData(path,postDataParams);
        try {
            System.out.println(response + "menu");
            jsonObject = new JSONObject(response);
            //Get Values from JSONobject
            System.out.println("success=" + jsonObject.get("successs"));
            //success = jsonObject.getInt("success");
            success = Integer.parseInt(jsonObject.getString("successs").trim());
            System.out.println("Do in");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        System.out.println("Post 1");
        System.out.println(strname);
        if (progressDialog.isShowing()) {
            System.out.println("Post 2");
            System.out.println(strpass);
            progressDialog.dismiss();
        }
        if(success==1) {
            //Toast.makeText(getApplicationContext(), "Berhasil", Toast.LENGTH_LONG).show();
            Intent intent;
            Bundle b;
            b = new Bundle();
            b.putString("username", strname);
            intent = new Intent(getApplicationContext(), LandingPage.class);
            intent.putExtras(b);
            startActivity(intent);
        } else{
            Toast.makeText(getApplicationContext(), "Login gagal", Toast.LENGTH_LONG).show();
        }

    }
}

这是我的详细信息代码:

public class DetailGagasanku extends AppCompatActivity {
    TextView judul, manfaat;
    Bundle bundle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail_gagasanku);

    judul = (TextView)findViewById(R.id.textJudul);
    manfaat = (TextView) findViewById(R.id.textManfaat);

    bundle = getIntent().getExtras();

    judul.setText(bundle.getString("judul_gagasan"));

    if (bundle != null) {
        if(bundle.containsKey("username"))
        {
            String s = bundle.getString("username");
            manfaat.setText(s);
        }
        else
        {
            System.out.println("not send");
        }
    }
}
}

我想问为什么该用户名具有空值。为了您的信息,&#34; judul_gagasan&#34;变量可以从另一个活动接收值。我从我的适配器发送它。

这是我的适配器代码:

public class GagasanAdapter extends RecyclerView.Adapter<GagasanAdapter.GagasanHolder> {

List<String> gagasanList = new ArrayList<>();

public GagasanAdapter(List<String> gagasanList) {
    this.gagasanList = gagasanList;
    Log.v("gagasanSize", "" + gagasanList.size());
}

@Override
public GagasanHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_gagasan, parent, false);
    return new GagasanHolder(v);
}

@Override
public void onBindViewHolder(final GagasanHolder holder, final int position) {

    Log.v("Gagasan[" + position + "]", gagasanList.get(position));
    String item = gagasanList.get(position);
    holder.judulGagasan.setText(item);

    holder.judulGagasan.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Context context = v.getContext();
            Intent intent = new Intent(context, DetailGagasanku.class);
            intent.putExtra("judul_gagasan", holder.judulGagasan.getText().toString());
            context.startActivity(intent);
        }
    });
}

@Override
public int getItemCount() {
    return gagasanList.size();
}

public class GagasanHolder extends RecyclerView.ViewHolder{

    TextView judulGagasan;

    public GagasanHolder(View itemView) {
        super(itemView);
        judulGagasan = (TextView) itemView.findViewById(R.id.tvListGagasan);
    }
}
}

1 个答案:

答案 0 :(得分:0)

你通过&#34;用户名&#34;在Bundle to LandingPage活动中,您无法访问DetailGagasanku活动。

所以要访问&#34;用户名&#34;在DetailGagasanku Activity中,从适配器传递用户名。