Adapterについて勉強してみた2
前回の続き・・・
本当はTwitterからデータを取ってきて的な事を考えていましたが、まとめるのが面倒なのでAdapterだけに
機能を絞りました。
早速解説
list_rowの作成:
layaot/list_row.xml
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight" android:padding="6dip" > <ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="6dip" android:src="@drawable/ic_launcher" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical" > <TextView android:id="@+id/toptext" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:gravity="center_vertical" /> <TextView android:id="@+id/bottomtext" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:ellipsize="marquee" android:singleLine="true" /> </LinearLayout> </LinearLayout>
AdapterSampleActivity.java:mainクラス
public class AdapterSampleActivity extends ListActivity { private ArrayList<TestStatus> list = null; private TestAdapter adapter = null; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); createDummyData(); adapter = new TestAdapter(this, R.layout.list_row, list); //アダプターにデータを渡す setListAdapter(adapter); } //面倒だがデータ1件づつ格納クラスにsetする private void createDummyData(){ this.list = new ArrayList<TestStatus>(); TestStatus item1 = new TestStatus(); item1.setScreenName("ゆるゆり"); item1.setText("OP:ゆりゆららららゆるゆり大事件"); list.add(item1); TestStatus item2 = new TestStatus(); item2.setScreenName("君と僕"); item2.setText("OP:バイバイ"); list.add(item2); TestStatus item3 = new TestStatus(); item3.setScreenName("WORKING´!!"); item3.setText("OP:COOLISH WALK"); list.add(item3); TestStatus item4 = new TestStatus(); item4.setScreenName("探偵オペラ ミルキィホームズ"); item4.setText("OP:正解はひとつ!じゃない!!"); list.add(item4); TestStatus item5 = new TestStatus(); item5.setScreenName("迷い猫オーバーラン!"); item5.setText("OP:はっぴぃ にゅうにゃあ"); list.add(item5); TestStatus item6 = new TestStatus(); item6.setScreenName("みつどもえ"); item6.setText("OP:みっつ数えて大集合!"); list.add(item6); } }
TestStatus.java:データを格納するクラス
public class TestStatus { private String screenName; private String text; public String getScreenName() { return screenName; } public void setScreenName(String screenName) { this.screenName = screenName; } public String getText() { return text; } public void setText(String text) { this.text = text; } }
TestAdapter.java:今回のメインとなるクラス
AdapterはArrayAdapterクラスを継承して使う。
public class TestAdapter extends ArrayAdapter<TestStatus> { private ArrayList<TestStatus> items; private LayoutInflater inflater; private Context context; public TestAdapter(Context context, int textViewResourceId, ArrayList<TestStatus> items) { super(context, textViewResourceId, items); this.items = items; this.context = context; this.inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } //1行分の処理 //とにかく何をするにしてもこのgetViewというメソッドが1行操作するごとに呼ばれる @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; //新規に作る場合はnullがわたってくる if (view == null) { //1行分layoutからViewの塊を生成 view = inflater.inflate(R.layout.list_row, null); } //itemsからデータ //viewから画面にくっついているViewを取り出して値をマッピングする TestStatus testItem = (TestStatus) items.get(position); if (testItem != null) { imageView.setImageResource(testItem.getProfileImageUrl()); } if (screenName != null) { screenName.setText(testItem.getScreenName()); } if (text != null) { text.setText(testItem.getText()); } } return view; } }