Fragments and how to pass activitys infections to them

Asked

Viewed 46 times

0

I have to pass an Adapter string array of an Activity, which is where my database is loading into a Fragment, where I want to set this in a list, but nothing I try to do right. Would anyone have any idea?


   // Banco

    try{
        database=new DataBase(this);

        conn = database.getWritableDatabase();

        res = new Repositorio(conn);
        res.testeinserir();


        // esse adpter recebe a lista vindo do banco 
        this.adpter = res.ListContas(this);


        Toast.makeText(this, "Conexão feita com sucesso!", Toast.LENGTH_SHORT).show();

    }
    catch(SQLException ex){
        Toast.makeText(this,"Erro ao se conectar ao banco!",Toast.LENGTH_SHORT).show();
    }

     //Aqui é onde mando a lista para o metodo setar lista que ta dentro da Classe Contas, que eu instanciei como adp  

    Runnable r = new Runnable() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if(adp != null){
                        adp.SetarLista(adpter);
                    }
                }
            });
        }
    };
    Thread t = new Thread(r);
    t.start();

// This is where the list is set with adpter, within the accounts class public void Setarlista(Arrayadapter adpter){ this.lista_contas . setAdapter(adpter); }

  • You could post what you’ve tried?

  • look down my most recent attempt, but I’ve tried through the Bundle or even set searching for the id of the listview , if you have any idea... but thanks in advance

  • @BM_DNS, first you didn’t have to connect to the bank so direct from Activity, I advise you to create a class responsible for this. Following example: https://goo.gl/BDXCS8. With this you can search wherever you want in Activity and Fragment, but if you want to search in Activity and pass to Fragment use the standard to pass parameters to Fragments. https://stackoverflow.com/a/17436739/5140172

  • @Ezequielmessore wrong user, Obs. Who asks the question always receives notification, no mention is required

  • @BM_DNS opa foi mals did not know it ... vlw by touch.

  • @Ezequielmessore of good, happens

  • Vlew guy, helped a lot ... really did not know that it was not good to load in Activity... To kind that starting the studies, thank you very much helped!!!

  • But here, there’s a way to pass an Arrayadapter through the Bundle??

Show 3 more comments

1 answer

0

Create a custom Adapter and implement the Serializable interface. To use in Listview I use the Base Adapter:

public class MeuAdapter extends BaseAdapter implements Serializable

This way, it will be possible to pass the instance of this Adapter through the Bundle with putSerializable("meuAdapter", meuAdapter).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.