问题描述:

Hey everyone sorry in advance I have been looking frantically for the answer to this question. I know in theory I'm supposed to use

i.putExtra("jsonArray", jArray.toString());

in my intent but it either can't resolve or is giving me errors.

I'm trying to pass the person's name and the id from the array (the id and name from my query). and pass it so i can use the id in my next query on my next activity.

 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);

}

};

}

网友答案:

You could also implements Parcelable in your class Person to send the current instance of class Person

Or just send the person's id to the next activity with intent extra and get the Json after to compare the id:

intent.putExtra("PERSON_ID", current_person.id); 
相关阅读:
Top