5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ListViewでセクションを付ける

Posted at
sample.java
public class SampleAdapter extends BaseAdapter{

	private ArrayList<Object> items;
	private Context context;
	private LayoutInflater inflater;
	
	
	public TagSelectAdapter(Context context,ArrayList<Object> items){
		this.context = context;
		this.items = items;
		this.inflater = LayoutInflater.from(context);
	}
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(items.get(position).getClass().equals(String.class)){
			convertView = inflater.inflate(R.layout.sample_section, null);
			TextView section = (TextView) convertView.findViewById(R.id.section);
			section.setText((String) items.get(position));
		}
		else{
			convertView = inflater.inflate(R.layout.custom_cell, null);
			//ここにオリジナルのcellをいろいろ書く
			/**
			*itemsをセットするとき	
			*section.setText((String) items.get(position));のように
			*itemsの前に変換するクラスのキャストをすることでArrayList<Object>に
			*いろいろ突っ込んだクラスごとにViewを生成することが出来る
			*/
		}

		
		
		
		return convertView;
	}
	
	
	
	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}


	

}

以上のコメントアウトにも書いたようにArrayListの中身を
items.getClass().equals()で振り分けすることで一つのListViewに
複数の種類のオリジナルのセルを作ることが出来る。

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?