我正在开发一个Android应用程序而我在我的SecondActivity中我已经将一些JSON值解析为listview,并且我想在单击特定列表项时打开ThirdActivity并将值“name”和“number”发送到ThirdActivity。
我试过在OnPostExecute方法中创建一个Intent。但我无法弄清楚如何将值传递给下一个活动。
package com.example.acer.videoapp;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class SecondActivity extends AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
private ListView listView1;
Toolbar toolbar1;
String subjectName;
ArrayList<HashMap<String, String>> lessonList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//setting title to toolbar
toolbar1 = (Toolbar) findViewById(R.id.toolbar1);
Bundle bundle = getIntent().getExtras();
if(bundle!=null) {
toolbar1.setTitle(bundle.getString("Subject"));
subjectName=toolbar1.getTitle().toString();
}
lessonList = new ArrayList<>();
listView1 = (ListView) findViewById(R.id.list);
new GetLessons().execute();
}
/* Async task class to get json by making HTTP call */
private class GetLessons extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(SecondActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String url = getResources().getString(R.string.lessons_url, subjectName);
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
// Getting JSON Array
JSONArray lessons = new JSONArray(jsonStr);
// looping through All lessons
for (int i = 0; i < lessons.length(); i++) {
JSONObject c = lessons.getJSONObject(i);
String number = c.getString("lessonNo");
String name = c.getString("lessonName");
// tmp hash map for single lesson
HashMap<String, String> lesson = new HashMap<>();
// adding each child node to HashMap key => value
lesson.put("number", number);
lesson.put("name", name);
// adding lesson to lesson list
lessonList.add(lesson);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),"Json parsing error: " + e.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),"Couldn't get json from server. Check LogCat for possible errors!",Toast.LENGTH_LONG).show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
SecondActivity.this, lessonList,R.layout.list_item, new String[]{"number", "name",}, new int[]{R.id.lnumber,R.id.lname});
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
//intent.putExtra("Lesson", listView1.getItemAtPosition(i).toString());
startActivity(intent);
}
});
}
}
}
答案 0 :(得分:1)
请你试试吗
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
HashMap<String, String> lesson = lessonList.get(i);
intent.putExtra("number", lesson.get("number"));
intent.putExtra("name", lesson.get("name"));
startActivity(intent);
}});
答案 1 :(得分:0)
之前已经提出过类似的问题。你已经完成了大部分工作,只需使用Intent.PutStringArrayListExtra,然后使用getIntent.getStringArrayListExtra在活动之间传递对象。
检查一下: