无法在onResponse

时间:2018-04-29 09:47:02

标签: android

我正在尝试在onResponse请求中使用适配器,但是当我必须将上下文传递给适配器时,我遇到了一些问题。

这是错误:

  

CodiciAdapter中的CodiciAdapter()无法应用于:预期   参数:Actual Arguments:context:android.content.Context   这个(anonymous..Codici>>)项目:   java..Codici> R.layout.activity_listview(INT)   响应(java..Codici&GT)

这是我的要求:

Override
        public void onResponse(List<Codici> response) {


            //ArrayAdapter adapter = new ArrayAdapter<String>(this,
             //       R.layout.activity_listview, mobileArray);
            ListView listView = (ListView) findViewById(R.id.codici_list);


            CodiciAdapter codiciAdapter = new CodiciAdapter(this, R.layout.activity_listview, response); //what should I use instead of this??

            listView.setAdapter(codiciAdapter);

        }

这是我的适配器:

public class CodiciAdapter extends BaseAdapter {

private Context context;
private final List<Codici> items;

public CodiciAdapter(Context context, List<Codici> items) {
    this.context = context;
    this.items = items;
}

@Override
public int getCount() {
    return this.items.size();
}

@Override
public Codici getItem(int position) {
    return this.items.get(position);
}

@Override
public long getItemId(int position) {
    return this.items.get(position).hashCode();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    final Codici item = getItem(position);

    LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(convertView == null) {
        // If convertView is null we have to inflate a new layout

        convertView = inflater.inflate(R.layout.activity_listview, null);

        final CodiciViewHolder viewHolder = new CodiciViewHolder();
        viewHolder.name = (TextView) convertView.findViewById(R.id.name);

        // We set the view holder as tag of the convertView so we can access the view holder later on.
        convertView.setTag(viewHolder);
    }

    // Retrieve the view holder from the convertView
    final CodiciViewHolder viewHolder = (CodiciViewHolder) convertView.getTag();

    // Bind the values to the views
    viewHolder.name.setText(item.getName());

    return convertView;
}

}

2 个答案:

答案 0 :(得分:0)

只需从构造函数中删除布局文件

private void MyUserControl_SomethingHappened(object sender, MyEventArgs e)
{
    button1.Enabled = e.Enable;
    button2.Enabled = e.Enable;
    ...
    button7.Enabled = e.Enable;
}

答案 1 :(得分:0)

CodiciAdapter的第一个参数是ContextSee CodiciAdapter(Context context, List<Codici> items)

因此,当您在内部类this中使用this时,将指向当前类而不是外部类。

解决方法是此类是否为活动。然后使用下面的代码并删除额外的参数R.layout.activity_listview

Override
public void onResponse(List<Codici> response) {
    ListView listView = (ListView) findViewById(R.id.codici_list);
    CodiciAdapter codiciAdapter = new CodiciAdapter(YourActivityName.this, response); 
    listView.setAdapter(codiciAdapter);
}

或者在其他情况下,您需要提供Context