简体   繁体   English

Android:在onCreate()方法之外更新ArrayAdapter

[英]Android: Update ArrayAdapter outside of onCreate() method

I have a onCreate method where I create one ArrayAdapter with a empty string array, after that I call one AsyncTask that return one String array which I want to update the ArrayAdapter with, but the way I'm doing it don't work: 我有一个onCreate方法,其中我用一个空的字符串数组创建一个ArrayAdapter,此后,我调用了一个AsyncTask,该AsyncTask返回了一个我要更新ArrayAdapter的String数组,但是这样做的方式不起作用:

public class FillTransportPlaceActivity extends AppCompatActivity {

    public static String[] lines = new String[13];
    public static ArrayAdapter<String> arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_fill_transport_place);

         arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, lines);
         new transportDB().execute(metroText); // AsyncTask method which return me the String[] array.
         MaterialBetterSpinner materialDesignSpinner = findViewById(R.id.listLines);
         materialDesignSpinner.setAdapter(arrayAdapter);
    }
}

In the onPostExecute(String[] result) method of the AsyncTask I call updateAdapter(String[] result) function: 在AsyncTask的onPostExecute(String [] result)方法中,我调用updateAdapter(String [] result)函数:

private class transportDB extends AsyncTask<String, String[], String[]> {

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected String[] doInBackground(String... strings) {
       return DB_transportPlace.getLines(strings[0]);
    }

    @Override
    protected void onPostExecute(String[] result) {
       updateAdapter(result);
    }

    public static void updateAdapter(String[] result) {
       for(int i = 0; i < 14; i++) {
          arrayAdapter.add(result[i]);
       }
       //arrayAdapter.addAll(lines);
       //arrayAdapter.notifyDataSetChanged();
    }

As you can see I've tried different methods like addAll() and notifyDataSetChanged() but I'm not sure if I'm using this methods propertly. 如您所见,我尝试了addAll()和notifyDataSetChanged()之类的其他方法,但不确定是否正确使用了这些方法。

The thing I want to do is fill the MaterialDesignSpinner with the content of the array returned by the AsyncTask, if anyone knows any other options to do that I'd apreciate any advice, thank you and any help will be apreciated! 我想做的是用AsyncTask返回的数组的内容填充MaterialDesignSpinner,如果有人知道有其他选择可以解决我的问题,谢谢您,我们将为您提供帮助!

first of all you shouldn't set array with null object inside to array adapter it lead to NullPointerException 首先,您不应该在数组适配器内部设置带有null对象的数组,这会导致NullPointerException

you can update your arrayAdapter like this : 您可以像这样更新arrayAdapter:

 sp = findViewById(R.id.someSpinner);

ArrayList<String> list = new ArrayList<>();

final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, list);


sp.setAdapter(adapter);

list.add("Hello");
list.add("How are you");
adapter.notifyDataSetChanged();
 public static void updateAdapter(String[] result) {

         arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, result);
     materialDesignSpinner.setAdapter(arrayAdapter);
    }

try above code 试试上面的代码

None of the answers served to me, but I finally found a solution, I post it here in case of someone need it: 没有答案对我有用,但是我终于找到了解决方案,如果有人需要,可以在这里发布:

public class FillTransportPlaceActivity extends AppCompatActivity {

    private ArrayList<String> list;
    private ArrayAdapter<String> arrayAdapter;
    private MaterialBetterSpinner spinner;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fill_transport_place);

        Toolbar tb = findViewById(R.id.toolbar_center);
        setSupportActionBar(tb);
        ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setHomeAsUpIndicator(R.drawable.ic_arrow_back);

        list = new ArrayList<>();
        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
        spinner = findViewById(R.id.listLines);


        SharedPreferences sp = getApplicationContext().getSharedPreferences("transportButton", 0);
        boolean metro = sp.getBoolean("metro", false);
        boolean bus = sp.getBoolean("bus", false);

        if(metro) {
            String metroText = "metro";
            new transportDB().execute(metroText);
            spinner.setAdapter(arrayAdapter);
        } else {
            if (bus) {
                String busText = "bus";
                new transportDB().execute(busText);
                spinner.setAdapter(arrayAdapter);
            }
        }
    }
}

    public void updateAdapter(String[] result) {
        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, result);
        spinner.setAdapter(arrayAdapter);
}

Thanks so much for the help! 非常感谢你的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM