Problems with Adapter in Fragment

Asked

Viewed 532 times

0

I’m having trouble with Adapter in Fragment.

Meu Fragment:

public class HomeActivity extends Fragment {

    private final DataBaseHandler db = new DataBaseHandler(getActivity());

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.main, container, false);    
        return rootView;
    }

    public void onActivityCreated (Bundle savedInstanceState){

        ArrayList<Contact> imageArry = new ArrayList<Contact>();
        ContactImageAdapter adapter;

        List<Contact> contacts = db.getAllContacts("Bancada");
        for (Contact cn : contacts) {
            //add contacts data in arrayList
            imageArry.add(cn);
        }

        // linha com erro!
        adapter = new ContactImageAdapter(this, R.layout.screen_list,imageArry);  

        ListView dataList = (ListView) findViewById(R.id.list); // linha com erro!
        dataList.setAdapter(adapter);
    }
}

Detail, in an Activity does not give error.

  • Which error gives? You have two lines with error?

  • Thank you Lucas! The constructor Contactimageadapter(Homeactivity, int, Arraylist<Contact>) is Undefined and The method findViewById(int) is Undefined for the type Homeactivity

  • Emerson, move that error (and all associated) code to after the inflate on onCreateView. In the place where this (onActivityCreated) the View has not yet been built, so the errors you found.

  • How so thank you? Thanks for what? @Emerson Barcellos I didn’t do anything, rsrsrsrs. Post error log please.

  • wakim, I did what you asked me and still the same mistake.

  • The page here is updating after...shit... Anyway I could post the constructor of the Contactimageadapter class?

  • public class ContactImageAdapter extends ArrayAdapter<Contact>{&#xA;Context context;&#xA;int layoutResourceId;&#xA;ArrayList<Contact> data=new ArrayList<Contact>();&#xA;public ContactImageAdapter(Context context, int layoutResourceId, ArrayList<Contact> data) {&#xA;super(context, layoutResourceId, data);&#xA;this.layoutResourceId = layoutResourceId;&#xA;this.context = context;&#xA;this.data = data;&#xA;}

  • @Emersonbarcellos post the code this way here in the comment is unscathed. Please edit your question and put the code there edited and cute please.

  • Apparently the error is to compile and not to Runtime. It could include the compiler and logcat message if you have?

Show 4 more comments

1 answer

4


Although I put the class code adapter as a comment I believe I was able to visualize the error. The class ContactImageAdapter has a construtor waiting for a context, int, ArrayList<Contact> data. You at the time creating your Adapter in Fragment are not passing the argumentos correct for the parâmetros of the class builder adapter.

Change this line:

adapter = new ContactImageAdapter(this, R.layout.screen_list,imageArry);  // linha com erro!

For this line:

adapter = new ContactImageAdapter( getActivity().getApplicationContext(), R.layout.screen_list, imageArry);  // linha sem erro!

Change this line:

ListView dataList = (ListView) findViewById(R.id.list); // linha com erro!

For this line:

ListView dataList = (ListView) getView().findViewById(R.id.list); // linha sem erro!

I recommend you leave your code this way:

public class HomeActivity extends Fragment {
    private final DataBaseHandler db = new DataBaseHandler(getActivity());
    private ListView dataList; // Seu ListView.

    // Neste método você inicializa tudo referente a seu layout, como por exemplo os componentes gráficos que tem na tela, neste casso, em R.layout.main
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.main, container, false);
        dataList = (ListView) rootView.findViewById(R.id.list); // criação do List View antes estava lá no onActivityCreate e agora veio para cá.

        return rootView;
    }

    public void onActivityCreated (Bundle savedInstanceState){
        ArrayList<Contact> imageArry = new ArrayList<Contact>();
        ContactImageAdapter adapter;

        List<Contact> contacts = db.getAllContacts("Bancada");
        imageArry.addAll( contacts ); // Pode adicionar assim ao invés de percorrer no for aprimorado e adicionar um por um.

        adapter = new ContactImageAdapter(getActivity().getApplicationContext(), R.layout.screen_list,imageArry);

        dataList.setAdapter(adapter);
    }
}

If you want you can also create variables as fields of course and not as local variables.

  • Okay Thanks a lot, I’ll try here.

  • 1

    Okay! Worked pretty good, you’re worth the guy!

  • 1

    Nothing @Emersonbarcellos. As for the errors you made, the first mistake was that the Adapter constructor wanted an object of the type Context and you weren’t passing by. You were passing the reference this of Fragment and not the context. The second mistake was that the method findViewById needed to be invoked from a View and in this case View would have to be your layout and you weren’t doing it. I was calling the method directly from the Fragment instance. Understood?

Browser other questions tagged

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