Android GSON with Custom ListView Tutorial , Article Link: http://devahoy.com/2014/05/android-custom-listview-with-gson-tutorial/
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
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<ListView | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_margin="10dp" | |
android:dividerHeight="2dp" | |
android:id="@+id/listView" /> | |
</RelativeLayout> |
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
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.devahoy.sample.ahoygson" > | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<activity | |
android:name="com.devahoy.sample.ahoygson.MainActivity" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
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
package com.devahoy.sample.ahoygson; | |
import com.google.gson.annotations.SerializedName; | |
import java.util.List; | |
public class Blog { | |
String status; | |
int count; | |
@SerializedName("count_total") | |
int totalCount; | |
int pages; | |
List<Post> posts; | |
public String getStatus() { | |
return status; | |
} | |
public void setStatus(String status) { | |
this.status = status; | |
} | |
public int getCount() { | |
return count; | |
} | |
public void setCount(int count) { | |
this.count = count; | |
} | |
public int getTotalCount() { | |
return totalCount; | |
} | |
public void setTotalCount(int totalCount) { | |
this.totalCount = totalCount; | |
} | |
public int getPages() { | |
return pages; | |
} | |
public void setPages(int pages) { | |
this.pages = pages; | |
} | |
public List<Post> getPosts() { | |
return posts; | |
} | |
public void setPosts(List<Post> posts) { | |
this.posts = posts; | |
} | |
} |
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
apply plugin: 'android' | |
android { | |
compileSdkVersion 19 | |
buildToolsVersion "19.0.3" | |
defaultConfig { | |
minSdkVersion 10 | |
targetSdkVersion 18 | |
versionCode 1 | |
versionName "1.0" | |
} | |
buildTypes { | |
release { | |
runProguard false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' | |
} | |
} | |
} | |
dependencies { | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
compile 'com.android.support:appcompat-v7:19.+' | |
compile 'com.google.code.gson:gson:2.2.4' | |
} |
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
package com.devahoy.sample.ahoygson; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.os.AsyncTask; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import java.io.IOException; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.List; | |
public class CustomAdapter extends BaseAdapter { | |
private LayoutInflater mInflater; | |
private List<Post> mPosts; | |
private ViewHolder mViewHolder; | |
private Bitmap mBitmap; | |
private Post mPost; | |
private Activity mActivity; | |
public CustomAdapter(Activity activity, List<Post> posts) { | |
mInflater = (LayoutInflater) activity.getSystemService( | |
Context.LAYOUT_INFLATER_SERVICE); | |
mPosts = posts; | |
mActivity = activity; | |
} | |
@Override | |
public int getCount() { | |
return mPosts.size(); | |
} | |
@Override | |
public Object getItem(int position) { | |
return mPosts.get(position); | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
if (convertView == null) { | |
convertView = mInflater.inflate(R.layout.post, parent, false); | |
mViewHolder = new ViewHolder(); | |
mViewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.post_thumbnail); | |
mViewHolder.author = (TextView) convertView.findViewById(R.id.post_author); | |
mViewHolder.title = (TextView) convertView.findViewById(R.id.post_title); | |
mViewHolder.date = (TextView) convertView.findViewById(R.id.post_date); | |
convertView.setTag(mViewHolder); | |
} else { | |
mViewHolder = (ViewHolder) convertView.getTag(); | |
} | |
mPost = mPosts.get(position); | |
if (mPost.getThumbnail() != null) { | |
new AsyncTask<Void, Void, Void>() { | |
@Override | |
protected Void doInBackground(Void... params) { | |
try { | |
URL url = new URL(mPost.getThumbnail()); | |
mBitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream()); | |
} catch (MalformedURLException e) { | |
} catch (IOException e) { | |
} | |
return null; | |
} | |
}.execute(); | |
mViewHolder.thumbnail.setImageBitmap(mBitmap); | |
} | |
// ถ้าใช้ Picasso ก็ uncomment ข้างล้างนี้ แล้วลบ AsyncTask ออก | |
// Picasso.with(mActivity) | |
// .load(mPost.getThumbnail()) | |
// .into(mViewHolder.thumbnail); | |
mViewHolder.author.setText(mPost.getAuthor()); | |
mViewHolder.title.setText(mPost.title); | |
mViewHolder.date.setText(mPost.date); | |
return convertView; | |
} | |
private static class ViewHolder { | |
ImageView thumbnail; | |
TextView title; | |
TextView author; | |
TextView date; | |
} | |
} |
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
package com.devahoy.sample.ahoygson; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.support.v7.app.ActionBarActivity; | |
import android.widget.ListView; | |
import com.google.gson.Gson; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.client.ClientProtocolException; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.util.List; | |
public class MainActivity extends ActionBarActivity { | |
public static final String URL = | |
"http://blog.teamtreehouse.com/api/get_recent_summary/"; | |
private ListView mListView; | |
private CustomAdapter mAdapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mListView = (ListView) findViewById(R.id.listView); | |
new SimpleTask().execute(URL); | |
} | |
private class SimpleTask extends AsyncTask<String, Void, String> { | |
@Override | |
protected void onPreExecute() { | |
// Create Show ProgressBar | |
} | |
protected String doInBackground(String... urls) { | |
String result = ""; | |
try { | |
HttpGet httpGet = new HttpGet(urls[0]); | |
HttpClient client = new DefaultHttpClient(); | |
HttpResponse response = client.execute(httpGet); | |
int statusCode = response.getStatusLine().getStatusCode(); | |
if (statusCode == 200) { | |
InputStream inputStream = response.getEntity().getContent(); | |
BufferedReader reader = new BufferedReader | |
(new InputStreamReader(inputStream)); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
result += line; | |
} | |
} | |
} catch (ClientProtocolException e) { | |
} catch (IOException e) { | |
} | |
return result; | |
} | |
protected void onPostExecute(String jsonString) { | |
// Dismiss ProgressBar | |
showData(jsonString); | |
} | |
} | |
private void showData(String jsonString) { | |
Gson gson = new Gson(); | |
Blog blog = gson.fromJson(jsonString, Blog.class); | |
List<Post> posts = blog.getPosts(); | |
mAdapter = new CustomAdapter(this, posts); | |
mListView.setAdapter(mAdapter); | |
} | |
} |
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
package com.devahoy.sample.ahoygson; | |
public class Post { | |
int id; | |
String url; | |
String title; | |
String date; | |
String author; | |
String thumbnail; | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getUrl() { | |
return url; | |
} | |
public void setUrl(String url) { | |
this.url = url; | |
} | |
public String getTitle() { | |
return title; | |
} | |
public void setTitle(String title) { | |
this.title = title; | |
} | |
public String getDate() { | |
return date; | |
} | |
public void setDate(String date) { | |
this.date = date; | |
} | |
public String getAuthor() { | |
return author; | |
} | |
public void setAuthor(String author) { | |
this.author = author; | |
} | |
public String getThumbnail() { | |
return thumbnail; | |
} | |
public void setThumbnail(String thumbnail) { | |
this.thumbnail = thumbnail; | |
} | |
} |
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
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<ImageView | |
android:layout_width="64dp" | |
android:layout_height="64dp" | |
android:layout_marginRight="8dp" | |
android:id="@+id/post_thumbnail" | |
android:src="@drawable/ic_launcher" | |
android:layout_alignParentTop="true" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentStart="true"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Title" | |
android:textSize="16sp" | |
android:textStyle="bold" | |
android:layout_marginTop="8dp" | |
android:id="@+id/post_title" | |
android:layout_alignParentTop="true" | |
android:layout_toRightOf="@+id/post_thumbnail"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Author Name" | |
android:id="@+id/post_author" | |
android:layout_marginTop="8dp" | |
android:layout_below="@+id/post_title" | |
android:layout_alignLeft="@+id/post_title" | |
android:layout_alignStart="@+id/post_title"/> | |
<TextView android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Date" | |
android:id="@+id/post_date" | |
android:layout_marginRight="8dp" | |
android:layout_alignTop="@+id/post_author" | |
android:layout_alignParentRight="true" | |
android:layout_alignParentEnd="true"/> | |
</RelativeLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment