문제

I am working on this activity that I have a bunch of dynamic spinners. To set the items of each one of those I've set one List attribute for each SpinnerAdapter.

To manage what spinner items i'll to update I've created a private method which is supposed to work like a factory pattern but only returning the List for a specific Spinner that I need to update.

In my mind (at least thinking in objective-c) this code was supposed to work.

package com.package.demo;

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

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

public class SomeActivity extends Activity implements OnItemSelectedListener {

    private int activitySpinners[] = {
            R.id.spinner_a,
            R.id.spinner_b,
            R.id.spinner_c
    };

    private List<String> spinnerAdapterItemsA;
    private List<String> spinnerAdapterItemsB;
    private List<String> spinnerAdapterItemsC;


    private List<String> teste ;

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

        setContentView(R.layout.activity_orders);

        for (int i = 0; i < this.activitySpinners.length; i++) {
            int spinnerId = this.activitySpinners[i];
            Spinner spinner = (Spinner) findViewById(spinnerId);
            this.clearArrayAdapterItemsWithPrompt(spinnerId);
            this.updateArrayAdapterItemsWithPrompt(spinnerId);
            spinner.setOnItemSelectedListener(this);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item, this.adapterItemsForSpinnerId(spinnerId));
            adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
            spinner.setAdapter(adapter);
        }
    }

    private List<String> adapterItemsForSpinnerId(int spinnerId) {
        List<String> list = null;
        switch (spinnerId) {
            case R.id.spinner_a:
                list = this.spinnerAdapterItemsA;
                break;
            case R.id.spinner_b:
                list = this.spinnerAdapterItemsB;
                break;
            case R.id.spinner_c:
                list = this.spinnerAdapterItemsC;
                break;
            default:
                return list;
        }
        return list;
    }

    private void clearArrayAdapterItemsWithPrompt(int spinnerId) {
        List<String> list = this.adapterItemsForSpinnerId(spinnerId);
        list = new ArrayList<String>();
    }

    private void updateArrayAdapterItemsWithPrompt(int spinnerId) {
        Spinner spinner = (Spinner) findViewById(spinnerId);
        String prompt = (String) spinner.getPrompt();
        this.adapterItemsForSpinnerId(spinnerId).add(0, prompt);
    }

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        // TODO: Handling Stuff
    }

    public void onNothingSelected(AdapterView<?> parent) {
        // TODO: Handling Stuff
    }
}

But I am having java null pointer exception. When the updateArrayAdapterItemsWithPrompt try to set the prompt value for the List.

도움이 되었습니까?

해결책

The problem of your class is with your setter method for the "Factory attributes". You have null pointer exceptions because it was not set a new ArrayList in each of the private variables. To fix that you have to do the following setter. As hoaz said, the clearArrayAdapterItemsWithPrompt is useless because it not sets properly the variables. To fix you should have do the Following Code:

private void clearArrayAdapterItemsWithPrompt(int spinnerId) {
    setArrayAdapterItemsWithSpinnerId(spinnerId, new ArrayList<String>())
}

private void setArrayAdapterItemsWithSpinnerId(int spinnerId, List<String> list) {
    switch (spinnerId) {
        case R.id.spinner_a:
            this.spinnerAdapterItemsA = list;
            break;
        case R.id.spinner_b:
            this.spinnerAdapterItemsB = list;
            break;
        case R.id.spinner_c:
            this.spinnerAdapterItemsC = list;
            break;
        default:
        break;
}

And is valid to mention that maybe you may lose the styling for your Spinner maybe you will have to edit the method a little bit to fit your needs.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top