在Spinner中更改所选项目

时间:2014-03-26 19:28:00

标签: android listview spinner

我有一个自定义微调器类,我想更改所选项目文本颜色。如您所见,虚拟设备中的文本几乎不可读。我可以将列表项设置为黑色,您可以在我的代码中看到,但我不确定如何更改所选的项目颜色?任何想法?

image

    package com.cbs.iomamobile;

import java.util.ArrayList;

import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class CustomCreateAssemblyViewAdapter extends BaseAdapter{

    private static ArrayList<ArrayList<String>> partLotNumbersArrayList;
    private static ArrayList<String> partNames;
    private static String selected;
    private LayoutInflater mInflater;

    private Context myContext;

    public CustomCreateAssemblyViewAdapter(Context context, ArrayList<ArrayList<String>> results, ArrayList<String> parts){
        partLotNumbersArrayList = results;
        partNames = parts;
        mInflater = LayoutInflater.from(context);
        myContext = context;
    }
    @Override
    public int getCount() {
        return partLotNumbersArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return partLotNumbersArrayList.get(position);
    }

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



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

        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.view_assembly_parts, null);
            holder = new ViewHolder();
            holder.txtName = (TextView) convertView.findViewById(R.id.partName);
            holder.spinner = (Spinner) convertView.findViewById(R.id.LotNumbers);
            convertView.setTag(holder); 
            holder.txtName.setTextColor(Color.BLACK);

            /*if(!holder.txtName.hasFocus()){
                holder.txtName.setFocusable(true);
                holder.txtName.requestFocus();
            }
            if(!holder.spinner.hasFocus()){
                holder.spinner.setFocusable(true);
                holder.spinner.requestFocus();
            }*/
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtName.setText(partNames.get(position));        

        //this is needed to set the text color of the dropdown items, else the text is unreadable.
        ArrayAdapter<String> adp1=new ArrayAdapter<String>(myContext, android.R.layout.simple_list_item_1, partLotNumbersArrayList.get(position)){
            @Override
            public View getDropDownView(int position, View convertView,android.view.ViewGroup parent){
                View v = convertView;
                View view = super.getView(position, convertView, parent);               
                TextView text = (TextView) view.findViewById(android.R.id.text1);
                text.setTextColor(Color.BLACK);
                text.setBackgroundColor(Color.WHITE);
                return view;
            }
        };

        adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //set the adapter to the spinnner
        holder.spinner.setAdapter(adp1);        

        //if there is only one other part besides "" then set that as default part
        if(partLotNumbersArrayList.get(position).size() == 2){
            holder.spinner.setSelection(1);
        }   

        holder.spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {


            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
                ///ViewOrderlineAssemblyActivity.assembly.addPart(new Part((String)holder.txtName.getText(), ""));
                //Log.v("OnNothingSelected", (String)holder.txtName.getText());
            }

        });

        return convertView;
    }

    static class ViewHolder {
        TextView txtName;
        Spinner spinner;
    }
}

1 个答案:

答案 0 :(得分:2)

尝试在onItemSelected中实施OnItemSelectedListener

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    ((TextView) arg0.getChildAt(0)).setTextColor(Color.RED);
}