【问题标题】:How to populate a Custom List Adapter with more than 2 values ?- JAVA android如何使用超过 2 个值填充自定义列表适配器?- JAVA android
【发布时间】: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


【解决方案1】:

您忘记在convertView == null 分店致电setTag()

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);
    //forgot to do that
    convertView.setTag(holder)
} ...

这就是为什么当你稍后尝试获取标签时,你什么也得不到。

【讨论】:

  • 我试过了,但一直遇到同样的问题。你认为这就是我试图检索这些值的方式吗?与自定义适配器 HoleListAdapter 代码中的 objectItem ?
  • @PedroGCA 你用调试器检查过mHoleData 的值吗?
  • mHoleData = [Lcom.example.app.Scorecard$ObjectItem;@433de628
  • @PedroGCA 这只是地址。请尝试添加我提到的字符串并使用更新的代码发布新的 logcat
  • 我刚刚包含了字符串并进行了调试。我的 setImageResourceholder 似乎没问题。然而,问题似乎是下面的一行 objectItemnull。这是跟踪:objectItem = nullobjectItem.strokes = java.lang.NullPointerExceptionobjectItem.hole = java.lang.NullPointerExceptionobjectItem.par = java.lang.NullPointerException
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-21
  • 1970-01-01
相关资源
最近更新 更多