2016-11-24 45 views
0

logcat的:在我的DataAdapterlogcat的錯誤構造的DataAdapter類DataAdapter的canot被appied

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

的ArrayList不能在Android的版本適用於我的數組列表

我相信這是一個關於上下文,但我不清楚哪一行需要修改。

MainActivity.java單詞'data'引發錯誤。

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

AndroidVersion.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 
} 

回答

1

DataAdapter類沒有構造函數創建一個類似new DataAdapter(data);的對象,所以裏面onResponse()方法,你需要改變從adapter = new DataAdapter(data);adapter = new DataAdapter(getApplicationContext(), data);,或者在您的DataAdapter課程中提供構造函數。