在Android活动中添加多个动态微调器视图,而不是在第二个微调器中加载选定的微调器数据

时间:2016-06-06 06:21:21

标签: android android-layout spinner android-spinner dynamic-view

  1. 在我的应用程序中,我有动态旋转器。

  2. 方法中共有2个旋转器,两者都是相关的(SPinner1,Spinner2)。

  3. 我可以多次调用该方法。

  4. 从本地数据库加载微调器数据。

  5. 问题:

    我两次调用该方法所以活动显示完全4个旋转器。

           1a.spinner1
           1b.SPinner2
    
           2a.Spinner1
           2b.Spinner2
    

    根据我选择的第三个Spinner,第四个Spinner数据加载完美。

    但基于第一个微调器所选项目数据未显示在第二个微调器中,因为最后一个迭代数据(第三个和第四个微调器)仅存储在字符串数组中。

    请帮我解决这个问题。显示所有微调器中的数据。

    我创建动态SPinners的方法

    // Helper for inflating a row Dynamic SPINNER
    
    private void inflateDynamic_SpinnerRow(String ObjectidTypeIdVal,final String LabelvalDynamic,String ValidationID,String SavedFormidValString
                , final String FormObjectIDValue)
        {
    
    
            Log.i("Dynamic Spinner", LabelvalDynamic+"Form OBJECT Id"+FormObjectIDValue);
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View rowView123 = inflater.inflate(R.layout.rowfor_dynamic_spinner_with_label, null);
            final TextView Textview_Label1 = (TextView) rowView123.findViewById(R.id.textViewa1Labelxml);
            final TextView Textview_Label2 = (TextView) rowView123.findViewById(R.id.textViewa2Labelxml);
            RelativeLayoutDynamic2=(RelativeLayout) rowView123.findViewById(R.id.Realtive2);
    
    
            DynamicSpinner123 = (Spinner) rowView123.findViewById(R.id.DynamicSpinner1xml);
    
            DynamicSpinner123Dependent2=(Spinner)rowView123.findViewById(R.id.DynamicSpinner2xml);
    
    
            DynamicSpinnerarr= new ArrayList<String>();
            DynamicSpinnerarr_IdValue= new ArrayList<String>();
    
    
    
          //  DynamicSpinnerDependdentarr= new ArrayList<String>();
    
    
            if (!LabelvalDynamic.equals(""))
            {
    
    
    //Converting string to Array list
    
                        try
                        {
    
    
                            MobileServiceList<th_tbdynamicspinner> results09;
                            results09 = mToDoTable_Form_DynamicSPinner_Table88_Local_Database.read(mPullQuery12378).get();
    
                            //Offline Sync
                            if(!results09.isEmpty())
                            {
    
    
                                Log.i("Data Is Available ", "From Lcoal>>>>>>" + "Data  DynamicSPinner");
    
    
                                for (int i = 0; i < results09.size(); i++)
                                {
    
                                    Boolean  mComplete_BooleanState=results09.get(i).isComplete();
    
                                    Dynamic_Spinner_ID_StringVal1_TableValue3=results09.get(i).getDynamic_Spinner_Id_StringOne();
                                    Form_Object_ID_StringVal1_TableValue3=results09.get(i).getForm_Object_Id_StringOne();
                                    Table_Name_StringVal1_TableValue3=results09.get(i).getTable_Name_StringOne();
                                    ID_Column_StringVal1_TableValue3=results09.get(i).getID_Column_StringOne();
                                    Data_Column_StringVal1_TableValue3=results09.get(i).getData_Column_StringOne();
                                    Data_Condition_StringVal1_TableValue3=results09.get(i).getData_Condition_StringOne();
                                    Dependent_SPinner_StringVal1_TableValue3=results09.get(i).getDependent_Spinner_StringOne();
                                    FormID_value_StringVal1_TableValue3=results09.get(i).getFormId_Val_StringOne();
    
                                    Log.i("Local Database Listview", Table_Name_StringVal1_TableValue3 + ":" + Form_Object_ID_StringVal1_TableValue3);
    
                                   /* DynamicSpinnerarr.add(Dependent_SPinner_StringVal1_TableValue3);
                                    DynamicSpinnerarr_IdValue.add(ID_Column_StringVal1_TableValue3);*/
    
                                    Collections.addAll(DynamicSpinnerarr,Dependent_SPinner_StringVal1_TableValue3);
                                    Collections.addAll(DynamicSpinnerarr_IdValue,ID_Column_StringVal1_TableValue3);
    
                                    if(Dependent_SPinner_StringVal1_TableValue3.equals(""))
                                    {
                                        Log.i("SPINN", "Spinner Data NotFound from Local");
                                    }
                                    else
                                    {
                                        Log.i("Listview ", "Dynamic Spinner Data AVAILABLE");
    
                                        String Data[]=LabelvalDynamic.split(",");
                                        Textview_Label1.setText(Data[0].toString());
                                        Textview_Label2.setText(Data[1].toString());
                                        arrayadpFIrstDynamic=new ArrayAdapter<String>(Form_DetailsAddPage.this,android.R.layout.simple_spinner_dropdown_item,DynamicSpinnerarr);
                                        arrayadpFIrstDynamic.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                                        DynamicSpinner123.setAdapter(arrayadpFIrstDynamic);
    
                                    }
    
    
                                }
    
                            }
                            else
                            {
    
                                Log.i("Dyanmic SPinner ", "Data NotFound in Local");
    
                            }
    
                        } catch (Exception e)
                        {
    
                            e.printStackTrace();;
                            Log.i("Local In Listview", "Exception Occur......!");
                        }
    
            }
            else
            {
                mExclusiveEmptyView = rowView123;
                //deleteButton.setVisibility(View.INVISIBLE);
    
                Textview_Label1.setText("NOt Avilable");
            }
    
    
    /*Item Selected in First Dynamic Spinner :-)*/
            DynamicSpinner123 .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
            {
    
                @Override
                public void onItemSelected(AdapterView<?> spinner123, View view, int position, long id)
                {
                    try
                    {
                        DynamicSpinnerDependdentarr= new ArrayList<String>();
    
                        String selectedItem = spinner123.getItemAtPosition(position).toString();
    
                        for (String member : DynamicSpinnerarr)
                        {
                            Log.i("Member name: ", member);
                        }
                        DYNAMICSPINNER_VALUE_ARRAY=new String[DynamicSpinnerarr.size()];
                        DYNAMICSPINNER_VALUE_ARRAY=DynamicSpinnerarr.toArray(DYNAMICSPINNER_VALUE_ARRAY);
    
                        DYNAMICSPINNER_ID_VALUE_ARRAY=new String[DynamicSpinnerarr_IdValue.size()];
                        DYNAMICSPINNER_ID_VALUE_ARRAY=DynamicSpinnerarr_IdValue.toArray(DYNAMICSPINNER_ID_VALUE_ARRAY);
    
    
    
                        Log.i("String Selcted",selectedItem);
                        Log.i(" MY ARRAY Selected :-)",Arrays.toString(DYNAMICSPINNER_VALUE_ARRAY));
    
    
              /*Finding the Id related to Dynamic Spinner*/
                        for(int i=0;i<DYNAMICSPINNER_VALUE_ARRAY.length;i++)
                        {
    
    
                            if(selectedItem.equals(DYNAMICSPINNER_VALUE_ARRAY[i]))
                            {
    
                                DynamicSpinnerIdString=DYNAMICSPINNER_ID_VALUE_ARRAY[i];
                                Log.i("DYNAMIC ID",DynamicSpinnerIdString);
    
                                // break;
                            }
    
                        }
    
    
    /*For Getting Spinner LocalDatabase Data*/
    
    
    
                        MobileServiceList<th_tbdynamicspinnerdependenttable> results678;
                        results678 = mToDoTable_Form_DynamicSPinnerDependent_Table77_Local_Database.read(mPullQuery1234).get();
    
    
                        //Offline Sync
                        if(!results678.isEmpty())
                        {
    
                            RelativeLayoutDynamic2.setVisibility(View.VISIBLE);
    
                            Log.i("Data Is Available ", "From Lcoal>>>>>>" + "Data SPinner");
    
    
                            for (int i = 0; i < results678.size(); i++)
                            {
    
    
                                Boolean  mComplete_BooleanState=results678.get(i).isComplete();
                                DyncamicDependent_ColumnIDStringVal1_TableValue2 = results678.get(i).getId_Column_StringOne();
                                DynamicDependent_CITYList_VALUE_StringVal1_TableValue2 = results678.get(i).getCity_List_StringOne();
    
                                Log.i("Local Database Listview", DyncamicDependent_ColumnIDStringVal1_TableValue2 + ":" + DynamicDependent_CITYList_VALUE_StringVal1_TableValue2);
    
                                Collections.addAll(DynamicSpinnerDependdentarr, DynamicDependent_CITYList_VALUE_StringVal1_TableValue2.split(","));
    
    
                                if(DyncamicDependent_ColumnIDStringVal1_TableValue2.equals(""))
                                {
                                    Log.i("SPINN", "Spinner Data NotFound from Local");
    
                                }
                                else
                                {
                                    Log.i("Listview ", "Spinner Data AVAILABLE");
                                    // TextviewLabel.setText(Labelval.toString());
                                    arrayadp2_Dependent=new ArrayAdapter<String>(Form_DetailsAddPage.this,android.R.layout.simple_spinner_dropdown_item,DynamicSpinnerDependdentarr);
                                    arrayadp2_Dependent.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                                    DynamicSpinner123Dependent2.setAdapter(arrayadp2_Dependent);
    
                                }
                            }
    
                        }
                        else
                        {
    
                            Log.i("DynamiceSPinner ", "Dependent Data NotFound in Local");
    
                            RelativeLayoutDynamic2.setVisibility(View.GONE);
    
                        }
    
    
                    } catch (Exception e)
                    {
                        e.printStackTrace();
                        Log.i("","Spinner Error  OnItemSelectedClick");
    
                    }
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> arg0)
                {
                    // TODO Auto-generated method stub
                }
            });
    
    
    
            DynamicSpinner123.setId(viewsCount++);
            allViews.add(DynamicSpinner123);
            allValidation_arr.add(ValidationID);
            allObjectid_arr.add(ObjectidTypeIdVal);
            allSavedformid_arr.add(SavedFormidValString);
            allformObject_id_arr.add(FormObjectIDValue);
    
            DynamicSpinner123Dependent2.setId(viewsCount++);
            allViews.add(DynamicSpinner123Dependent2);
            allValidation_arr.add(ValidationID);
            allObjectid_arr.add(ObjectidTypeIdVal);
            allSavedformid_arr.add(SavedFormidValString);
            allformObject_id_arr.add(FormObjectIDValue);
    
    
            // Inflate at the end of all rows
            mContainerView.addView(rowView123);
    
        }
    

