试图形成ArrayAdapter的问题

时间:2017-09-20 20:34:06

标签: android android-arrayadapter retrofit2

我知道这可能是一个简单的问题,但我很困惑为什么ArrayAdapter无法正常工作。我正在使用改造来回调数据,我正在获取数据,但当我尝试将该数据添加到ArrayAdapter时,我在ide中遇到以下错误:

Error:(49, 37) error: no suitable constructor found for ArrayAdapter(HomeActivity,int,OpenPulls)
constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable
(argument mismatch; OpenPulls cannot be converted to int)
constructor ArrayAdapter.ArrayAdapter(Context,int,OpenPulls[]) is not applicable
(argument mismatch; OpenPulls cannot be converted to OpenPulls[])
constructor ArrayAdapter.ArrayAdapter(Context,int,List<OpenPulls>) is not applicable
(argument mismatch; OpenPulls cannot be converted to List<OpenPulls>)

我确定分辨率可能就在那里,但我无法看到它。这是我的代码。

HomeActivity.java

public class HomeActivity extends AppCompatActivity {

  public CdenApi cdenAPI;
  private ArrayAdapter<OpenPulls> openPullsAdapter;
  public OpenPulls openpullsObject;

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

    cdenAPI = ((MyApplication) this.getApplication()).getCdenAPI();

    cdenAPI.getOpenPulls(cdenAPI.REQ_WITH, cdenAPI.CONT_TYPE,
            ((MyApplication) this.getApplication()).getAuthToken())
            .enqueue(new Callback<OpenPulls>() {

                @Override
                public void onResponse(Call<OpenPulls> call, Response<OpenPulls> response) {
                    if (response.isSuccessful()) {
                       openpullsObject = response.body();

                        openPullsAdapter =
                                new ArrayAdapter<OpenPulls>(HomeActivity.this, 0, openpullsObject) {
                                    @Override
                                    public View getView(int position,
                                                        View convertView,
                                                        ViewGroup parent){
                                        Currentpull currentItem = openpullsObject.currentpulls.get(position);

                                        if(convertView == null) {
                                            convertView = getLayoutInflater()
                                                    .inflate(R.layout.join_pulls_item, null, false);
                                        }

                                        TextView pullTo = (TextView)convertView.findViewById(R.id.storeToJoinTV);
                                        TextView pullFrom = (TextView)convertView.findViewById(R.id.storeFromJoinTV);

                                        pullTo.setText(currentItem.to.toString());
                                        pullFrom.setText(currentItem.from.toString());

                                        return convertView;
                                    }
                                };

                        final ListView itemsList = (ListView) findViewById(R.id.openPullsLV);
                        itemsList.setAdapter(openPullsAdapter);

                        Toast.makeText(HomeActivity.this, "Pulls returned Home.", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(HomeActivity.this, "Pulls not returned Home", Toast.LENGTH_SHORT).show();
                    }
                }
                @Override
                public void onFailure(Call<OpenPulls> call, Throwable t) {
                    t.printStackTrace();
                }
            });

  }

}

OpenPulls.java

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class OpenPulls {

  @SerializedName("currentpulls")
  @Expose
  public ArrayList<Currentpull> currentpulls = null;

  }

Currentpull.java

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Currentpull {

  @SerializedName("id")
  @Expose
  public Integer id;
  @SerializedName("created_at")
  @Expose
  public String createdAt;
  @SerializedName("updated_at")
  @Expose
  public String updatedAt;
  @SerializedName("pull_id")
  @Expose
  public Integer pullId;
  @SerializedName("box_count")
  @Expose
  public Integer boxCount;
  @SerializedName("status")
  @Expose
  public String status;
  @SerializedName("total_quantity")
  @Expose
  public Integer totalQuantity;
  @SerializedName("to")
  @Expose
  public String to;
  @SerializedName("from")
  @Expose
  public String from;

}

我只需要知道在尝试解析数据并将其传递给ListView时我做错了什么。

非常感谢您在这件事上的时间和帮助。

1 个答案:

答案 0 :(得分:1)

从错误:

Error:(49, 37) error: no suitable constructor found for ArrayAdapter(HomeActivity,int,OpenPulls)
constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable
(argument mismatch; OpenPulls cannot be converted to int)
constructor ArrayAdapter.ArrayAdapter(Context,int,OpenPulls[]) is not applicable
(argument mismatch; OpenPulls cannot be converted to OpenPulls[])
constructor ArrayAdapter.ArrayAdapter(Context,int,List<OpenPulls>) is not applicable
(argument mismatch; OpenPulls cannot be converted to List<OpenPulls>)

构造函数需要最后一个参数为int,或OpenPulls[]List<OpenPulls>。但是你将一个对象传递给构造函数:

openpullsObject = response.body();

openPullsAdapter =  new ArrayAdapter<OpenPulls>(HomeActivity.this, 0, 
                          openpullsObject) {...}

因此,您需要传递正确的参数。