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?
– Lucas Santos
Thank you Lucas! The constructor Contactimageadapter(Homeactivity, int, Arraylist<Contact>) is Undefined and The method findViewById(int) is Undefined for the type Homeactivity
– Emerson Barcellos
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.– Wakim
How so thank you? Thanks for what? @Emerson Barcellos I didn’t do anything, rsrsrsrs. Post error log please.
– Lucas Santos
wakim, I did what you asked me and still the same mistake.
– Emerson Barcellos
The page here is updating after...shit... Anyway I could post the constructor of the Contactimageadapter class?
– Lucas Santos
public class ContactImageAdapter extends ArrayAdapter<Contact>{
Context context;
int layoutResourceId;
ArrayList<Contact> data=new ArrayList<Contact>();
public ContactImageAdapter(Context context, int layoutResourceId, ArrayList<Contact> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
– Emerson Barcellos
@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.
– Lucas Santos
Apparently the error is to compile and not to Runtime. It could include the compiler and logcat message if you have?
– Wakim