Hudhud
Hudhud

Reputation: 61

Why can't it resolve the method?

I'm trying to create a constructor with 2 string arrays, but it says that it can't resolve the method.

I'm trying to make a customlistadapter:

public class CustomListAdapter extends ArrayAdapter<String> {

private final String[] søgeord;
private final Activity context;
private final String[] itemname;
private final Integer[] imgid;
LayoutInflater inflater;

public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid, String[] søgeord) {
    super(context, R.layout.mylist, itemname, søgeord); //it can't resolve this

    this.context=context;
    this.itemname=itemname;
    this.imgid=imgid;
    this.søgeord=søgeord;
}

This is only the relevant code. However, I can't really see why it gives me this error: Cannot resolve method 'super(android.app.Activity, int, java.lang.String[], java.lang.String[])

I need it to contain an activity, 2 string arrays and an integer array

EDIT This is my new CustomListAdapter class

public class CustomListAdapter extends ArrayAdapter<Container>{

private final Activity context;
LayoutInflater inflater;

    public CustomListAdapter(Activity context, Container[] containers) {
        super(context, R.layout.mylist, containers);
        this.context=context;
    }
public View getView(int position,View view,ViewGroup parent) {
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    view = inflater.inflate(R.layout.mylist, null);

    TextView txtTitle = (TextView) view.findViewById(R.id.item);
    ImageView imageView = (ImageView) view.findViewById(R.id.icon);
    TextView extratxt = (TextView) view.findViewById(R.id.textView1);

    Container c = new Container();

    txtTitle.setText(c.itemname[position]);
    imageView.setImageResource(c.imgid[position]);
    extratxt.setText(c.itemname[position]);
    return view;

 };
}

This is the OnCreate method:

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);


    final String[] søgestreng = getIntent().getExtras().getStringArray("søg");


    filterText = (EditText)findViewById(R.id.sogefelt);
    ListView itemList = (ListView)findViewById(R.id.listView);
    listAdapter = new CustomListAdapter (this, Container[] containers); //this is wrong, why?

    itemList.setAdapter(listAdapter);
    itemList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(), "Position " + position, Toast.LENGTH_LONG).show();
        }
    });

Upvotes: 2

Views: 1569

Answers (1)

leonardkraemer
leonardkraemer

Reputation: 6793

You should wrap your data into containers and create a List of these containers which you then feed into the ArrayAdapter

String[] itemname, Integer[] imgid, String[] søgeord

fits into

public class Container{
    public final String søgeord;
    public final String itemname;
    public final Integer imgid;
    Container(String søgeord, String itemname, Integer imgid){
        this.søgeord=søgeord;
        this.itemname = itemname;
        this.imgid = imgid;
    }
}

and becomes

Container[] containers;

and can then be feed into the super constructor

super(context, R.layout.mylist, containers);

This way there can not be a mismatch between the sizes of all your arrays.

All in all:

public class CustomListAdapter extends ArrayAdapter<Container>

private final Activity context;
LayoutInflater inflater;

public CustomListAdapter(Activity context, Container[] containers) {
    super(context, R.layout.mylist, containers); 
    this.context=context;
}

then you can access the data in getView like this:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Container currentItem = getItem(position);
    ...
    return rowView;
}

Upvotes: 2

Related Questions