如何将来自ListView的subjetfile.getSubjectID值传递给另一个为SubjectActivity的活动,以便$ id = $ _ GET [' subjectID'];在我的PHP代码赢了不是?有人可以帮忙吗?我有一个listView里面的主题列表,当我点击listView中的任何项目时,我想将它传递给SubjectActivity,并且活动显示主题的细节,但是由于$,php代码似乎无法正常工作ID。如果有人可以提供帮助,我将不胜感激,谢谢!
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());
}
}
&#13;
<?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);
?>
&#13;
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);
}
});
&#13;
答案 0 :(得分:2)
跟着它 -
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);
}
});
使用SubjectActivity
活动的onCreate()
方法访问该意图。
String subjectId = getIntent().getStringExtra("subject_id");