0

I am creating app,in my activity I used getIntent.getstringExtra("string"),this string contain json response,in that response I have different fields which I need to display in list view,so how to do this

check this question How to display response in next activity

class AttemptLogin extends AsyncTask<String, String, String> {

    boolean failure = false;
    JSONObject jobj;
    String resultname;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Processing..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @SuppressWarnings("unused")
    @Override
    protected String doInBackground(String...args) {

        Looper.prepare();
        String userids = strtext.toString();
        String agesfrom = spfrom.getText().toString();
        String agesto=spto.getText().toString();
        String heightfroms=spheightfrom.getText().toString();
        String heighttos=spheightto.getText().toString();
        String language=splang.getText().toString();
        String religion = sprelg.getText().toString();
        String marriage=spmrgstatus.getText().toString();

        String contri=spcountry.getText().toString();
        String states=spstate.getText().toString();
        String city=spcity.getText().toString();

        System.out.println("Email : " + userids);
        System.out.println("Email : " + agesfrom);
        System.out.println("Days : " + agesto);
        System.out.println("Months : " + heightfroms);
        System.out.println("Years : " + heighttos);
        System.out.println("User : " + language);
        System.out.println("Password : " + religion);
        System.out.println("Gender : " + marriage);
        System.out.println("First NM : " + contri);
        System.out.println("Last NM : " + states);
        System.out.println("Profile : " + city);



         try {
             //Building Parameters


             List<NameValuePair> params = new ArrayList<NameValuePair>();
             params.add(new BasicNameValuePair("user_login_id", userids));
             params.add(new BasicNameValuePair("age_from", agesfrom));
             params.add(new BasicNameValuePair("age_to", agesto));
             params.add(new BasicNameValuePair("height_from", heightfroms));
             params.add(new BasicNameValuePair("height_to", heighttos));
             params.add(new BasicNameValuePair("language", language));
             params.add(new BasicNameValuePair("religion", religion));
             params.add(new BasicNameValuePair("maritalstatus", marriage));
             params.add(new BasicNameValuePair("country", contri));
             params.add(new BasicNameValuePair("state", states));
             params.add(new BasicNameValuePair("city", city));


             params.add(new BasicNameValuePair("version", "apps"));

             Log.d("request!", "starting");
             // getting product details by making HTTP request
            json = jsonParser.makeHttpRequest (
                 SEARCH_URL, "POST", params);

             //check your log for json response
             Log.d("Request attempt", json.toString());


             final String str = json.toString();

             jobj = new JSONObject(json.toString());
             final String msg = jobj.getString("searchresult");
             resultname=jobj.getString(TAG_NAME);

             return json.getString(TAG_SUCCESS);
             }catch (JSONException e) {
             e.printStackTrace();
         }
         return null;
    }

    // After completing background task Dismiss the progress dialog

    protected void onPostExecute(String file_url) {
        //dismiss the dialog once product deleted
         pDialog.dismiss();

            Intent intent=new Intent(getActivity(),SearchResults.class);
            intent.putExtra("id", strtext);

                intent.putExtra("whole",resultname);

            startActivity(intent);

}}

my nextactivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_view_searchresult);

    Id=this.getIntent().getStringExtra("id");
    System.out.println("searching id"+Id);
    results=this.getIntent().getStringExtra("whole");
    System.out.println("Results"+results);
    nomathc=(TextView)findViewById(R.id.no_match);

{"searchresult": [ {"match_detail_id":369,"profile_id":"GM686317","name":"Sonal Patel","image":"","location":"Rajkot ,Gujarat ,India","mothertongue":"Gujarati","religion":"Hindu","occupation":"Professor / Lecturer","education":"Masters - Arts/ Science/ Commerce/ Others"} , {"match_detail_id":396,"profile_id":"GM780609","name":"Hetal Trivedi","image":"","location":"Rajkot ,Gujarat ,India","mothertongue":"Gujarati","religion":"Hindu","occupation":"Administrative Professional","education":"Bachelors - Arts/ Science/ Commerce/ Others"} , {"match_detail_id":1078,"profile_id":"GM540027","name":"Shruti Dave","image":"","location":"Rajkot ,Gujarat ,India","mothertongue":"Gujarati","religion":"Hindu","occupation":"Education Professional","education":"Masters - Arts/ Science/ Commerce/ Others"} ] }

