Fragment adapter issues

0

I'm having a problem with adapter in Fragment.

My 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 gives no error.

    
asked by anonymous 03.06.2014 / 03:31

1 answer

3

Although I put the code of class adapter as a comment, I believe I could see the error. The ContactImageAdapter class has a construtor that expects a context, int, ArrayList<Contact> data . You at the time you create your adapter in the fragment is not passing the argumentos correct to the parâmetros of the constructor of the adapter class.

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 still create the variables as fields of course and not as local variables.

    
03.06.2014 / 04:24