2016-01-09 58 views
0

我的佈局中有兩個微調框。第一個選擇車輛,第二個從該特定車輛的短名單中選擇顏色。例如(選擇Bajaj,第二個需要顯示已保存的車輛顏色列表)多個自定義微調框始終將值設置爲一個spninner

在這裏,我確實從車輛和顏色的數據庫中獲取所有數據。這兩個價值都在我的手中。但是,如果我在Spinner1中選擇車輛類型,它將顯示同一個微調器中的車輛顏色,而不是顯示在Spinner2中。首先我有相同的(微調getset類,適配器類,自定義佈局)。現在我創建了所有三個不同的。即使現在也遇到同樣的問題。

如何找到,我在哪裏做錯了。以下是我正在使用的代碼。

public void setListData()// gets vehicles value from DB 
{ 
    List<VehicleGetSetter> contacts = vdb.getVehicleListToCustomer(vendor_id); 
    for (VehicleGetSetter cn : contacts) { 
     final BikeSpinner sched = new BikeSpinner(); 
        sched.setBikeName("Select Bike"); 
      CustomListBikeName.add(sched); 
     }} 

    private void setBikeAdapterValues() {// set Vehicle value in Spinner1 
    Resources res = getResources(); 
    adapter = new BikeSpinnerAdapter(NewCustomer.this, R.layout.bike_spinner, CustomListBikeName,res); 
    mySpinner.setAdapter(adapter); 
    mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parentView, View v, int position, long id) { 
      bike_no_from_adapter = ((TextView) v.findViewById(R.id.textbike_numbr)).getText().toString(); 
      what_bike = ((TextView) v.findViewById(R.id.textbike)).getText().toString(); 
      String OutputMsg = "Selected : \n\n"+bike_no_from_adapter; 
      if(position!=0){ 
       setListBikeColor(bike_no_from_adapter); 
      } 

     @Override 
     public void onNothingSelected(AdapterView<?> parentView) { 
       //Toast.makeText(getApplicationContext(), "You Must Select Vehicle", Toast.LENGTH_LONG).show(); 
     } 
    }); 
} 

    public void setListBikeColor(String bike_no_from_adapter){// get color of selected vehicles from DB 
    List<VehicleGetSetter> contacts1 = vdb.getVehicleColorListToCustomer(vendor_id,bike_no_from_adapter); 
    for (VehicleGetSetter cn1 : contacts1) { 
     final BikeSpinn sched1 = new BikeSpinn(); 
        sched1.setBikeNumber(cn1.getColor()); 
      CustomListBikeColor.add(sched1); 
      setBikeAdapterValuesColor(); 
     } 
} 


    private void setBikeAdapterValuesColor() {// trying to set value in second spinner, but setting in same vehicle spinner. 
    Resources res2 = getResources(); 
    coloradapter = new BikeSpinnerColor(NewCustomer.this, R.layout.bike_spinner_color, CustomListBikeColor,res2); 
    cust_bike_color.setAdapter(coloradapter); 
    cust_bike_color.setOnItemSelectedListener(new OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parentView, View v, int position, long id) { 
      // Get selected row data to show on screen 
      bike_no_from_adapter = ((TextView) v.findViewById(R.id.textbike_numbr)).getText().toString(); 
      what_bike = ((TextView) v.findViewById(R.id.textbike)).getText().toString(); 

     @Override 
     public void onNothingSelected(AdapterView<?> parentView) { 
       //Toast.makeText(getApplicationContext(), "You Must Select Vehicle", Toast.LENGTH_LONG).show(); 
     } 
    }); 
} 

MainLayout包含用於紡紗微調

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/bg_blue" 
android:orientation="vertical" 
> 
<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <TextView 
     android:id="@+id/bike" 
     android:layout_width="150dp" 
     android:layout_height="wrap_content" 
     android:textSize="22dp" 
     android:textColor="#000000" 
     android:text="Bike Details" /> 

    <Spinner 
     android:id="@+id/cust_bike" 
     android:layout_width="120dp" 
     android:layout_height="40dp" 
     android:paddingBottom="1dp" 
     android:paddingLeft="5dp" /> 
    <Spinner 
     android:id="@+id/cust_bike_color" 
     android:layout_width="120dp" 
     android:layout_height="40dp" 
     android:paddingBottom="1dp" 
     android:paddingLeft="5dp" /> 
    <Spinner 
     android:id="@+id/cust_bike_number" 
     android:layout_width="120dp" 
     android:layout_height="40dp" 
     android:paddingBottom="1dp" 
     android:paddingLeft="5dp" /> 
    </LinearLayout> 
</LinearLayout> 

自定義佈局(用於微調相似(名稱不同))

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" > 
<TextView 
    android:id="@+id/textbike" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="18dp" 
    android:layout_marginRight="5dp" 
    android:textColor="#000000" /> 
<TextView 
    android:id="@+id/textbike_numbr" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="18dp" 
    android:textColor="#000000" /> 

</LinearLayout> 

定製ArrayAdapterClass(有兩個紗廠兩個類似的類)

public class BikeSpinnerAdapter extends ArrayAdapter<String>{ 

private Activity activity; 
private ArrayList data; 
    public Resources res; 
    BikeSpinner tempValues=null; 
    LayoutInflater inflater; 
    public BikeSpinnerAdapter(NewCustomer activitySpinner, 
        int textViewResourceId, 
        ArrayList objects, 
        Resources resLocal 
        ) 
{ 
    super(activitySpinner, textViewResourceId, objects); 

    activity = activitySpinner; 
    data  = objects; 
    res  = resLocal; 

    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); 
} 

