Marshall S. Lee Marshall S. Lee - 8 days ago 6
Android Question

Android: Resources$NotFoundException: String resource 0#00 on list view item click

Here's the

Adapter
source code which extends
BaseAdapter
and contains multiple views.

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);
}
}


And this is the part of an
Activity
which contains the list and the
onItemClickListener
.

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();
}
});


When I ran this code, I expected that the Toast message would appear with the corresponding position number, but it crashed with this message -
android.content.res.Resources$NotFoundException: String resource ID #0x0
.

Answer

You should use

Toast.makeText(CreateNewAlarmActivity.this, ""+position, Toast.LENGTH_SHORT).show();

From your code, you are trying to toast the position of item click in listview. But Toast accepts a message in String resource to display.