Issue
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:
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:
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.
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!
Solution
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!
Answered By - DavidZam
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.