0
Good afternoon, everyone,
I am trying to send an Arraylist of an Activity to a Fragment that will use it in a Gridview, but I have not been successful.
When clicking the button the Activity creates a vector that is transformed into an Arraylist and then should be sent to the "Selectfragment" Fragment. The snippet of the Activity code that executes this follows below.
btnBuscaRota.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
csrAux = mDb.rawQuery("SELECT * FROM sqlite_master WHERE (TYPE = 'table' AND " +
"name <> 'usuarios' AND name <> 'sqlite_sequence' AND name <> 'historico' AND name <> 'android_metadata')", null);
csrAux.moveToFirst();
int total = csrAux.getCount();
String[] numbers = new String[total];
int contador = 0;
while (!csrAux.isAfterLast()) { // cria o vetor "Numbers"
numbers[contador] = csrAux.getString(csrAux.getColumnIndex("name"));
contador++;
csrAux.moveToNext();
}
list = new ArrayList<String>(Arrays.asList(numbers)); // transforma o vetor "numbers" em uma ArrayList - "list" declarada como "public"
Bundle bundle = new Bundle();
bundle.putStringArrayList("numbers", list);
SelecaoFragment yourFragment = new SelecaoFragment();
yourFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new SelecaoFragment())
.commit();
}
});
The snippet of the Fragment code that is called by Activity follows below.
rotas = getArguments().getStringArrayList("numbers"); // "rotas" declarada como ArrayList public
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, rotas);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) { ;
teste.setText(rotas.get(position).toString());
}
Thank you very much to those who can help me to correct the problem.