为什么不能解决方法?

时间:2015-11-21 01:28:05

标签: java android

我正在尝试创建一个包含2个字符串数组的构造函数,但它说它无法解析该方法。

我正在尝试制作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;
}

这只是相关代码。但是,我真的不明白为什么它会给我这个错误:Cannot resolve method 'super(android.app.Activity, int, java.lang.String[], java.lang.String[])

我需要它包含一个活动,2个字符串数组和一个整数数组

修改 这是我的新CustomListAdapter课程

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;

 };
}

这是OnCreate方法:

   @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();
        }
    });

1 个答案:

答案 0 :(得分:2)

您应该将数据包装到容器中并创建这些容器的列表,然后将这些容器提供给ArrayAdapter

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

适合

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;
    }
}

并成为

Container[] containers;

然后可以输入超级构造函数

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

这样,所有阵列的大小都不会不匹配。

总而言之:

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;
}

然后您可以像getView一样访问@Override public View getView(int position, View convertView, ViewGroup parent) { Container currentItem = getItem(position); ... return rowView; } 中的数据:

$http