简体   繁体   中英

How to populate a Custom List Adapter with more than 2 values ?- JAVA android

I'm trying to populate a Custom Adapter with a Multiple Values Objects Array but something is going wrong. I commented on the custom adapter code below where is my error.

This is my Scorecard Activity Code:

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

This is my custom adapter HoleListAdapter Code:

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

Here is the stack trace:

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)

If is there a better way to populate a custom adapter with 3 values please let me know. Thanks.

You forgot to call setTag() at the convertView == null branch:

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

That is why when later you are trying to get tag, you get nothing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM