简体   繁体   中英

How do i pass value from one activity to another activity

How do i pass the value which is the subjetfile.getSubjectID from the ListView to another activity which is SubjectActivity, so that $id=$_GET['subjectID']; in my php code won't be null? Anyone can help? I have a list of subject which place inside a listView, i would like to pass it to SubjectActivity when i click any item in the listView, and the activity display the detail of the subject, but the php code seems not working because of the $id. If anyone can help i will be appreciate, thanks!

 public class SubjectActivity extends AppCompatActivity { private ImageView imageViewDefault; private TextView textViewDefaultName; private TextView textViewDefaultDescription1; private TextView textViewDefaultDescription2; private static final String GET_URL_SUBJECT = "https://arlearn.000webhostapp.com/getSubject.php"; private static final int TAG1 = 1; private ProgressDialog pDialog; private subjectFile subjectfile; private String result = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_subject); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); imageViewDefault = (ImageView)findViewById(R.id.imageViewSubject); textViewDefaultName = (TextView)findViewById(R.id.textViewName); textViewDefaultDescription1 = (TextView)findViewById(R.id.textViewContent1); textViewDefaultDescription2 = (TextView)findViewById(R.id.textViewContent2); pDialog = new ProgressDialog(this); } public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.subject, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if(id == R.id.action_refresh) { getSubjectContent(getApplicationContext(), GET_URL_SUBJECT); } return super.onOptionsItemSelected(item); } private void getSubjectContent(Context context, String url) { RequestQueue request = Volley.newRequestQueue(context); if (!pDialog.isShowing()) pDialog.setMessage("Syn with server..."); pDialog.show(); JsonArrayRequest requestsubject = new JsonArrayRequest(url, new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { try { for (int i = 0; i < response.length(); i++) { JSONObject subjectResponse = (JSONObject) response.get(i); String name = subjectResponse.getString("name"); String image = subjectResponse.getString("image"); String description1 = subjectResponse.getString("Description1"); String description2 = subjectResponse.getString("Description2"); subjectfile = new subjectFile(name, image, description1, description2); } loadFile(); if (pDialog.isShowing()) pDialog.dismiss(); }catch(Exception e) { Toast.makeText(getApplicationContext(), "Error:" + e.getMessage(), Toast.LENGTH_LONG).show(); e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.i("tagconvertstr", "["+result+"]"); Toast.makeText(getApplicationContext(), "Error" + error.getMessage(), Toast.LENGTH_LONG).show(); error.printStackTrace(); if (pDialog.isShowing()) pDialog.dismiss(); } } ); request.add(requestsubject); } private void loadFile() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] imageBytes = baos.toByteArray(); imageBytes = Base64.decode(subjectfile.getImage(), Base64.DEFAULT); Bitmap decodeImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length); imageViewDefault.setImageBitmap(decodeImage); textViewDefaultName.setText(subjectfile.getName()); textViewDefaultDescription1.setText(subjectfile.getDescription1()); textViewDefaultDescription2.setText(subjectfile.getDescription2()); } } 
 <?php include("connection1.php"); // connecting to db $conn = mysqli_connect($hostname_localhost, $username_localhost, $password_localhost, $database_localhost); /* check connection */ if (mysqli_connect_errno()) { echo "Error: Connect failed: %s\\n"; exit(); } $response = array(); $id=$_GET['subjectID']; $query = "SELECT image, name, Description1, Description2 from subject WHERE subjectID = ".$id; /* Select queries return a resultset */ if ($result = mysqli_query($conn, $query)) { $response = array(); while ($row = mysqli_fetch_array($result)) { $item = array(); $item["image"] = base64_encode($row["image"]); $item["name"] = $row["name"]; $item["Description1"] = $row["Description1"]; $item["Description2"] = $row["Description2"]; array_push($response, $item); } mysqli_free_result($result); } else{ $response["success"] = 0; $response["message"] = "Required field(s) is missing"; } echo json_encode($response); /* close connection */ mysqli_close($conn); ?> 
 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { subjectFile subjectfile = imageAdapter.getItem(position); Log.i("Database", subjectfile.getSubjectID()); Intent intent = new Intent(getActivity(), SubjectActivity.class); startActivity(intent); } }); 

Follow it -

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                subjectFile subjectfile = imageAdapter.getItem(position);
                Log.i("Database", subjectfile.getSubjectID());
                Intent intent = new Intent(getActivity(), SubjectActivity.class);
intent.putExtra("subject_id", subjectfile.getSubjectID());
startActivity(intent);
            }
        });

Access that intent on SubjectActivity activity's onCreate() method.

String subjectId = getIntent().getStringExtra("subject_id");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM