如何根据微调器选择在edittext中设置值?

时间:2015-12-02 13:24:07

标签: android json android-edittext android-spinner

我有以下回复,

{
    "1": {
        "name": "abc",
        "mobile_number": "123456789",
        "address": "streetno3",
        "landmark": "landmarksss",

    },
    "2": {
        "name": "def",
        "mobile_number": "123456789",
        "address": "streetno3",
        "landmark": "landmarksss",

    },
    "3": {
        "name": "ghi",
        "mobile_number": "23423423234",
        "address": "abcdefgh",
        "landmark": "usa",

    },
    "4": {
        "name": "vvb",
        "mobile_number": "55666655",
        "address": "xvgghg",
        "landmark": "fghgh",

    },
    "5": {
        "name": "test",
        "mobile_number": "77699231010",
        "address": "pune",
       "landmark": "fghgh",
    }
}

我得到了spinner中的所有名字,直到这里它工作正常,现在我正在尝试默认情况下我选择了abc,如果我从微调器中选择测试,那我怎样才能在我的{{{ 1}},简而言之,根据名称选择,我试图在edittexts

中获取并设置详细信息

Java代码

edittext

2 个答案:

答案 0 :(得分:1)

最好为此类数据制作POJO类,以便轻松管理数据。但是如果你还没有选择POJO类,那么现在你可以从它的位置获取微调器数据并将其拆分为在EditText中打印。

假设您在spinner中的ItemSelected上获得数据字符串中的数据,您可以按照以下代码将其打印在edittext中。

String split[]=data.split(",");
String name=split[0];
String mobile=split[1];

答案 1 :(得分:0)

你有两个选择

1.创建自定义微调器适配器,如下例

<强> CustomAdapter.java

 /***** Adapter class extends with ArrayAdapter ******/
public class CustomAdapter extends ArrayAdapter<String>{

    private Activity activity;
    private ArrayList data;
    public Resources res;
    SpinnerModel tempValues=null;
    LayoutInflater inflater;

    /*************  CustomAdapter Constructor *****************/
    public CustomAdapter(
                          CustomSpinner activitySpinner, 
                          int textViewResourceId,   
                          ArrayList objects,
                          Resources resLocal
                         ) 
     {
        super(activitySpinner, textViewResourceId, objects);

        /********** Take passed values **********/
        activity = activitySpinner;
        data     = objects;
        res      = resLocal;

        /***********  Layout inflator to call external xml layout () **********************/
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

      }

    @Override
    public View getDropDownView(int position, View convertView,ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    // This funtion called for each row ( Called data.size() times )
    public View getCustomView(int position, View convertView, ViewGroup parent) {

        /********** Inflate spinner_rows.xml file for each row ( Defined below ) ************/
        View row = inflater.inflate(R.layout.spinner_rows, parent, false);

        /***** Get each Model object from Arraylist ********/
        tempValues = null;
        tempValues = (SpinnerModel) data.get(position);

        TextView label        = (TextView)row.findViewById(R.id.company);
        TextView sub          = (TextView)row.findViewById(R.id.sub);
        ImageView companyLogo = (ImageView)row.findViewById(R.id.image);

        if(position==0){

            // Default selected Spinner item 
            label.setText("Please select company");
            sub.setText("");
        }
        else
        {
            // Set values for spinner each row 
            label.setText(tempValues.getCompanyName());
            sub.setText(tempValues.getUrl());
            companyLogo.setImageResource(res.getIdentifier
                                         ("com.androidexample.customspinner:drawable/"
                                          + tempValues.getImage(),null,null));

        }   

        return row;
      }
 }

<强> spinner_rows.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 android:padding="3dip"
>
    <ImageView
         android:id="@+id/image"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         />
    <TextView
         android:layout_toRightOf="@+id/image"
         android:padding="3dip"
         android:layout_marginTop="2dip"
         android:textColor="@drawable/red"
         android:textStyle="bold"
         android:id="@+id/company"
         android:layout_marginLeft="5dip"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
     <TextView
         android:layout_toRightOf="@+id/image"
         android:padding="2dip"
         android:textColor="@drawable/darkgrey"
         android:layout_marginLeft="5dip"
         android:id="@+id/sub"
         android:layout_below="@+id/company"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
</RelativeLayout>

<强> SpinnerModel.java

public class SpinnerModel {

        private  String CompanyName="";
        private  String Image=""; 
        private  String Url="";

        /*********** Set Methods ******************/
        public void setCompanyName(String CompanyName)
        {
            this.CompanyName = CompanyName;
        }

        public void setImage(String Image)
        {
            this.Image = Image;
        }

        public void setUrl(String Url)
        {
            this.Url = Url;
        }

        /*********** Get Methods ****************/
        public String getCompanyName()
        {
            return this.CompanyName;
        }

        public String getImage()
        {
            return this.Image;
        }

        public String getUrl()
        {
            return this.Url;
        }   
  }

<强> activity_custom_spinner.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">

      <TextView
          android:paddingTop="20dip"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"/>

      <Spinner
          android:id="@+id/spinner"
          android:drawSelectorOnTop="true"
          android:prompt="@string/defaultText"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"

          />

      <TextView
          android:paddingTop="20dip"
          android:paddingLeft="20dip"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:id="@+id/output"
          />

  </LinearLayout>

<强> CustomSpinner.java

public class CustomSpinner extends Activity {

    /**************  Intialize Variables *************/
    public  ArrayList<SpinnerModel> CustomListViewValuesArr = new ArrayList<SpinnerModel>();
    TextView output = null;
    CustomAdapter adapter;
    CustomSpinner activity = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_spinner);

        activity  = this;

        Spinner  SpinnerExample = (Spinner)findViewById(R.id.spinner);
        output                  = (TextView)findViewById(R.id.output);

        // Set data in arraylist
        setListData();

        // Resources passed to adapter to get image
        Resources res = getResources(); 

        // Create custom adapter object ( see below CustomAdapter.java )
        adapter = new CustomAdapter(activity, R.layout.spinner_rows, CustomListViewValuesArr,res);

        // Set adapter to spinner
        SpinnerExample.setAdapter(adapter);

        // Listener called when spinner item selected
        SpinnerExample.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View v, int position, long id) {
                // your code here

                // Get selected row data to show on screen
                String Company    = ((TextView) v.findViewById(R.id.company)).getText().toString();
                String CompanyUrl = ((TextView) v.findViewById(R.id.sub)).getText().toString();

                String OutputMsg = "Selected Company : \n\n"+Company+"\n"+CompanyUrl;
                output.setText(OutputMsg);

                Toast.makeText(
                        getApplicationContext(),OutputMsg, Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // your code here
            }

        });
    }

    /****** Function to set data in ArrayList *************/
    public void setListData()
    {

        // Now i have taken static values by loop.
        // For further inhancement we can take data by webservice / json / xml;

        for (int i = 0; i < 11; i++) {

            final SpinnerModel sched = new SpinnerModel();

              /******* Firstly take data in model object ******/
               sched.setCompanyName("Company "+i);
               sched.setImage("image"+i);
               sched.setUrl("http:\\www."+i+".com");

            /******** Take Model Object in ArrayList **********/
            CustomListViewValuesArr.add(sched);
        }

    }

  }
  1. 创建一个bean类型arraylist并按照你在arrallstates中存储的相同顺序存储所有细节,并且在spinner的onItemSelected方法中你总是得到位置,并且在两个数组列表中你都有相同的数据位置,所以现在从bean中获取数据键入数组列表,然后根据需要设置它