2 个答案:

答案 0 :(得分:1)

public class DynamicSpiner extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    //main spinner data
    String firstSpinnerArray[] = new String[]{"One", "Two", "Three", "Four"};
    //dependent spinner data in arrays
    List<String[]> dependentDataList = new ArrayList<>();

    //initialize main adapter and dependent adapter
    ArrayAdapter<String> adapter, adapterDependent;
    //initialize main Spinner and dependent Spinner
    Spinner spinner, spinnerDependent;
    //creating map for dependent Spinner to load the data according to its parent spinner
    HashMap<Integer, Spinner> hashMap = new HashMap<Integer,Spinner>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dynamic_spiner);
        //add data in List for dependentSpinner data
        dependentDataList.add(new String[]{"One-1", "One-2", "One-3", "One-4"});
        dependentDataList.add(new String[]{"Two-1", "Two-2", "Two-3", "Two-4"});
        dependentDataList.add(new String[]{"Three-1", "Three-2", "Three-3", "Three-4"});
        dependentDataList.add(new String[]{"Four-1", "Four-2", "Four-3", "Four-4"});

        adapter = new ArrayAdapter<String>(DynamicSpiner.this, android.R.layout.simple_list_item_1);
        adapterDependent = new ArrayAdapter<String>(DynamicSpiner.this, android.R.layout.simple_list_item_1);


        for (int i=0; i<firstSpinnerArray.length; i++){
            spinner = new Spinner(DynamicSpiner.this);
            spinnerDependent = new Spinner(DynamicSpiner.this);
            spinner.setId(i);
            spinnerDependent.setId(i);
            hashMap.put(i, spinnerDependent);
            spinner.setOnItemSelectedListener(this);
            adapter.add(firstSpinnerArray[i]);
        }
        spinner.setAdapter(adapter);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String getDependentArray[] = dependentDataList.get(position);
        for(String string : getDependentArray){
            adapterDependent.add(string);
        }
        int spinnerId = view.getId();
        if(spinnerId == position){
            Spinner getSpinnerDependent = hashMap.get(position);
            getSpinnerDependent.setAdapter(adapterDependent);
        }
    }

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

    }
}

我没有测试过这个。希望对你有所帮助。

答案 1 :(得分:0)

在向mContainerView.addView(rowView123);添加视图之前

首先删除以前的子视图。

//snippet.

//remove all views
mContainerView.removeAllViews();
//add new views
mContainerView.addView(rowView123);