1

I've been browsing to the following example:

http://www.ezzylearning.com/tutorial.aspx?tid=1763429

It's a Customizing Android ListView Items with Custom ArrayAdapter.


Everything static is working, but I don't get it dynamically:

Weather weather_data[] = new Weather[]
    {
        new Weather(R.drawable.weather_cloudy, "Cloudy"),
        new Weather(R.drawable.weather_showers, "Showers"),
        new Weather(R.drawable.weather_snow, "Snow"),
        new Weather(R.drawable.weather_storm, "Storm"),
        new Weather(R.drawable.weather_sunny, "Sunny")
    };


For dynamically data it says:

Weather weather_data[] = youservice.GetWeatherData();


Can you someone please give an example how to implement the youservice.GetWeatherData();

I've tried a lot of things, but don't manage it.

2
  • Its not android issue, but your Java concept issue. Commented Jul 4, 2012 at 14:18
  • You should read a Json parser tutorial. youservice.GetWeatherData(); is simple function return the right format input data for you adapter. In your case, it return an array of Weather. The Dynamic is far from simple answer. There're too many ways to provide input data. Commented Nov 22, 2012 at 2:32

2 Answers 2

3

I just found a way:

    Weather weather_data[] = new Weather[6] ;

    for (int  i= 0; i < 6; i++){
       weather_data[i] = new Weather("A","B");
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Example is good. When you get your array simply pass it to your custom list view adapter.

private ListView mCustomListView;

CustomListViewAdapter adapter = new CustomListViewAdapter(context, weather_data);
mCustomListView.setAdapter(adapter);
mCustomListView.requestFocus();

Adapter will have to do the rest. If you want an implementation of youservice.GetWeatherData() you need to split your question in smaller chunks. E.g.: - How to create REST Web service, - How to establish http connection and send "get" request, - how to parse response stream??

Edit 1

You don't need to replace data in your array, read Json create new array and pass it to the adapter.

List<WeatherItem> weatherList = getWeatherFromJson();
weather_data = new Weather[weatherList.size()];
weather_data = weatherList.toArray(weather_data );

// and from above

CustomListViewAdapter adapter = new CustomListViewAdapter(context, weather_data); ....

2 Comments

Tnx for the answer, but I think you don't understand my question. I need to replace each New Weather(R.drawable.weather_cloudy, "Cloudy") etc. with data I get from the web through Json. So all the fixed settings must be removed and filled with dynamical content.
Weather weather_data[] = new Weather[] { here dynamically each new Weather() }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.