Skip to content

Instantly share code, notes, and snippets.

@jesselima
Created May 17, 2018 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesselima/075b28becd24496a5ab1aa688e4d36cc to your computer and use it in GitHub Desktop.
Save jesselima/075b28becd24496a5ab1aa688e4d36cc to your computer and use it in GitHub Desktop.
package com.udacity.tourguide.fragments;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.udacity.tourguide.R;
import com.udacity.tourguide.StartupDetailsActivity;
import com.udacity.tourguide.adapters.StartupAdapter;
import com.udacity.tourguide.models.Startup;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
/**
* A simple {@link Fragment} subclass.
*/
public class StartupsFragment extends Fragment {
public StartupsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.list_view, container, false);
final ArrayList<Startup> startups = new ArrayList<Startup>();
String jsonStr = null;
try {
InputStream is = getContext().getAssets().open("startups.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
jsonStr = new String(buffer, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
try {
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray startupsJsonArray = jsonObj.getJSONArray("startups");
for (int i = 0; i < startupsJsonArray.length(); i++) {
JSONObject startupData = startupsJsonArray.getJSONObject(i);
String name = startupData.getString("name");
String description = startupData.getString("description");
int mImageResourceId = startupData.getInt("mImageResourceId");
double lat = startupData.getDouble("lat");
double lng = startupData.getDouble("lng");
int founded = startupData.getInt("founded");
String founders = startupData.getString("founders");
String industry = startupData.getString("industry");
String site = startupData.getString("site");
String address = startupData.getString("address");
Startup data = new Startup(name, description, mImageResourceId, lat, lng, founded, founders, industry, site, address);
startups.add(data);
}
} catch (JSONException e) {
e.printStackTrace();
}
StartupAdapter adapter = new StartupAdapter(getActivity(), startups, R.id.image_view_item_list);
ListView listView = rootView.findViewById(R.id.list_view);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String name = startups.get(position).getName();
String description = startups.get(position).getDescription();
Double lat = startups.get(position).getLat();
Double lng = startups.get(position).getLng();
int founded = startups.get(position).getFounded();
String founders = startups.get(position).getFounders();
String site = startups.get(position).getSite();
String industry = startups.get(position).getIndustry();
String address = startups.get(position).getAddress();
int imageResourceId = startups.get(position).getmImageResourceId();
Intent intent = new Intent(getContext(), StartupDetailsActivity.class);
intent.putExtra("name", name);
intent.putExtra("description", description);
intent.putExtra("lat", lat);
intent.putExtra("lng", lng);
intent.putExtra("founded", founded);
intent.putExtra("founders", founders);
intent.putExtra("site", site);
intent.putExtra("industry", industry);
intent.putExtra("address", address);
intent.putExtra("imageResourceId", imageResourceId);
startActivity(intent);
}
});
return rootView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment