1. 程式人生 > >DemoSpinner有關多個Spinner的例子,網上找的都不是 想要的,可以網路獲取資料新增的Spinner

DemoSpinner有關多個Spinner的例子,網上找的都不是 想要的,可以網路獲取資料新增的Spinner

直接上程式碼:

xml佈局:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
     >
    
<LinearLayout
   android:layout_width="fill_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >


    <EditText
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
          />


    <Spinner  
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner"/>
    
    <EditText
        android:id="@+id/textt1"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
          />


    <Spinner  
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner1"/>
    
    
    <EditText
        android:id="@+id/textt2"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
          />


    <Spinner  
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner2"/>
    </LinearLayout>
</RelativeLayout> 

自定義adapter工具類

package com.example.spinnerdemo;



import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;


import android.content.Context;
import android.text.Layout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;


public class MyBaseAdapter extends BaseAdapter{
 //上下文 
    Context  context; 
    List<String> list=new ArrayList<String>();
    public MyBaseAdapter(Context context,List<String> list){ 
        this.context=context; 
        this.list = list;
    } 
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}


@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout layout=new LinearLayout(context); 
         LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
                 ViewGroup.LayoutParams.FILL_PARENT); 
         layout.setOrientation(LinearLayout.HORIZONTAL); 
         LinearLayout.LayoutParams params2=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
                 ViewGroup.LayoutParams.WRAP_CONTENT); 
         
         TextView  textView=new TextView(context); 
         textView.setText(this.getItem(position).toString()); 
         
         layout.addView(textView, params2); 
         
         
return layout;
}
 

}

Spinner程式碼:

package com.example.spinnerdemo;


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




import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;


public class DemoSpinner extends Activity{

private Spinner sp,sp1,sp2;
private EditText text,text2,text1;
private List<String> arr;
private List<String> arr1;
private List<String> arr2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
    
   text=(EditText)findViewById(R.id.text);  
    sp=(Spinner)findViewById(R.id.spinner);
    
    text1=(EditText)findViewById(R.id.textt1);  
    sp1=(Spinner)findViewById(R.id.spinner1);
    
    text2=(EditText)findViewById(R.id.textt2);  
    sp2=(Spinner)findViewById(R.id.spinner2);
    
    
    //注意這個資料可以是網路json的資料了
    
    arr = new ArrayList<String>();
arr.add("1");
arr.add("2");
arr.add("3");
arr.add("4");
arr.add("5");
arr.add("6");
//注意這個資料可以是網路json的資料了


arr1 = new ArrayList<String>();
arr1.add("汙染");
arr1.add("風格的");
arr1.add("發給");
arr1.add("濰坊");
arr1.add("發給");
arr1.add("而非");

//注意這個資料可以是網路json的資料了


arr2 = new ArrayList<String>();
arr2.add("123");
arr2.add("224");
arr2.add("223");
arr2.add("4234");
arr2.add("234");
arr2.add("324");

MyBaseAdapter adapter = new MyBaseAdapter(DemoSpinner.this, arr);
   // Apply the adapter to the spinner 
   sp.setAdapter(adapter);
   sp.setPrompt("MySpinner"); //作用是給下拉選單新增標題  
       sp.setOnItemSelectedListener(new SpinnerListener());  
       
       MyBaseAdapter adapter1 = new MyBaseAdapter(DemoSpinner.this, arr1);
   // Apply the adapter to the spinner 
   sp1.setAdapter(adapter1);
   sp1.setPrompt("MySpinner1"); //作用是給下拉選單新增標題  
       sp1.setOnItemSelectedListener(new SpinnerListener1());
       
       MyBaseAdapter adapter2 = new MyBaseAdapter(DemoSpinner.this, arr2);
   // Apply the adapter to the spinner 
   sp2.setAdapter(adapter2);
   sp2.setPrompt("MySpinner2"); //作用是給下拉選單新增標題  
       sp2.setOnItemSelectedListener(new SpinnerListener2());
       
       
       
       
}

class SpinnerListener implements OnItemSelectedListener{


@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
//這個函式當選擇下拉選單選項時呼叫,第一個引數是下拉選單控制元件,第二個引數是當前被選中的Item,  
            //第三個引數是選中的位置,第四個引數是當前選中的Id。  
            String selected=arg0.getItemAtPosition(arg2).toString();  
            text.setText(selected); 
//            
//            String selected1=arg0.getItemAtPosition(arg2).toString();  
//            text1.setText(selected1); 
//            
//            
//            String selected2=arg0.getItemAtPosition(arg2).toString();  
//            text2.setText(selected2); 
}


@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
}


class SpinnerListener1 implements OnItemSelectedListener{


@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
//這個函式當選擇下拉選單選項時呼叫,第一個引數是下拉選單控制元件,第二個引數是當前被選中的Item,  
            //第三個引數是選中的位置,第四個引數是當前選中的Id。  
            
            String selected1=arg0.getItemAtPosition(arg2).toString();  
            text1.setText(selected1); 
            
            
}


@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
}





class SpinnerListener2 implements OnItemSelectedListener{


@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
//這個函式當選擇下拉選單選項時呼叫,第一個引數是下拉選單控制元件,第二個引數是當前被選中的Item,  
            //第三個引數是選中的位置,第四個引數是當前選中的Id。  
            
            
            String selected2=arg0.getItemAtPosition(arg2).toString();  
            text2.setText(selected2); 
}


@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
}
}