在我的自定义arrayadapter上保持-1作为位置

时间:2013-11-26 13:17:59

标签: android android-arrayadapter

我一直在自定义arrayadapter上获得-1作为位置,我想要的是setselected 但我想用之前选择的选项预加载微调器。 这是我的arrayadapter:OfficeAccomodationArrayAdapter

package com.xxx.xxx.model;

import java.util.List;

import com.xxx.xxx.R;
import com.xxx.xxx.entities.OfficeAccommodation;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class OfficeAccommodationArrayAdapter extends ArrayAdapter<OfficeAccommodation>{
Context context;
int layoutResourceId;   
List<OfficeAccommodation> data = null;

public OfficeAccommodationArrayAdapter(Context context, int layoutResourceId, List<OfficeAccommodation> rows) {
    super(context, layoutResourceId, rows);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = rows;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    Holder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new Holder();
        holder.txtSpinner = (TextView)row.findViewById(R.id.txtSpinner);
        row.setTag(holder);
    }
    else
    {
        holder = (Holder)row.getTag();
    }

    OfficeAccommodation item = data.get(position);
    holder.Id = item.getId();
    holder.txtSpinner.setText(item.getName());

    return row;
}

@Override
public View getDropDownView (int position, View convertView, ViewGroup parent) {
    View row = convertView;
    Holder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new Holder();
        holder.txtSpinner = (TextView)row.findViewById(R.id.txtSpinner);
        row.setTag(holder);
    }
    else
    {
        holder = (Holder)row.getTag();
    }

    OfficeAccommodation item = data.get(position);
    holder.Id = item.getId();
    holder.txtSpinner.setText(item.getName());

    return row;
}
//@Override
//public int getPosition(OfficeAccommodation item) {
//     return data.indexOf(item);
//}

static class Holder
{
    int Id;
    TextView txtSpinner;
}

}

在我的活动中我有这个

protected void setOfficeAccommodation() {
    ///////////Build Spinner for Office Accommodation////////////////


    List<OfficeAccommodation> OAList = db.getAllOfficeAccommodation();
    OfficeAccommodationArrayAdapter OAArrayAdapter = new OfficeAccommodationArrayAdapter(this, R.layout.spinner, OAList);
    OfficeAccommodation item = db.getOfficeAccommodationByAgentLicenseId(model.getId());
    int position  = OAArrayAdapter.getPosition(item);
    spnOfficeAccomodation =  new Spinner(this);
    spnOfficeAccomodation.setId(1);
    spnOfficeAccomodation.setAdapter(OAArrayAdapter);
    spnOfficeAccomodation.setSelection(position);

}

0 个答案:

没有答案