我有一个onCreate方法,我在其中创建一个带有空字符串数组的ArrayAdapter,之后我调用一个AsyncTask返回一个我想用ArrayAdapter更新的String数组,但是我这样做的方式是& #39; t work:
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);
}
}
在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();
}
正如您所看到的,我尝试过不同的方法,例如addAll()和notifyDataSetChanged(),但我不确定我是否正在使用这种方法。
我想要做的是用AsyncTask返回的数据内容填充MaterialDesignSpinner,如果有人知道其他任何选项,我会提供任何建议,谢谢你,任何帮助都会被贬值!
答案 0 :(得分:0)
首先你不应该在数组适配器中将带有null对象的数组设置为导致NullPointerException
您可以像这样更新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();
答案 1 :(得分:0)
public static void updateAdapter(String[] result) {
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, result);
materialDesignSpinner.setAdapter(arrayAdapter);
}
尝试上面的代码
答案 2 :(得分:0)
这些答案都没有提供给我,但我终于找到了解决方案,我在这里发布以防有人需要它:
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);
}
非常感谢你的帮助!