Reputation: 11
I have searched everywhere, but cannot find a solution for my problem. I'm new to java and I'm trying to build an app, but for some reason I can't get past this problem. I get this error:
Error:(68, 33) error: no suitable constructor found for ArrayAdapter(OneFragment,int,String[]) constructor ArrayAdapter.ArrayAdapter(Context,int,int,List) is not applicable (actual and formal argument lists differ in length) constructor ArrayAdapter.ArrayAdapter(Context,int,List) is not applicable (actual argument OneFragment cannot be converted to Context by method invocation conversion) constructor ArrayAdapter.ArrayAdapter(Context,int,int,String[]) is not applicable (actual and formal argument lists differ in length) constructor ArrayAdapter.ArrayAdapter(Context,int,String[]) is not applicable (actual argument OneFragment cannot be converted to Context by method invocation conversion) constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable (actual argument OneFragment cannot be converted to Context by method invocation conversion) constructor ArrayAdapter.ArrayAdapter(Context,int) is not applicable (actual and formal argument lists differ in length)
The code (OneFragment.java):
package memo.javahelps.com.hanzesoep;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class OneFragment extends Fragment{
ListView list;
String[] itemname = {
"Kippensoep",
"Tomatensoep",
"Thaise Kippensoep"
};
Integer[] imgid = {
R.mipmap.kippensoep,
R.drawable.tomatensoep,
R.drawable.thaisekip,
};
public OneFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_one, container, false);
CustomListAdapter adapter = new CustomListAdapter(getActivity(), itemname, imgid);
list=(ListView) rootView.findViewById(R.id.list);
list.setAdapter(adapter);
return rootView;
/* op item klikken */
ArrayAdapter adapter2 = new ArrayAdapter<String>(OneFragment.this,
R.layout.list_item, itemname);
list.setAdapter(adapter2);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (position) {
case 0:
Intent newActivity = new Intent(OneFragment.this.getActivity(), Kippensoep.class);
startActivity(newActivity);
break;
}
}
});
}
}
I'm trying to get separate items in a listview clickable with arrayadapter. Can anyone help me? What am I doing wrong?
Upvotes: 0
Views: 1794
Reputation: 393841
The ArrayAdapter
constructor most similar to your constructor invocation is ArrayAdapter.ArrayAdapter(Context,int,String[])
, but it is not applicable since actual argument OneFragment cannot be converted to Context by method invocation conversion
.
Here
ArrayAdapter adapter2 = new ArrayAdapter<String>(OneFragment.this,
R.layout.list_item, itemname);
you are passing a Fragment
instance to the ArrayAdapter
where a Context
is required. You should pass the enclosing Activity
instead:
ArrayAdapter adapter2 = new ArrayAdapter<String>(getActivity(),
R.layout.list_item, itemname);
In addition, return rootView;
should be the last statement in your onCreateView
method. Otherwise, the code that follows it will be unreachable.
Upvotes: 1