Last active
August 29, 2015 14:12
-
-
Save huewu/5ef113181ba024dd2bb0 to your computer and use it in GitHub Desktop.
한국에서 진행중인 Udacity Mini Codelab 의 예제 코드입니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class CourseListRequestTask extends AsyncTask<Void, Void, String> { | |
private static final String TAG = "CourseListRequestTask"; | |
@Override | |
protected String doInBackground(Void... params) { | |
try { | |
URL url = new URL("https://www.udacity.com/public-api/v0/courses"); | |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
connection.connect(); | |
InputStream stream = connection.getInputStream(); | |
InputStreamReader reader = new InputStreamReader(stream); | |
Gson gson = new Gson(); | |
Courses courses = gson.fromJson(reader, Courses.class); | |
Log.v(TAG, "The length of course list: " + courses.courses.size()); | |
Log.v(TAG, "The title of the first course:" + courses.courses.get(0).title); | |
Log.v(TAG, "The teaser video url of the first course:" | |
+ courses.courses.get(0).teaser_video.youtube_url); | |
reader.close(); | |
stream.close(); | |
} catch (MalformedURLException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Courses { | |
public ArrayList<Course> courses; | |
public static class TeaserVideo { | |
public String youtube_url; | |
@Override | |
public String toString() { | |
return youtube_url; | |
} | |
} | |
public static class Course { | |
public String key; | |
public String title; | |
public String subtitle; | |
public String summary; | |
public String image; | |
public String homepage; | |
public TeaserVideo teaser_video; | |
@Override | |
public String toString() { | |
return String.format("[%s] %s", key, title); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void onItemSelected(Courses.Course course) { | |
Bundle arguments = new Bundle(); | |
arguments.putString(String.valueOf(R.id.course_detail_title), course.title); | |
arguments.putString(String.valueOf(R.id.course_detail_subtitle), course.subtitle); | |
arguments.putString(String.valueOf(R.id.course_detail_summary), course.summary); | |
arguments.putString(String.valueOf(R.id.course_detail_image), course.image); | |
arguments.putString(String.valueOf(R.id.course_detail_teaser_video), course.teaser_video.youtube_url); | |
if (mTwoPane) { | |
// In two-pane mode, show the detail view in this activity by | |
// adding or replacing the detail fragment using a | |
// fragment transaction. | |
CourseDetailFragment fragment = new CourseDetailFragment(); | |
fragment.setArguments(arguments); | |
getSupportFragmentManager().beginTransaction() | |
.replace(R.id.course_detail_container, fragment) | |
.commit(); | |
} else { | |
// In single-pane mode, simply start the detail activity | |
// for the selected item ID. | |
Intent detailIntent = new Intent(this, CourseDetailActivity.class); | |
detailIntent.putExtras(arguments); | |
startActivity(detailIntent); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
View view; | |
if (convertView == null) { | |
view = LayoutInflater.from(getContext()).inflate( | |
R.layout.course_list_item, parent, false); | |
} else { | |
view = convertView; | |
} | |
Courses.Course course = getItem(position); | |
ImageView iv = (ImageView) view.findViewById(R.id.course_image); | |
Glide.with(getContext()) | |
.fromString() | |
.load(course.image) | |
.fitCenter() | |
.error(R.drawable.ic_launcher) | |
.into(iv); | |
TextView titleView = (TextView) view.findViewById(R.id.course_title); | |
TextView subtitleView = (TextView) view.findViewById(R.id.course_subtitle); | |
titleView.setText(course.title); | |
subtitleView.setText(course.subtitle); | |
return view; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool :D