Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 894795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:27:16+00:00 2026-05-15T14:27:16+00:00

I’d like to set a ListView to data I get from a web service.

  • 0

I’d like to set a ListView to data I get from a web service. I get the data in a AsyncTask instance, but when I try to set some of my ListView attributes, it crashes (on line “lv.setVisibility(View.VISIBLE);”). Anybody can help?

thanks

public class Atable extends Activity {

    private EditText mSearch;
    private static final int ACTIVITY_EDIT=0;
    private Button mSearchButton;
    private TextView mNoresults;
    private ListView lv;
    private CheckBox checkBox;
    private LocationManager locationManager;
    private RestaurantList restaurantList;
    private Criteria criteria;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        lv= (ListView)findViewById(R.id.listview);

        mNoresults = (TextView) findViewById(R.id.noresults);  
        mNoresults.setVisibility(View.GONE);

        mSearchButton = (Button)this.findViewById(R.id.button);
        checkBox = (CheckBox) findViewById(R.id.local_check);        

        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);

        mSearchButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mNoresults.setVisibility(View.GONE);
                mSearch = (EditText) findViewById(R.id.search);  
                String tmp_str = mSearch.getText().toString().replace(" ","+");  
                String url = "http://www.atable.org/getRestaurantByQuery/?query=" + tmp_str;

                if (checkBox.isChecked()) {

                    //get location
                    String provider = locationManager.getBestProvider(criteria, true);                
                    Location location = locationManager.getLastKnownLocation(provider);

                    if (location!=null) {

                        String lat = String.valueOf(location.getLatitude());
                        String lng = String.valueOf(location.getLongitude());

                        url += "&lat="+lat+"&lng="+lng;                   
                    }
                }
                new GetRestaurantData().execute(url);               
            }
        });

    };

    private class GetRestaurantData extends AsyncTask<String, Boolean, RestaurantList> {

        private HttpClient httpclient = new DefaultHttpClient();

        @Override
        protected RestaurantList doInBackground(String... url) {            

            publishProgress(true);         

            HttpGet httpget = new HttpGet(url[0]);          

            // Execute the request
            HttpResponse response;
            try {
                response = httpclient.execute(httpget);
                HttpEntity entity = response.getEntity();

                if (entity != null) {

                    InputStream instream = entity.getContent();

                    Reader r = new InputStreamReader(instream);                      

                    Gson gson = new Gson();
                    restaurantList = gson.fromJson(r, RestaurantList.class);

                    int nResults = restaurantList.getSize();

                    if (nResults>0) {                       

                        lv.setVisibility(View.VISIBLE); //app crashes here                      

                        lv.setAdapter( new ArrayAdapter<String>(Atable.this ,android.R.layout.simple_list_item_1,restaurantList.getRestaurantNames()));

                        lv.setOnItemClickListener(new OnItemClickListener() {

                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                                Intent intent = new Intent(Atable.this, RestaurantDescription.class);
                                Restaurant tmp_resto = restaurantList.getRestaurant((int)id);

                                String tmp_categories = tmp_resto.getCategories().get(0);
                                for (int i=1; i<tmp_resto.getCategories().size(); i++) {
                                    tmp_categories+=", "+tmp_resto.getCategories().get(i);
                                }

                                String address = tmp_resto.getStreet()+", "+tmp_resto.getStreetNumber()+"\n"+tmp_resto.getCity()+
                                                " "+tmp_resto.getPostalCode()+"\n"+tmp_resto.getCountry();

                                intent.putExtra("name", tmp_resto.getName());
                                intent.putExtra("address", address);                                
                                intent.putExtra("rating", tmp_resto.getRating());
                                intent.putExtra("price_range", tmp_resto.getPriceRange());
                                intent.putExtra("categories", tmp_categories);
                                intent.putExtra("latitude", tmp_resto.getLatitude());
                                intent.putExtra("longitude", tmp_resto.getLongitude());
                                startActivityForResult(intent, ACTIVITY_EDIT);
                            }
                        });


                    }

                    else {
                        lv.setVisibility(View.GONE);
                        mNoresults.setVisibility(View.VISIBLE);
                    }

                    //Closing the input stream will trigger connection release
                    instream.close();
                }   


            } catch (ClientProtocolException e) {                   
                e.printStackTrace();
            } catch (IOException e) {                   
                e.printStackTrace();
            }

            return restaurantList;
        }

        @Override
        protected void onProgressUpdate(Boolean... progress) {
            // line below coupled with 
            //    getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS) 
            //    before setContentView 
            // will show the wait animation on the top-right corner
            Atable.this.setProgressBarIndeterminateVisibility(progress[0]);
        }

        @Override
        protected void onPostExecute(RestaurantList result) {
            publishProgress(false);
            // Do something with result in your activity
        }




    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:27:17+00:00Added an answer on May 15, 2026 at 2:27 pm

    In Android, all UI updates are done on the main thread (also called UI thread). It’s good when you spawn a new thread to do time consuming tasks in order not to block the UI.

    But in order to avoid all sorts of indeterminate behaviors, UI updates always have to be done on the UI thread. If you ever attempt to do so from a different thread, an exception will be thrown.

    In your example, I see you’re performing different updatings to ListView based on the value of nResults. You can try returning nResults for doInBackground(). It will be passed to onPostExecute(), which will be executed on the UI thread.

    You wanna check out this article for some useful information about threadings in Android: http://android-developers.blogspot.com/2009/05/painless-threading.html

    HTH

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
Seemingly simple, but I cannot find anything relevant on the web. What is the
I've got a string that has curly quotes in it. I'd like to replace
I have text I am displaying in SIlverlight that is coming from a CMS
I want to count how many characters a certain string has in PHP, but
I have a JSP page retrieving data and when single or double quotes are
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I used javascript for loading a picture on my website depending on which small
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.