-2

I have successfully parsed JSON data into ListView using ArrayList, but unable to populate List based on City name.

Like I am allowing user to select City and then l am trying to populate results into ListView using new ArrayList, but not getting any data into ListView.

public class TitleListActivity extends Activity {

  ....

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_title);

    ......
    new JSONAsyncTask().execute("http://murl.com/json/citywise.json");  

    try {
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            value = extras.getString("new_variable_name");
            Toast.makeText(TitleListActivity.this, value, Toast.LENGTH_LONG).show(); 

            // not getting data city wise
            cityWiseSearch();
        }
        else
        {
            // getting all data
            adapter = new TitleListAdapter(TitleListActivity.this, R.layout.row, allArrayList);             
            listview.setAdapter(adapter); 
        }
    } catch (Exception e) {
        // TODO: handle exception
    }


    ....
}

class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {

    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected Boolean doInBackground(String... urls) {
        try {

            ....

                JSONArray jarray = jsono.getJSONArray("cities");

                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject object = jarray.getJSONObject(i);

                    destination = new TitleList();                      
                    destination.setTitle(object.getString("address"));                      
                    allArrayList.add(destination);

                ......

        return false;
    }

    protected void onPostExecute(Boolean result) {
        dialog.cancel();
        ....
    }
}

public void cityWiseSearch() {              

    for (int i = 0; i <allArrayList.size(); i++) {
        destination = (TitleList) allArrayList.get(i);

        String strStatuss = allArrayList.get(i).getCity();

        if (strStatuss.equalsIgnoreCase(value)){                
            cityArrayList.add(destination);         
        }                      
    }              

    adapter = new TitleListAdapter(TitleListActivity.this, R.layout.row, cityArrayList);        
    listview.setAdapter(adapter);

   }
 }
Sun
  • 6,768
  • 25
  • 76
  • 131
  • 1
    if this post deserves downvotes then do, but also tell me the reason of downvoting ... – Sun Jun 30 '15 at 09:01
  • 7
    Guys please dont downvote without telling the reason, as a developer you stuck in problems hence we all use stackoverflow. – Amitabh Sarkar Jun 30 '15 at 09:05
  • 1
    first check if this "http://murl.com/json/citywise.json" returning a proper response, then check what data are you getting before binding to the arraylist. Print on log. also before using adapter.notifyDataSetChanged(); empty the ArrayList. see this http://stackoverflow.com/questions/15422120/notifydatasetchange-not-working-from-custom-adapter, and then post your output/error log. – Amitabh Sarkar Jun 30 '15 at 09:08
  • thank you @AmitabhSarkar i will be back soon... thanks found the issue – Sun Jun 30 '15 at 09:23
  • please mention the solution also, so that other can benifit from it. – Amitabh Sarkar Jun 30 '15 at 09:52
  • 2
    @AmitabhSarkar i just missed destination.setCity(object.getString("city")); due to this was not getting the city name :( – Sun Jun 30 '15 at 09:53

2 Answers2

2

Put your setAdapter code inside onPostExecute and you must need to fetch city name as you are fetching title to compare city name in cityWiseSearch() method

destination.setCity(object.getString("city")); 
Sun
  • 6,768
  • 25
  • 76
  • 131
Narendra Singh
  • 3,990
  • 5
  • 37
  • 78
0

I think your ListView is empty beceause of you update the ArrayList but you have to send allArrayList to the adapter before calling

adapter.notifyDataSetChanged();
Maxime
  • 1,332
  • 1
  • 15
  • 43
  • but i am getting all data, not getting when trying to fetch based on city name only – Sun Jun 30 '15 at 09:07
  • Ok. So you should try to add Log.v("City", strStatuss +" - "+value); just after this line : String strStatuss = allArrayList.get(i).getCity(); – Maxime Jun 30 '15 at 10:58