Android Spinner值在新Activity中不会更改

时间:2017-06-16 23:52:48

标签: java android

我在我的应用程序中使用微调器打开相同的Activity,但使用不同的值来下载Json文件并显示它们。第一个问题是,当我在onItemSelected方法中添加一个微调器值并且在onItemSelected方法中使用if语句(if(position> 0))查询之前,微调器不起作用活动。它是一个解决方案但不是很好。

下一个问题是,微调器的值每次都显示微调器列表的0位置。当我制作一个数组来填充微调器或不同的字符串时,它是一样的。用不同的json文件打开相同的Activity工作正常,但是微调器始终在位置0值。

public class FragmentResult extends Fragment implements
        GestureOverlayView.OnGesturePerformedListener, AdapterView.OnItemSelectedListener {
    private ArrayList<ResultModel> arrayListResult = new ArrayList<>();
    public static int dayOfMatch = 34;
    private GestureLibrary gestureLibrary;
    Spinner spinnerDay;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //matchDayCheck();

        ServerResult serverResult = new ServerResult(getActivity().getApplicationContext(),
                "https://www.dein-weg-in-die-cloud.de/tomcat7/RestSoccer/fussball/spieltag/" + dayOfMatch);
        serverResult.execute();

        String downResult = null;
        try {
            downResult = serverResult.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        JsonParser jsonParser = new JsonParser();
        arrayListResult = jsonParser.parseJSONResult(downResult);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        GestureOverlayView gestureOverlayView = new GestureOverlayView(getActivity());
        View view = inflater.inflate(R.layout.layout_result, container, false);

        ListView result_list = (ListView)view.findViewById(R.id.result_list);
        AdapterResult adapterResult = new AdapterResult(getActivity().getApplicationContext(), arrayListResult);
        result_list.setAdapter(adapterResult);

        gestureOverlayView.addView(view);
        gestureOverlayView.setFadeEnabled(false);
        gestureOverlayView.addOnGesturePerformedListener(this);
        gestureOverlayView.setGestureColor(Color.TRANSPARENT);
        gestureOverlayView.setUncertainGestureColor(Color.TRANSPARENT);
        gestureOverlayView.setFadeOffset(0);
        gestureOverlayView.setHapticFeedbackEnabled(true);
        gestureLibrary = GestureLibraries.fromRawResource(getActivity().getApplicationContext(), R.raw.gestures); //!!
        if (!gestureLibrary.load()) {
            getActivity().finish();
        }

        // Spinner element
        Spinner spinnerYear = (Spinner)view.findViewById(R.id.spinnerResultYear);
        spinnerDay = (Spinner)view.findViewById(R.id.spinnerResultDay);

        // Spinner click listener
        spinnerYear.setOnItemSelectedListener(this);
        spinnerDay.setOnItemSelectedListener(this);


        // Spinner Drop down elements
        List<String> spinnerListYear = new ArrayList<String>();
        spinnerListYear.add("2013/2014");

        List<String> spinnerListDay = new ArrayList<String>();

        spinnerListDay.add("");
        for(int i = 1; i<=34; i++){
            spinnerListDay.add(i + ".Spieltag");
        }

        // Creating adapter for spinner
        ArrayAdapter<String> spinnerYearAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_spinner_item, spinnerListYear);
        ArrayAdapter<String> spinnerDayAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_spinner_item, spinnerListDay);

        // Drop down layout style - list view with radio button
        spinnerYearAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerDayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        spinnerYear.setAdapter(spinnerYearAdapter);
        spinnerDay.setAdapter(spinnerDayAdapter);


        /*result_list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                ResultModel resultModel = (ResultModel) parent.getItemAtPosition(position);
                int matchId = resultModel.getMatchId();
                Intent matchIntent = new Intent(getActivity(), MatchActivity.class);
                matchIntent.putExtra("MatchId", matchId);
                matchIntent.putExtra("Matchday", dayOfMatch);
                startActivity(matchIntent);
            }
        });*/

        return gestureOverlayView;
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        // On selecting a spinner item
        String item = parent.getItemAtPosition(position).toString();

        if(position >0) {

            final Intent intent;
            intent = new Intent(getActivity().getApplicationContext(), ResultActivity.class);
            dayOfMatch = position;
            intent.putExtra("dayOfMatch", dayOfMatch);

            startActivity(intent);
        }
    }

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

    }
.
.
.

}

0 个答案:

没有答案