2017-03-07 162 views
1

在AlertDialog中使用微調器作爲過濾器。當我選擇一個項目後重新打開對話框時,我想默認選擇之前選擇的項目。但一些魔法正在發生,我不明白爲什麼微調選擇錯誤的項目。微調器的項目列表是一個字符串數組,其編號從4到42. {"4","6",.."42"} 當我選擇第一個項目"4"時,位置應爲0,當我重新打開對話框時,應顯示確切的項目。但是,相反,它會顯示在4位。Android - 微調器位置值

代碼Dialogfragment項目"10"

public class FittingSelectionDialogFragment extends DialogFragment { 

private String dTitle; 

    private String[] list; 

    private int position; 

    private String filterItem; 

    private List<String> chosenFilterItems; 

    public interface FittingSelectionDialogListener { 
     public void onChosenDialogItem(String filterStrings,int menuPosition); 
    } 

    FittingSelectionDialogListener mListener; 


    public Dialog onCreateDialog(Bundle savedInstanceState){ 
      final Spinner spinner2 = new Spinner(getActivity()); 
      final TextView label = new TextView(getActivity()); 
      final TextView label2 = new TextView(getActivity()); 
      LinearLayout layout = new LinearLayout(getActivity()); 

      layout.setOrientation(LinearLayout.HORIZONTAL); 
      layout.setGravity(Gravity.CENTER); 

      ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getContext(), R.layout.support_simple_spinner_dropdown_item, list); 
      spinner2.setAdapter(adapter2); 

      if(chosenFilterItems.get(position).equals("")) { 
       spinner2.setSelection(0); 
      } 
      else{ 
       spinner2.setSelection(Integer.valueOf(chosenFilterItems.get(position))); 
      } 

      label.setText("Rohr AD:"); 
      label.setTextColor(getResources().getColor(R.color.colorParkerBlack2)); 
      label.setTextSize(16); 

      label.setPadding(60,0,40,0); 

      label2.setText("mm"); 
      label2.setTextSize(16); 
      label2.setTextColor(getResources().getColor(R.color.colorParkerBlack2)); 

      dialog.setTitle(getdTitle()); 

      layout.addView(label); 
      layout.addView(spinner2); 
      layout.addView(label2); 

      dialog.setView(layout); 

      spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
       @Override 
       public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int spinner2Position, long id) { 
        filterItem = list[spinner2Position]; 
        chosenFilterItems.set(position,String.valueOf(spinner2Position)); 
        Toast.makeText(getContext(),chosenFilterItems.get(position),Toast.LENGTH_SHORT).show(); //correct value 
       } 

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

       } 
      }); 
      dialog.setPositiveButton("Bestätigen", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i) { 
        mListener.onChosenDialogItem(filterItem,position); 
        Toast.makeText(getContext(),chosenFilterItems.get(position),Toast.LENGTH_SHORT).show();//wrong value 
        dismiss(); 
       } 
      }); 
      dialog.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i) { 
        filterItem = ""; 
        chosenFilterItems.set(position,""); 
        mListener.onChosenDialogItem(filterItem, position); 
       } 
      }); 
      return dialog.create(); 
    } 
} 

我做了兩個敬酒聽衆裏面。瘋狂的是,在OnItemSelectedListener中顯示正確的位置值。在PositiveButton中,OnClickListener顯示錯誤的值。你有什麼想法嗎?

+0

走進調試器,看看發生了什麼事情。如果沒有這樣做很難。 – Vucko

+0

我看到你使用列表作爲地圖。爲什麼不使用map/HashMap呢? – gvlachakis

回答

0

使用Handler,並添加一些延遲

int delay = 1000; 
runOnUiThread (new Runnable() { 
     @Override 
     public void run() { 

      if(chosenFilterItems.get(position).equals("")) { 
       spinner2.setSelection(0); 
      } else { 
       Integer.valueOf(chosenFilterItems.get(position))); 
      } 
     } 
}, delay); 
1

我自己做了一個解決方法。我沒有通過調試發現錯誤。所以我不用這個職位。我使用該值重新選擇所選項目。不是最好的方式,但適用於這個小項目列表。

代碼:

if(chosenFilterItems.get(position).equals("")) { 
    spinner2.setSelection(0); 
} 
else{ 
    for(int i=0; i<list.length;i++){ 
     if(list[i].equals(chosenFilterItems.get(position))){ 
      spinner2.setSelection(i); 
     } 
    }  
} 

spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
@Override 
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int spinner2Position, long id) { 
    filterItem = list[spinner2Position]; 
} 

dialog.setPositiveButton("Bestätigen", new DialogInterface.OnClickListener() { 
@Override 
public void onClick(DialogInterface dialogInterface, int i) { 
    chosenFilterItems.set(position,filterItem); 
    mListener.onChosenDialogItem(filterItem,position); 
    dismiss(); 
} 
});