Created
October 3, 2013 17:09
-
-
Save shikajiro/6813330 to your computer and use it in GitHub Desktop.
ListViewに複雑なレイアウトを施すためのAdapterなどのカスタマイズ。
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.example.listviewsample; | |
/** | |
* カスタマイズしたリストのビューは、タイトルや本文、画像のURLなど複数のデータを保持している。 | |
* データをやりとりするためのクラス Data Transfer Object を利用すると良い。 | |
* @author shikajiro | |
* | |
*/ | |
public class ArticleDTO { | |
private String title; | |
private String content; | |
private String date; | |
private String imageUrl; | |
public String getTitle() { | |
return title; | |
} | |
public void setTitle(String title) { | |
this.title = title; | |
} | |
public String getContent() { | |
return content; | |
} | |
public void setContent(String content) { | |
this.content = content; | |
} | |
public String getDate() { | |
return date; | |
} | |
public void setDate(String date) { | |
this.date = date; | |
} | |
public String getImageUrl() { | |
return imageUrl; | |
} | |
public void setImageUrl(String imageUrl) { | |
this.imageUrl = imageUrl; | |
} | |
} |
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.example.listviewsample; | |
import java.util.List; | |
import android.content.Context; | |
import android.net.Uri; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
/** | |
* 複雑なデザインのListViewのレイアウトに対応するための | |
* 拡張されたAdapter。 | |
* ArticleDTOを扱う指定することで、複数のデータを扱うことができるようになる。 | |
* @author shikajiro | |
* | |
*/ | |
public class CustomAdapter extends ArrayAdapter<ArticleDTO> { | |
private int resource; | |
/** | |
* @param context Activityのインスタンスを指定する | |
* @param resource レイアウトファイルを指定する | |
* @param objects データが詰まったリストを指定する | |
*/ | |
public CustomAdapter(Context context, int resource, List<ArticleDTO> objects) { | |
super(context, resource, objects); | |
//リソースファイルは後で使うので、メンバ変数に保持しておく。 | |
this.resource = resource; | |
} | |
/* | |
* このメソッドは、Listに入っているデータ数分呼ばれる。 | |
* 10件のデータを設定していれば、10回呼ばれることになる。 | |
*/ | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
/*レイアウトを生成する*/ | |
//Viewをinfrate(膨らませる)ことで、レイアウトを作成する。 | |
convertView = View.inflate(getContext(), this.resource, null); | |
/*生成したViewから各パーツを取得する*/ | |
//ここから先は onCreateの処理に似ている | |
TextView titleView = (TextView) convertView.findViewById(R.id.titleView); | |
TextView contentView = (TextView) convertView.findViewById(R.id.contentView); | |
TextView dateView = (TextView) convertView.findViewById(R.id.dateView); | |
// ImageView thumbnailsImageView = (ImageView) convertView.findViewById(R.id.thumbnailsImageView); | |
//今の行のデータを取得する | |
ArticleDTO item = getItem(position); | |
//各パーツにデータを設定していく | |
titleView.setText(item.getTitle()); | |
contentView.setText(item.getContent()); | |
dateView.setText(item.getDate()); | |
// Uri uri = Uri.parse(item.getImageUrl()); | |
// thumbnailsImageView.setImageURI(uri); | |
//一所懸命作ったViewを返す。 | |
return convertView; | |
} | |
} |
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" | |
android:orientation="vertical" > | |
<ImageView | |
android:id="@+id/thumbnailsImageView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentTop="true" | |
android:layout_marginLeft="15dp" | |
android:layout_marginTop="17dp" | |
android:src="@drawable/ic_launcher" /> | |
<TextView | |
android:id="@+id/titleView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignTop="@+id/thumbnailsImageView" | |
android:layout_toRightOf="@+id/thumbnailsImageView" | |
android:text="タイトル" /> | |
<TextView | |
android:id="@+id/contentView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignBottom="@+id/thumbnailsImageView" | |
android:layout_alignLeft="@+id/titleView" | |
android:text="本文が表示される場所" /> | |
<TextView | |
android:id="@+id/dateView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentRight="true" | |
android:layout_alignParentTop="true" | |
android:text="2000/01/01" | |
android:textAppearance="?android:attr/textAppearanceSmall" /> | |
</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
package com.example.listviewsample; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import android.app.Activity; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.widget.ListView; | |
public class MainActivity extends Activity { | |
List<ArticleDTO> list = new ArrayList<ArticleDTO>(); | |
private ListView listView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
listView = (ListView) findViewById(R.id.listView1); | |
AsyncTask<Void, Integer, String> task = new AsyncTask<Void, Integer, String>() { | |
@Override | |
protected String doInBackground(Void... params) { | |
StringBuilder bld = new StringBuilder(); | |
try { | |
// URLのデータを読み込む | |
URL url = new URL( | |
"http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=http://b.hatena.ne.jp/hotentry.rss&num=10"); | |
URLConnection connection = (HttpURLConnection) url | |
.openConnection(); | |
InputStream is = connection.getInputStream(); | |
// feed.jsonを読み込む処理に似てます。 | |
BufferedReader buff = new BufferedReader( | |
new InputStreamReader(is)); | |
String line = null; | |
while ((line = buff.readLine()) != null) { | |
bld.append(line); | |
} | |
// ③文字列をjsonとして扱う | |
JSONObject json = new JSONObject(bld.toString()); | |
JSONObject responseData = json | |
.getJSONObject("responseData"); | |
JSONObject feed = responseData.getJSONObject("feed"); | |
JSONArray entries = feed.getJSONArray("entries"); | |
for (int i = 0; i < entries.length(); i++) { | |
JSONObject entry = entries.getJSONObject(i); | |
String title = entry.getString("title"); | |
String content = entry.getString("content"); | |
String date = entry.getString("publishedDate"); | |
ArticleDTO dto = new ArticleDTO(); | |
dto.setTitle(title); | |
dto.setContent(content); | |
dto.setDate(date); | |
//dto.setImageUrl("");//jsonに画像のURLがなかった・・・(´・ω・`) | |
list.add(dto); | |
} | |
} catch (MalformedURLException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (JSONException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return bld.toString(); | |
} | |
@Override | |
protected void onPostExecute(String result) { | |
super.onPostExecute(result); | |
Log.i("json", result.toString()); | |
CustomAdapter adapter = new CustomAdapter( | |
MainActivity.this, | |
R.layout.item_list, | |
list); | |
listView.setAdapter(adapter); | |
} | |
}; | |
task.execute(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment