Problem with ArrayAdapter<String> i can't show data from String []
First thanks in advance for any help.
I got not exception in this code but for any reason i can't show the list of the data .
from my activity
ListView mRemindersList = (ListView) findViewById(R.id.list_reminders);
String[] mTimes = getResources().getStringArray(R.array.eventTimes);
mRemindersList.setAdapter(new ReminderAdapter(this,
R.layout.reminders_list, mTimes));
my data called evenTimes ( R.array.eventTimes )
<string-array name="eventTimes">
<item name="None">None</item>
<item name="5m">5 minutes before</item>
<item name="15m">15 minutes before</item>
<item name="30m">30 minutes before</item>
<item name="1h">1 hour before</item>
<item name="2h">2 hour before</item>
<item name="1d">1 day before</item>
</string-array>
this is my adapter which is a nested class in my activity
private class ReminderAda开发者_JAVA技巧pter extends ArrayAdapter<String> {
private int resId;
private LayoutInflater mInflater;
/**
* @param context
* @param resource
* @param objects
*/
public ReminderAdapter(Context context, int resId, String[] objects) {
super(context, resId, objects);
this.resId = resId;
this.mInflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.items = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView remider = null;
if (convertView == null) {
convertView = mInflater.inflate(resId, parent, false);
remider = (TextView) convertView.findViewById(R.id.reminder);
}
String time = (String) getItem(position);
remider.setText(time);
return convertView;
}
}
and this is the textview with the data.
<TextView android:id="@+id/reminder" android:layout_gravity="center"
android:layout_width="fill_parent" android:layout_height="30dip"
android:textColor="#000000"
android:singleLine="true" />
any advice.
Cheers Ron
When your convert view is not null, you are not getting remider
so this may be null in the call to remider.setText(time);
and the getView()
method will fail with a NPE.
Have a look at the docs for a correct implementation of the view holder pattern (e.g. in this example ; I can't find the article that explains this atm)..
精彩评论