这显示了listview上相同活动的数据,但我想在第二个活动上显示数据,我如何将JSON对象传递给另一个类。
这是代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static final String DATA_URL = ; public static final String KEY_NAME = "Name";
public static final String KEY_CITY = "City";
public static final String KEY_AGE = "Age";
public static final String KEY_GENDER = "Gender";
public static final String KEY_ADDRESS = "Address";
public static final String KEY_BLOOD = "Blood_Group";
public static final String KEY_CONTACT = "contact_no";
public static final String JSON_ARRAY = "result";
private EditText editText;
private Button buttonGet;
ListView list;
private ProgressDialog loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
buttonGet = (Button) findViewById(R.id.buttonGet);
buttonGet.setOnClickListener(this);
}
private void getData() {
String b = editText.getText().toString().trim();
if (b.equals("")) {
Toast.makeText(this, "Please enter name", Toast.LENGTH_LONG).show();
return;
}
loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
String url = DATA_URL+editText.getText().toString().trim();
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response){
String[] Name;
String[] Blood_Group;
String[] Gender;
String[] contact_no;
String[] City;
String[] Age;
String[] Address;
int i;
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(JSON_ARRAY);
Name = new String[result.length()];
Blood_Group = new String[result.length()];
Address = new String[result.length()];
City = new String[result.length()];
contact_no = new String[result.length()];
Age = new String[result.length()];
Gender = new String[result.length()];
for(i = 0 ; i< result.length(); i++) {
JSONObject employee_data = result.getJSONObject(i);
Blood_Group[i] = employee_data.getString(KEY_BLOOD);
Name[i] = employee_data.getString(KEY_NAME);
contact_no[i] = employee_data.getString(KEY_CONTACT);
Address[i]= employee_data.getString(KEY_ADDRESS);
Age[i] = employee_data.getString(KEY_AGE);
Gender[i] = employee_data.getString(KEY_GENDER);
City[i]= employee_data.getString(KEY_CITY);
ListView list = (ListView) findViewById(R.id.listView);
CustomList cl = new CustomList(this, Name ,Blood_Group,City,contact_no,Address,Age,Gender);
list.setAdapter(cl);
}
} catch (JSONException e) {
Toast.makeText(this,"Sorry",Toast.LENGTH_SHORT).show();
}
Toast.makeText(this,"Show",Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View v) {
getData();
}
}
自定义LIST.java
public class CustomList extends ArrayAdapter<String> {
private Activity context;
private String[] Name;
private String[] Blood_Group;
private String[] Gender;
private String[] contact_no;
private String[] City;
private String[] Age;
private String[] Address;
public CustomList(Activity context, String[] Name, String[] Blood_Group, String[] Gender, String[] contant_no, String[] City, String[] Address, String[] Age) {
super(context, R.layout.list_item, Blood_Group);
this.context = context;
this.Name = Name;
this.Blood_Group = Blood_Group;
this.Age = Age;
this.City = City;
this.contact_no = contant_no;
this.Gender = Gender;
this.Address =Address;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.list_item, null, true);
TextView textViewName = (TextView) listViewItem.findViewById(R.id.tvListname);
TextView textViewAge = (TextView) listViewItem.findViewById(R.id.tvListage);
TextView textViewBlood = (TextView) listViewItem.findViewById(R.id.tvListbloodgroup);
TextView textViewGender = (TextView) listViewItem.findViewById(R.id.tvListgender);
TextView textViewContact_No = (TextView) listViewItem.findViewById(R.id.tvListcontact);
TextView textViewCity = (TextView) listViewItem.findViewById(R.id.tvListcity);
TextView textViewAddress = (TextView) listViewItem.findViewById(R.id.tvListaddress);
textViewName.setText("Name"+Name[position]);
textViewAge.setText(Age[position]);
textViewBlood.setText(Blood_Group[position]);
textViewGender.setText(Gender[position]);
textViewContact_No.setText(contact_no[position]);
textViewCity.setText(City[position]);
textViewAddress.setText(Address[position]);
return listViewItem;
}
}
答案 0 :(得分:1)
您可以从JSON对象创建一个字符串,并通过意图
传递字符串Intent intent = new Intent(MainActivity.this,DetailedActivity.class);
intent.putExtra("json",jsonObject.toString());
startActivity(intent);
在详细活动中,你可以从intent extra中提取字符串,并从该字符串创建一个JSON对象,如下所示
String fromIntentExtra = getIntent().getStringExtra("json");
JSONObject jsonObject = new JSONObject(intentExtra);
这将满足您的目的,但这不是一个好的实践