2012年7月24日火曜日

ListView:SimpleAdapter View再利用 getView


使い方があってるか良く分からない。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class AndroidListviewTest01Activity extends Activity {

 private ArrayList<Map<String, String>> retDataList;
 private SimpleAdapter adapter;
 private HashMap<String, String> data;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  new ArrayList<Map<String, Object>>();
  retDataList = new ArrayList<Map<String, String>>();

  for (int n = 0; n < 11; n++) {
   data = new HashMap<String, String>();
   data.put("title", n + "行目");
   data.put("comment", "テキスト" + n);
   retDataList.add(data);
  }

  // adapter = new SimpleAdapter(this, retDataList,R.layout.raw, new
  // String[] { "title", "comment" }, new int[] {android.R.id.text1,
  // android.R.id.text2 });
  adapter = new MySimpleAdapter(this, retDataList, R.layout.raw, new String[] { "title", "comment" }, new int[] { android.R.id.text1, android.R.id.text2 });

  // MyAdapter adapter = new MyAdapter(this, list);

  ListView listView = (ListView) findViewById(R.id.listView1);
  listView.setAdapter(adapter);//
 }

}

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MySimpleAdapter extends SimpleAdapter {

 // private Context context;
 private LayoutInflater inflater;
 private List<? extends Map<String, ?>> listData;

 public class ViewHolder {
  TextView line1;
  TextView line2;
 }

 public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
  super(context, data, resource, from, to);
  // TODO 自動生成されたコンストラクター・スタブ
  // this.context = context;
  this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  this.listData = data;
 }

 public View getView(int position, View convertView, ViewGroup parent) {
  ViewHolder holder;

  // ビューを受け取る
  View view = convertView;

  if (view == null) {
   view = inflater.inflate(R.layout.raw, parent, false);
   // LayoutInflater inflater = (LayoutInflater)
   // context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   // view = inflater.inflate(R.layout.raw, null);

   holder = new ViewHolder();
   holder.line1 = (TextView) view.findViewById(android.R.id.text1);
   holder.line2 = (TextView) view.findViewById(android.R.id.text2);

   view.setTag(holder);
  } else {
   holder = (ViewHolder) view.getTag();
  }

  String text1 = ((HashMap<?, ?>) listData.get(position)).get("title").toString();
  String text2 = ((HashMap<?, ?>) listData.get(position)).get("comment").toString();
  holder.line1.setText(text1);
  holder.line2.setText(text2);

  return view;
 }
}

関連記事

0 件のコメント:

コメントを投稿