Change text value dynamically in a spinner

☆樱花仙子☆ 提交于 2019-12-13 04:57:33

问题


I want to use a spinner in my app with a static list of choices, for example:

  • Speed
  • Altitude
  • Latitude
  • Longitude

So, when clicking on the spinner the drop down view appears and if I select altitude I want to display the altitude value and then update this value into the spinner itself (I've got a GPS service to get location data).

To achieve this I create my own adapter and override the getView() and the getDropDownView():

package com.example.spinnertest;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MySpinnerAdapter extends BaseAdapter {
    private Context _context;
    private ArrayList<String> _values;

    public MySpinnerAdapter(Context context, ArrayList<String> values) {
        this._context = context;
        this._values = values;
    }

    @Override
    public int getCount() {
        return _values.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater layout = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layout.inflate(R.layout.item_spinner_value, null);
        }
        TextView valueTv = (TextView) convertView.findViewById(R.id.item_spinner_value_tv);
        return convertView;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater layout = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layout.inflate(R.layout.item_spinner_list, null);
        }
        ImageView imView = (ImageView) convertView.findViewById(R.id.item_spinner_list_iv);
        TextView textView = (TextView) convertView.findViewById(R.id.item_spinner_list_tv);
        textView.setText(_values.get(position));
        return convertView;
    }   
}

In the getView() I can access the TextView where I want to display the dynamic value but I don't know how to access it from my Activity. Any idea how to do this?

My MainActivity.java :

package com.example.spinnertest;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Spinner;

public class MainActivity extends Activity {

    private ArrayList<String> maListe = new ArrayList<String>(){{add("Altitude");add("Speed");add("Longitude");add("Latitude");}};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Spinner spi = (MySpinner)findViewById(R.id.spinner1);
        MySpinnerAdapter spiAd = new MySpinnerAdapter(getApplicationContext(), maListe);
        spi.setAdapter(spiAd);
    }
}

I didn't implements my gps service yet. I'm just trying to solve my spinner problem.


回答1:


Try to change adapter in Spinner. OnItemSelected.



来源:https://stackoverflow.com/questions/24753462/change-text-value-dynamically-in-a-spinner

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!