gpt4 book ai didi

java - Logcat错误DataAdapter类中的构造函数数据适配器无法应用

转载 作者:行者123 更新时间:2023-12-01 09:13:06 27 4
gpt4 key购买 nike

日志猫:

  required:Context, ArrayList<AndroidVersion>
found: ArrayList<AndroidVersion>
reason:actual and formal argument lists differ in length

我的 DataAdapter 中的 ArrayList 无法应用于 Android 版本中的数组列表

我相信这与上下文有关,但我不清楚需要修改哪一行。

MainActivity.java,其中“数据”一词引发错误。

   package davidfoale.info.relativelayout_json;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;

import android.util.Log;
import java.util.Arrays;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

private final String android_image_urls[] = {
"http://davidfoale.info/images/app_images/vetsurgeon.jpg",
"http://davidfoale.info/images/app_images/hairdresser.jpg",
"http://davidfoale.info/images/app_images/vetsurgeon.jpg",
"http://davidfoale.info/images/app_images/hairdresser.jpg",
"http://davidfoale.info/images/app_images/vetsurgeon.jpg",
"http://davidfoale.info/images/app_images/hairdresser.jpg",
"http://davidfoale.info/images/app_images/vetsurgeon.jpg",
"http://davidfoale.info/images/app_images/hairdresser.jpg",
"http://davidfoale.info/images/app_images/vetsurgeon.jpg",
"http://davidfoale.info/images/app_images/hairdresser.jpg",

};

private RecyclerView recyclerView;
private ArrayList<AndroidVersion> data;
private DataAdapter adapter;


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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}


private void initViews() {
recyclerView = (RecyclerView) findViewById(R.id.card_recycler_view);
recyclerView.setHasFixedSize(true);

RecyclerView.LayoutManager layoutManager
= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);

ArrayList androidVersions = prepareData();
DataAdapter adapter = new DataAdapter(getApplicationContext(), androidVersions);
recyclerView.setAdapter(adapter);

loadJSON();
}

private void loadJSON(){
Retrofit retrofit = new Retrofit.Builder()

.baseUrl("http://davidfoale.info/")
.addConverterFactory(GsonConverterFactory.create())
.build();

RequestInterface request = retrofit.create(RequestInterface.class);

Call<JSONResponse> call = request.getJSON();
call.enqueue(new Callback<JSONResponse>() {

@Override
public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {

JSONResponse jsonResponse = response.body();
data = new ArrayList<>(Arrays.asList(jsonResponse.getAndroid()));
adapter = new DataAdapter(data);
recyclerView.setAdapter(adapter);
}

@Override
public void onFailure(Call<JSONResponse> call, Throwable t) {
Log.d("Error",t.getMessage());
}
});
}

private ArrayList prepareData() {
ArrayList android_version = new ArrayList<>();
for (int i = 0; i < android_image_urls.length; i++) {
AndroidVersion androidVersion = new AndroidVersion();
androidVersion.setAndroid_image_url(android_image_urls[i]);
android_version.add(androidVersion);
}
return android_version;
}
}

DataAdapter.java

package davidfoale.info.relativelayout_json;

import android.content.Context;
import android.support.v4.view.ScrollingView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.content.Intent;

import android.widget.TextView;

import com.squareup.picasso.Picasso;
import java.util.ArrayList;




public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<AndroidVersion> android_versions, android;
private Context context;

public DataAdapter(Context context,ArrayList<AndroidVersion> android_versions) {
this.context = context;
this.android_versions = android_versions;
this.android = android_versions;
}




@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_layout, viewGroup, false);
return new ViewHolder(view);
}








@Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {
Picasso.with(context).load(android_versions.get(i).getAndroid_image_url()).resize(61, 65).into
(viewHolder.img_android);

// added from MySQL-Retrofit NOT Picasso
viewHolder.job_desc.setText(android.get(i).getDesc());
// end of added
viewHolder.img_android.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View view) {
// Intent intent = new Intent(DataAdapter.this, GetDetails.class);
Intent intent = new Intent (context, SecondActivity.class);
// in.putExtra("USE_A_CONSTANT_HERE_AS_KEY", ?);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
}




@Override
public int getItemCount() {
return android_versions.size();
}




public class ViewHolder extends RecyclerView.ViewHolder{
private TextView job_desc;
ImageView img_android;
public ViewHolder(View view) {
super(view);
img_android = (ImageView)view.findViewById(R.id.img_android);
job_desc = (TextView)view.findViewById(R.id.job_desc);
}
}
}

Android版本.java

package davidfoale.info.relativelayout_json;

public class AndroidVersion {

private String android_image_url;

public String getAndroid_image_url() {
return android_image_url;
}
public void setAndroid_image_url(String android_image_url) {
this.android_image_url = android_image_url;
}

// added in to get desc data
private String job_desc;

public String getDesc() {
return job_desc;
}
// end
}

最佳答案

您的 DataAdapter 类没有像 new DataAdapter(data); 这样的构造函数来创建对象,因此在 onResponse() 方法中您需要从 adapter = new DataAdapter(data); 更改为 adapter = new DataAdapter(getApplicationContext(), data); 或者在您的 中提供一个构造函数DataAdapter 类。

关于java - Logcat错误DataAdapter类中的构造函数数据适配器无法应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40793274/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com