public View getCustomView(int position, View convertView, ViewGroup parent) { 

    View row = inflater.inflate(R.layout.bike_spinner, parent, false); 

    tempValues = null; 
    tempValues = (BikeSpinner) data.get(position); 

    TextView label  = (TextView)row.findViewById(R.id.textbike_numbr); 
    TextView sub   = (TextView)row.findViewById(R.id.textbike); 

     label.setText(tempValues.getBikeName()); 
     sub.setText(tempValues.getBikeNumber()); 
    return row; 
} 
} 
+0

最終BikeSpinner SCHED =新BikeSpinner()你的layout.xml在哪兒? –

+0

你如何獲得列表中的值? –

+0

感謝小小的陽光,vivek mishra。我添加了佈局。價值觀得到妥善處理。如果我從Spinner 1中選擇車輛,顏色會正確。但是,顏色(紅色)正在替換Spinner1的值,而不是加載到Spinner2中 –

回答

0

喜歡這張我與彼此

獨立的類設置下拉列表適配器

public void setListBikeName(String vr_id) 
{ vendor_id=vr_id; 
    List<VehicleGetSetter> contacts = ((NewCustomer)context).vdb.getVehicleListToCustomer(vendor_id); 
    for (VehicleGetSetter cn : contacts) { 
     final BikeSpinner sched = new BikeSpinner(); 
       if(cn.getModelYear().equals("")) 
        sched.setBikeName("Select Bike"); 
       else 
        sched.setBikeName(cn.getModelYear()); 
      ((NewCustomer)context).CustomListBikeName.add(sched); /** Take Model Object in ArrayList */ 
     }   
} 

public void setListBikeColor(String name){ 
    ((NewCustomer)context).CustomListBikeColor.clear(); 
    List<VehicleGetSetter> contacts = ((NewCustomer)context).vdb.getVehicleColorListToCustomer(vendor_id,name); 
    for (VehicleGetSetter cn : contacts) { 
     final BikeSpinner sched = new BikeSpinner(); 

        sched.setBikeName(cn.getColor()); 

       ((NewCustomer)context).CustomListBikeColor.add(sched); /** Take Model Object in ArrayList */ 
       ((NewCustomer)context).colorAdapterIntermediater(); 

     } 
} 

public void setListBikeNumber(String name,String color){ 
    ((NewCustomer)context).CustomListBikeNumber.clear(); 
    List<VehicleGetSetter> contacts = ((NewCustomer)context).vdb.getVehicleNumberListToCustomer(vendor_id,name,color); 
    for (VehicleGetSetter cn : contacts) { 
     final BikeSpinner sched = new BikeSpinner(); 

        sched.setBikeName(cn.getNumber()); 

       ((NewCustomer)context).CustomListBikeNumber.add(sched); /** Take Model Object in ArrayList */ 
       ((NewCustomer)context).numberAdapterIntermediater(); 

     } 
} 

public void setBikeAdapterValues(ArrayList<BikeSpinner> CustomListBikeName,BikeSpinnerAdapter adapter, 
     Spinner mySpinner,final String str,Resources res) { 
    res = context.getResources(); 
    Spinner temp=mySpinner; 
    adapter = new BikeSpinnerAdapter((NewCustomer) context, R.layout.bike_spinner, CustomListBikeName,res); 
    temp.setAdapter(adapter); 
    temp.setOnItemSelectedListener(new OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parentView, View v, int position, long id) { 
      if(str.equals("name")){ 
       bike_name_from_adapter = ((TextView) v.findViewById(R.id.textbike_numbr)).getText().toString(); 
        setListBikeColor(bike_name_from_adapter); 
       } 
      else if(str.equals("color")){ 
       bike_color_from_adapter = ((TextView) v.findViewById(R.id.textbike_numbr)).getText().toString(); 
       setListBikeNumber(bike_name_from_adapter, bike_color_from_adapter); 
      } 
      else if(str.equals("number")){ 
       bike_no_from_adapter = ((TextView) v.findViewById(R.id.textbike_numbr)).getText().toString(); 

       if(bike_no_from_adapter.length()>=10){ 
        ((NewCustomer)context).last_layout.setVisibility(View.VISIBLE);          
       } 
       else 
        ((NewCustomer)context).last_layout.setVisibility(View.GONE); 
       ((NewCustomer)context).passBikeValueToCustomer(bike_name_from_adapter,bike_color_from_adapter,bike_no_from_adapter); 
      } 

     } 

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

     } 
    }); 
} 

} 

在主類相關的多個微調實現:

bikeadapterobj.setListBikeName(_id);   
    bikeadapterobj.setBikeAdapterValues(CustomListBikeName,adapter,mySpinner,"name",res); 
    public void colorAdapterIntermediater(){ 
    bikeadapterobj.setBikeAdapterValues(CustomListBikeColor,coloradapter,cust_bike_color,"color",res); 
} 

public void numberAdapterIntermediater(){ 
    bikeadapterobj.setBikeAdapterValues(CustomListBikeNumber,numberadapter,cust_bike_number,"number",res); 
} 
public void passBikeValueToCustomer(String name,String colr,String bkno){ 

     bike_name=name; 
     bike_color=colr; 
     bike_reg_number=bkno; 
     Log.d("Bike Details : ", bike_name+" "+bike_color+" "+bike_reg_number); 
    } 
相關問題