BaseAdapter
を拡張し、複数のビューを含むAdapter
ソースコードです。
public class AddNewAlarmOptionItemListAdapter extends BaseAdapter {
private static final int ITEM_TEXT_ONLY = 0;
private static final int ITEM_TEXT_AND_PROGRESS_BAR = 1;
private static final int ITEM_TEXT_AND_SWITCH = 2;
// number of types in total.
private static final int ITEM_TOTAL_TYPES = 3;
private ArrayList<AddNewAlarmOptionItem> itemLists = new ArrayList<>();
public AddNewAlarmOptionItemListAdapter() {
}
// overridden method for enabling the adapter to contain different types of views.
@Override
public int getViewTypeCount() {
return ITEM_TOTAL_TYPES;
}
// overridden method for enabling the adapter to contain different types of views.
@Override
public int getItemViewType(int position) {
return itemLists.get(position).getType();
}
@Override
public int getCount() {
return itemLists.size();
}
@Override
public Object getItem(int position) {
return itemLists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
final Context context = viewGroup.getContext();
int viewType = getItemViewType(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
AddNewAlarmOptionItem item = itemLists.get(position);
switch(viewType) {
case ITEM_TEXT_ONLY:
convertView = inflater.inflate(R.layout.listitem_text_only, viewGroup, false);
TextView tvSubject = (TextView) convertView.findViewById(R.id.tvTitle);
TextView tvOptions = (TextView) convertView.findViewById(R.id.tvSubtitle);
tvSubject.setText(item.getTitle());
tvOptions.setText(item.getSubtitle());
break;
case ITEM_TEXT_AND_PROGRESS_BAR:
convertView = inflater.inflate(R.layout.listitem_text_and_seek_bar, viewGroup, false);
TextView tvAlarmType = (TextView) convertView.findViewById(R.id.tvAlarmType);
TextView tvAlarmOption = (TextView) convertView.findViewById(R.id.tvAlarmOption);
tvAlarmType.setText(item.getTitle());
tvAlarmOption.setText(item.getSubtitle());
ImageView ivSpeaker = (ImageView) convertView.findViewById(R.id.ivSpeaker);
SeekBar sbVolume = (SeekBar) convertView.findViewById(R.id.sbVolume);
break;
case ITEM_TEXT_AND_SWITCH:
convertView = inflater.inflate(R.layout.listitem_text_and_switch, viewGroup, false);
TextAndSwitchItem textAndSwitchItem = new TextAndSwitchItem();
TextView tvAlarmOptionTitle = (TextView) convertView.findViewById(R.id.tvAlarmOptionTitle);
TextView tvAlarmOptionDescription = (TextView) convertView.findViewById(R.id.tvOptionDescription);
Switch switchOption = (Switch) convertView.findViewById(R.id.switchOption);
tvAlarmOptionTitle.setText(item.getTitle());
tvAlarmOptionDescription.setText(item.getSubtitle());
switchOption.setChecked(true);
break;
}
return convertView;
}
// add a text-only item to list view
public void addItem(TextOnlyItem item) {
item.setType(ITEM_TEXT_ONLY);
itemLists.add(item);
}
// add a text-and-progress-bar item to list view
public void addItem(TextAndSeekbarItem item) {
item.setType(ITEM_TEXT_AND_PROGRESS_BAR);
itemLists.add(item);
}
// add a text-and-switch item to list view
public void addItem(TextAndSwitchItem item) {
item.setType(ITEM_TEXT_AND_SWITCH);
itemLists.add(item);
}
}
そして、これはリストとonItemClickListener
が含まれていActivity
の一部です。私はこのコードを実行したとき
listAdapter = new AddNewAlarmOptionItemListAdapter();
lvAlarmOptionList = (ListView) findViewById(R.id.lvAlarmOptionList);
lvAlarmOptionList.setAdapter(listAdapter);
tpTimePicker = (TimePicker) findViewById(R.id.tpTimePicker);
btnCancel = (Button) findViewById(R.id.btnCancel);
btnSave = (Button) findViewById(R.id.btnSave);
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cancelAddingNewAlarm();
}
});
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveAlarm();
}
});
initializeListItems();
addItemsToListView();
lvAlarmOptionList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Toast.makeText(CreateNewAlarmActivity.this, position, Toast.LENGTH_SHORT).show();
}
});
、私はトーストメッセージは、対応する位置番号が表示されますことを期待しますが、このメッセージに墜落した - android.content.res.Resources$NotFoundException: String resource ID #0x0
。
トーストがint値を膨らませることができないためです。 String.valueOf(position)を使用する必要があります。 –