嘿大家提前对不起我一直在疯狂地寻找这个问题的答案。我知道理论上我应该使用
i.putExtra("jsonArray", jArray.toString());
在我的意图中,但它要么无法解决,要么给我错误。
我正在尝试从数组中传递此人的姓名和 ID(我的查询中的 ID 和姓名)。并传递它,以便我可以在下一个活动的下一个查询中使用该 ID。
Person resultRow = new Person();
//set that person's attributes
resultRow.id = json_data.getString("id");
resultRow.icon = json_data.getString("birthday");
resultRow.name = json_data.getString("name");
resultRow.address = json_data.getString("address");
resultRow.city = json_data.getString("city");
//this is our arrayList object, we add our Person object to it
arrayOfWebData.add(resultRow);
}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
//get our listview
ListView myListView = (ListView)findViewById(R.id.myListView);
//we initialize our fancy adapter object, we already declared it above
//in the class definition
aa=new FancyAdapter();
// here we set the adapter, this turns it on
myListView.setAdapter(aa);
myListView.setOnItemClickListener(onListClick);
}
catch (Exception e)
{
// this is the line of code that sends a real error message to the log
Log.e("ERROR", "ERROR IN CODE: " + e.toString());
// this is the line that prints out the location in
// the code where the error occurred.
e.printStackTrace();
}
}
// here is where the magic begins
//we extend our ArrayAdapter, and use it to create custom views
//as well as execute other more complicated functions
//this time, our ArrayAdapter is an array of Persons, instead of strings like in other tutorials
class FancyAdapter extends ArrayAdapter<Person> {
FancyAdapter() {
super(ListActivity.this, android.R.layout.simple_list_item_1, arrayOfWebData);
}
public View getView(int position, View convertView,
ViewGroup parent) {
ViewHolder holder;
//we call an if statement on our view that is passed in,
//to see if it has been recycled or not. if it has been recycled,
//then it already exists and we do not need to call the inflater function
//this saves us A HUGE AMOUNT OF RESOURCES AND PROCESSING
//this is the proper way to do it
if (convertView==null) {
LayoutInflater inflater=getLayoutInflater();
convertView=inflater.inflate(R.layout.row, null);
//here is something new. we are using a class called a view holder
holder=new ViewHolder(convertView);
//we are using that class to cache the result of the findViewById function
//which we then store in a tag on the view
convertView.setTag(holder);
}
else {
holder=(ViewHolder)convertView.getTag();
}
holder.populateFrom(arrayOfWebData.get(position));
return(convertView);
}
}
class ViewHolder {
public TextView name=null;
public TextView address=null;
public TextView icon=null;
ViewHolder(View row) {
name=(TextView)row.findViewById(R.id.name);
address=(TextView)row.findViewById(R.id.birthday);
birthday=(TextView)row.findViewById(R.id.favorite_color);
}
//notice we had to change our populate from to take an arguement of type person
void populateFrom(Person r) {
name.setText(r.name);
address.setText(r.address);
icon.setText(r.birthday);
}
}
private AdapterView.OnItemClickListener onListClick=new android.widget.AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i= new Intent(ListActivity.this, bar.class);
i.putExtra("jsonArray", Person(name).toString());
startActivity(i);
}
};
}