如何处理微调器中的数组项?

时间:2015-08-26 00:28:57

标签: android android-spinner

我需要一个代码来处理微调器内的数组中的项目以允许我检查 .....

if(item == "item name"){
    do something
}

if(item.position == 1){
    do something
}

1 个答案:

答案 0 :(得分:0)

您可以像这段代码一样更改适配器

公共类PlannedRideAdapter扩展BaseAdapter {     // private LayoutInflater mInflater;

private Context context;
private RideData rideData = new RideData();
private List<RideData> ride_data;

public PlannedRideAdapter(Context context, List<RideData> ridedata) {
    this.context = context;
    this.ride_data = ridedata;

}

@Override
public int getCount() {
    if (ride_data.size() > 0) {

        return ride_data.size();
    }
    return 1;
}

@Override
public Object getItem(int arg0) {
    return arg0;
}

@Override
public long getItemId(int arg0) {
    return arg0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewholder;

    if (convertView == null) {
        LayoutInflater inflator = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflator.inflate(R.layout.plannedride_km_list, null);
        viewholder = new ViewHolder();
        convertView.setTag(viewholder);

    } else {
        viewholder = (ViewHolder) convertView.getTag();
    }
    rideData = ride_data.get(position);

    // init view
    viewholder.imageView = (ImageView) convertView
            .findViewById(R.id.imageView1);
    viewholder.date = (TextView) convertView.findViewById(R.id.date_value);
    viewholder.source = (CustomFontTextView) convertView
            .findViewById(R.id.source_icon);
    viewholder.time = (TextView) convertView.findViewById(R.id.time_value);
    viewholder.destination = (CustomFontTextView) convertView
            .findViewById(R.id.dest_icon);
    viewholder.rideId = (CustomFontTextView) convertView
            .findViewById(R.id.rideId);
    viewholder.ride_id = (TextView) convertView
            .findViewById(R.id.rideIdText);
    viewholder.distance = (CustomFontTextView) convertView
            .findViewById(R.id.distance);
    viewholder.source_value = (TextView) convertView
            .findViewById(R.id.source_value);

    viewholder.ride_id_value = (TextView) convertView
            .findViewById(R.id.rideId_value);
    viewholder.distance_value = (TextView) convertView
            .findViewById(R.id.distance_value);
    // viewholder.ride_detail = (TextView) convertView
    // .findViewById(R.id.textRideDetail);
    // setview
    viewholder.date.setText("" + rideData.getDate());
    viewholder.source.setText(context.getResources().getString(
            R.string.font_awesome_marker_icon));
    viewholder.source_value.setText("" + rideData.getSource());
    viewholder.time.setText("" + rideData.getTime());
    viewholder.rideId.setText(context.getResources().getString(
            R.string.font_awesome_bookingnumber_icon));
    viewholder.ride_id_value.setText("" + rideData.getRideId());
    if (rideData.getRideType().equalsIgnoreCase("k")) {
        viewholder.distance.setVisibility(View.VISIBLE);
        viewholder.distance_value.setVisibility(View.VISIBLE);
        viewholder.imageView.setImageResource((R.drawable.one_way));

        viewholder.destination.setText(context.getResources().getString(
                R.string.font_awesome_radio_tick_icon));

        viewholder.distance.setText(context.getResources().getString(
                R.string.font_awesome_distance_icon));

        viewholder.distance_value.setText(""
                + rideData.getKilometerRide().getDestination());

    } else {
        viewholder.imageView.setImageResource((R.drawable.time));

        viewholder.destination.setText(context.getResources().getString(
                R.string.font_awesome_clock_icon));

        viewholder.distance.setVisibility(View.GONE);
        viewholder.distance_value.setVisibility(View.GONE);
        // viewholder.ride_detail.setVisibility(View.GONE);
    }
    return convertView;
}

public List<RideData> getRide_data() {
    return ride_data;
}

public void setRide_data(List<RideData> ride_data) {
    this.ride_data = ride_data;
}

public static class ViewHolder {
    public CustomFontTextView source;
    public CustomFontTextView destination;
    public CustomFontTextView rideId;
    public CustomFontTextView distance;
    public CustomFontTextView total_time;
    public TextView date, time, distance_value, source_value,
            destination_value, time_value, ride_id_value, ride_id;

    public ImageView imageView;
}

}

我正在显示基于千米(rideType k)或每小时(rideType h)的列表,具有相同的布局。