my adapter class

public class CustomAdapterSearch extends BaseAdapter{

private Context context;
private ArrayList<HashMap<String,String>> listData;
private AQuery aQuery;

private static final String TAG_NAME="name";
private static final String TAG_PROFILE="profile_id";
private static final String TAG_IMAGE="image";
private static final String TAG_CAST="cast";
private static final String TAG_AGE="age";
private static final String TAG_LOCATION="location";


public CustomAdapterSearch(Context context,ArrayList<HashMap<String,String>> listData) {
    this.context = context;
    this.listData=listData;
    aQuery = new AQuery(this.context);
}

@Override
public int getCount() {
    return listData.size();
}

@Override
public Object getItem(int position) {
    return listData.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = LayoutInflater.from(context).inflate(R.layout.list_item_searchresult,   null);
        holder.propic = (ImageView) convertView.findViewById(R.id.propicsearch);
        holder.txtproname = (TextView) convertView.findViewById(R.id.txtpronamesearch);
        holder.txtproid = (TextView) convertView.findViewById(R.id.txtproidsearch);
        holder.txtprofilecast = (TextView) convertView.findViewById(R.id.txtprofilecastsearch);
        holder.txtprofileage = (TextView) convertView.findViewById(R.id.txtprofileagesearch);
        holder.txtprofileplace = (TextView) convertView.findViewById(R.id.txtprofileplacesearch);

        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtproname.setText(listData.get(position).get(TAG_NAME));
    holder.txtproid.setText(listData.get(position).get(TAG_PROFILE));
    holder.txtprofilecast.setText(listData.get(position).get(TAG_CAST));
    holder.txtprofileage.setText(listData.get(position).get(TAG_AGE));
    holder.txtprofileplace.setText(listData.get(position).get(TAG_LOCATION));

    aQuery.id(holder.propic).image(listData.get(position).get(TAG_IMAGE),true,true,0,R.drawable.ic_launcher);

    // image parameter : 1 : memory cache,2:file cache,3:target width,4:fallback image
    return convertView;
}
class ViewHolder{
    ImageView propic;
    TextView txtproname;
    TextView txtproid;
    TextView txtprofilecast;
    TextView txtprofileage;
    TextView txtprofileplace;
}

}

Community
  • 1
  • 1
  • I suggest don't send json data through intent. Because if the length of the json is too large then it won't work. Possibly you get javaBinder error. – Amsheer Dec 09 '14 at 11:57
  • Before passing data using intent just parse the json data and add to bean. That is simply parse data and add to list just call from wherever you want – Amsheer Dec 09 '14 at 12:04
  • Simply it means parse the data and set to an static class and call from another activity. – Amsheer Dec 09 '14 at 12:06
  • `How to display intent data in listview?` ?? Why such a confusing subject? You want to use data from json text in a listview. You have retrieved the json text in your activity with intent get extra. So now continu with code for that activity. Remove the code here where you download the json as that is irrelevant now. Do not refer to your other post. You had better continued there instead of starting a new post. – greenapps Dec 09 '14 at 12:33
  • Instead you should have posted a piece of json text here. Told what data you wanted from it in the listview. Showed the code you tried. – greenapps Dec 09 '14 at 12:38

1 Answers1

0

Do you try this code?

public ArrayList<String> name = new ArrayList<>();
public ArrayList<String> description = new ArrayList<>();

try {
    JSONObject jsonObj = new JSONObject(jsonStr);
    JSONArray building = jsonObj.getJSONArray("data");
    for (int i = 0; i < building.length(); i++) {
        JSONObject c = building.getJSONObject(i);
        String namec = c.getString("title");
        String descriptionc = c.getString("description");
        name.add(namec);
        description.add(descriptionc);
    }
    } catch (JSONException e) {
    e.printStackTrace();
 }

 for (int i = 0; i < name.size(); i++) {
      la.add(new JSONList(name.get(i), description.get(i)));
 }
 lv.setAdapter(la);

