Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
alex gwartney
8,849 PointsGetting a extended base adapter to work on main activity
So im not sure if im even doing this to the correct extent but i can not seem to get a extended adapter to just work on the main activity i don't want to pass it through a intent. Can some one look through my code and see were in the world its falling thanks. It crashes on start up it seems right after i declare setListAdapter.
public class MainActivity extends ListActivity {
public static final String TAG = MainActivity.class.getSimpleName();
private CurrentWeather[] mDays;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// mProgressBar.setVisibility(View.INVISIBLE);
final double latitude = 37.8267;
final double longitude = -122.423;
// mRefreshImageView.setOnClickListener(new View.OnClickListener() {
//@Override
// public void onClick(View v) {
// getForecast(latitude, longitude);
// }
// });
//
getForecast(latitude, longitude);
adaptting();
}
private void getForecast(double latitude, double longitude) {
String apiKey = "27974c4bc33201748eaf542a6769c3b7";
String forecastUrl = "https://api.forecast.io/forecast/" + apiKey +
"/" + latitude + "," + longitude;
if (isNetworkAvailable()) {
//toggleRefresh();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(forecastUrl)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
alertUserAboutError();
}
@Override
public void onResponse(Response response) throws IOException {
try {
String jsonData = response.body().string();
Log.v(TAG, jsonData);
if (response.isSuccessful()) {
mDays = getCurrentDetails(jsonData);
} else {
alertUserAboutError();
}
}
catch (IOException e) {
Log.e(TAG, "Exception caught: ", e);
}
catch (JSONException e) {
Log.e(TAG, "Exception caught: ", e);
}
}
});
}
else {
Toast.makeText(this, getString(R.string.network_unavailable_message),
Toast.LENGTH_LONG).show();
}
}
//private void toggleRefresh() {
// if (mProgressBar.getVisibility() == View.INVISIBLE) {
// mProgressBar.setVisibility(View.VISIBLE);
// mRefreshImageView.setVisibility(View.INVISIBLE);
// }
// else {
// mProgressBar.setVisibility(View.INVISIBLE);
// mRefreshImageView.setVisibility(View.VISIBLE);
// }
// }
//
//private void updateDisplay() {
// mTemperatureLabel.setText(mDays.getTemperature() + "");
// mTimeLabel.setText("At " + mDays.getFormattedTime() + " it will be");
// mHumidityValue.setText(mDays.getHumidity() + "");
// mPrecipValue.setText(mDays.getPrecipChance() + "%");
// mSummaryLabel.setText(mDays.getSummary());
// Drawable drawable = getResources().getDrawable(mDays.getIconId());
// mIconImageView.setImageDrawable(drawable);
// }
private CurrentWeather[] getCurrentDetails(String jsonData) throws JSONException {
JSONObject forecast = new JSONObject(jsonData);
String timezone = forecast.getString("timezone");
JSONObject hourly = forecast.getJSONObject("hourly");
JSONArray data = hourly.getJSONArray("data");
CurrentWeather[] hours = new CurrentWeather[data.length()];
for (int i= 0 ; i<data.length(); i++){
JSONObject jsonHour = data.getJSONObject(i);
CurrentWeather hour = new CurrentWeather();
hour.setSummary(jsonHour.getString("summary"));
hour.setTemperature(jsonHour.getDouble("temperature"));
hour.setIcon(jsonHour.getString("icon"));
hour.setTime(jsonHour.getLong("time"));
hour.setTimeZone(timezone);
hours[i] = hour;
}
return hours;
}
public void adaptting(){
DayAdapter adapt = new DayAdapter(this,mDays);
setListAdapter(adapt);
}
private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()) {
isAvailable = true;
}
return isAvailable;
}
private void alertUserAboutError() {
AlertDialogFragment dialog = new AlertDialogFragment();
dialog.show(getFragmentManager(), "error_dialog");
}
}
public class DayAdapter extends BaseAdapter {
private Context mContext;
private CurrentWeather[] mDays;
public DayAdapter(Context context, CurrentWeather[] days) {
mContext = context;
mDays = days;
}
@Override
public int getCount() {
return mDays.length;
}
@Override
public Object getItem(int position) {
return mDays[position];
}
@Override
public long getItemId(int position) {
return 0; // we aren't going to use this. Tag items for easy reference
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
// brand new
convertView = LayoutInflater.from(mContext).inflate(R.layout.daily_list_item, null);
holder = new ViewHolder();
holder.temperatureLabel = (TextView) convertView.findViewById(R.id.tepraturelabel);
holder.dayLabel = (TextView) convertView.findViewById(R.id.daynamelabel);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
CurrentWeather day = mDays[position];
holder.temperatureLabel.setText(day.getTemperature() + "");
return convertView;
}
private static class ViewHolder {
ImageView iconImageView; // public by default
TextView temperatureLabel;
TextView dayLabel;
}
}
2 Answers
Ryan Ruscett
23,309 PointsI would recommend you launching it in debug mode. Then set breakpoints in the suspected places. Then when the program starts it will stop on the breakpoint.
Then you can check the values of everything, and then on the listView you can step into the method and literally follow it into the extended class and watch it set and load things.
If you did it like that it would probably become a lot clearer. Sorry I don't have the actual code error but following your code but that would involve putting it into an editor and fixing it.
alex gwartney
8,849 Pointswell unfrotnaly i did this and as i stated i get the problem and the setlist adapter