【发布时间】:2014-04-12 23:34:55
【问题描述】:
我正在尝试使用多值对象数组填充自定义适配器,但出现了问题。我评论了下面的自定义适配器代码,我的错误在哪里。
这是我的记分卡活动代码:
ObjectItem[] holeData = new ObjectItem[20];
for (int i = 1; i <= hole_counter; i++) {
// Getting values from Shared Prefs
strokeArray[i] = mySharedData.getInt("hole_" + i + "_stroke", 0);
parArray[i] = mySharedData.getInt("hole_" + i + "_par", 0);
// Populating Objects Array
holeData[i] = new ObjectItem(i, strokeArray[i], parArray[i]);
}
HoleListAdapter adapter = new HoleListAdapter(getListView().getContext(), holeData);
setListAdapter(adapter);
/** Object Items Class */
public class ObjectItem {
public int hole;
public int strokes;
public int par;
// constructor
public ObjectItem(int hole, int strokes, int par) {
this.hole = hole;
this.strokes = strokes;
this.par = par;
}
}
这是我的自定义适配器HoleListAdapter代码:
public class HoleListAdapter extends ArrayAdapter<Scorecard.ObjectItem> {
protected Context mContext;
Scorecard.ObjectItem mHoleData[] = null;
public HoleListAdapter(Context context, Scorecard.ObjectItem[] holeData) {
super(context, R.layout.scorecard_list_item, holeData);
mContext = context;
mHoleData = holeData;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null)
{
convertView = LayoutInflater.from(mContext).inflate(R.layout.scorecard_list_item, null);
holder = new ViewHolder();
holder.iconImageView = (ImageView) convertView.findViewById(R.id.pin_icon);
holder.holeTextView = (TextView) convertView.findViewById(R.id.hole_number_title);
holder.strokeTextView = (TextView) convertView.findViewById(R.id.stroke_number_title);
holder.parTextView = (TextView) convertView.findViewById(R.id.par_number_title);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Scorecard.ObjectItem objectItem = mHoleData[position + 1];
holder.iconImageView.setImageResource(R.drawable.ic_pin);
// HAVING ERROS ON THESE LINES BELOW
holder.holeTextView.setText(objectItem.hole);
holder.strokeTextView.setText(objectItem.strokes);
holder.parTextView.setText(objectItem.par);
return convertView;
}
public static class ViewHolder {
ImageView iconImageView;
TextView holeTextView;
TextView strokeTextView;
TextView parTextView;
}
}
这是堆栈跟踪:
Process: com.gca.plusgolf, PID: 17808
android.content.res.Resources$NotFoundException: String resource ID #0x1
// The line below is the Error's Place that I mentioned in the HoleListAdapter Code
at com.gca.plusgolf.HoleListAdapter.getView(HoleListAdapter.java:54)
如果有更好的方法来填充具有 3 个值的自定义适配器,请告诉我。 谢谢。
【问题讨论】:
-
发布堆栈跟踪。
-
@AmitGupta 有痕迹。 tks
标签: java android object populate custom-adapter