jsonStr is your json string, "data" is json array name.

la is ListAdapter (Custom Adapter) , lv is ListView.

I recommand use AsyncTask to print Json to ListView.

edit:

 public ArrayList<String> user_login_id = new ArrayList<>();
 public ArrayList<String> age_from = new ArrayList<>();

in the DoInBackground

 try {
     JSONObject jsonObj = new JSONObject(json.getString(TAG_SUCCESS));
     JSONArray data = jsonObj.getJSONArray("searchresult");
     for (int i = 0; i < data.length(); i++) {
         JSONObject c = data.getJSONObject(i);
         String user_login_idc = c.getString("user_login_id");
         String age_fromc = c.getString("age_from");
         ...
         user_login_id.add(user_login_idc);
         age_from.add(age_fromc);
         ...
     }
     } catch (JSONException e) {
     e.printStackTrace();
     }

In the onPostExecute

 for (int i = 0; i < age_from.size(); i++) {
      la.add(new JSONList(user_login_id.get(i), age_from.get(i) ...));
 }
 lv.setAdapter(la);

edit:

so, you can try this code.

List.class

public class List {
Drawable Propic;
String txtproname, txtproid, txtprofilecast, txtprofileage, txtprofileplace;

public List(drawable propic, String txtproname, String txtproid, String txtprofilecast, String txtprofileage, String txtprofileplace) {
this.propic = propic;
this.txtproname = proname;
this.txtproid = proid;
this.txtprofileage = txtprofileage;
this.txtprofileplace = txtprofileplace;
}

public Drawable getpropic() { return propic;}
public String gettxtproname() { return txtproname;}
public String txtproid() { return txtproid;}
public String txtprofileage() { return txtprofileage;}
public String txtprofileplace() { return txtprofileplace;}
}

so, you can simply your adapter

final List data = this.getItem(position)
holder.txtproname.setText(data.getproname());
holder.txtproid.setText(data.gettxtproid());
holder.txtprofilecast.setText(data.gettxtprofilecast());
holder.txtprofileage.setText(data.gettxtprofileage());
holder.txtprofileplace.setText(data.gettxtprofileplace());

In the onPostExecute

 for (int i = 0; i < age_from.size(); i++) {
      la.add(new List(user_login_id.get(i), age_from.get(i) ...));
 }
 lv.setAdapter(la);
WindSekirun
  • 1,038
  • 11
  • 22
  • did you followed the question i mention in my current question? –  Dec 09 '14 at 12:12
  • did you mean "print json into listview"? if so, you can json String to JSONObject(JSONObject jsonObj = new JSONObject(jsonStr);), and convert JSONObject to JSONArray(JSONArray building = jsonObj.getJSONArray("searchresult");) – WindSekirun Dec 09 '14 at 12:18
  • i edit my question here can you edit your suggestion? –  Dec 09 '14 at 12:21
  • @Johnson, you better remove all code here as that is very confusing. You already have the json (getIntent.getstringExtra("string")) so post the code of the next activity. – greenapps Dec 09 '14 at 12:29
  • i recommand to @Johnson read this article(Custom Listview tutorial) [Link](http://www.learn2crack.com/2013/10/android-custom-listview-images-text-example.html). try it your self. – WindSekirun Dec 09 '14 at 12:40
  • What is the json text? And where do you use it? Where is the code I proposed in your earlier post? And you did not show how it is build up. Nor didn't remove irrelevant code. – greenapps Dec 09 '14 at 12:41
  • Remove your asynctask please. Which variable holds that json text? Please show the code where you make a list and instantiate the adapter. – greenapps Dec 09 '14 at 12:55
  • @greenapps and windsekirun give your mail id so that i can send whole code –  Dec 09 '14 at 13:19