使用选定的微调器设置文本

时间:2014-05-16 21:53:41

标签: android spinner settext

我想在textview框中显示从微调器中选择的文本。 这是我的代码

private void registerClickCallback()
{
    ListView list =(ListView) findViewById(R.id.expcatlistview);
    //TextView selectedcategory = (TextView) findViewById (R.id.expenseCategory);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View viewClicked,
                int position, long id) 
        {
            Categoryexpense clickedcategory = mycategory.get(position);
            String message =" " + clickedcategory.getMake().toString();
            Toast.makeText(ExpenseCategory.this, message,Toast.LENGTH_LONG).show();

            //The above toast code works but when i try the following it gives me an error please help
            //TextView selectedcategory = (TextView) findViewById (R.id.expenseCategory);
            //selectedcategory.setText(message);
            //selectedcategory.setText(" " + message);
        }

1 个答案:

答案 0 :(得分:0)

我这样做了:

package com.yogeshojha.nagariksahayata;

import java.util.ArrayList;
import java.util.List;

import com.yogeshojha.nagariksahayata.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.app.Activity;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class ListViewFragment extends Fragment implements
        OnItemSelectedListener {
    public ListViewFragment() {
    }
    Spinner mspinner;
    TextView viewvdc;
    ArrayAdapter<String> adapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String[] name = { "Kitkat", "Jellybean",
            "Froyo"};
    System.out.println(name.length);
    View rootView = inflater.inflate(R.layout.fragment_vdc, container,
            false);
    viewvdc = (TextView) rootView.findViewById(R.id.viewvdc);
    mspinner = (Spinner) rootView.findViewById(R.id.spinnervdc);
    adapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_spinner_dropdown_item, name);
    mspinner.setAdapter(adapter);
    mspinner.setOnItemSelectedListener(this);
    return rootView;
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {
    mspinner.setSelection(position);
    String viewVdc = (String) mspinner.getSelectedItem();
    viewvdc.setText("Selected Android OS:" + viewVdc);

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {

}
}