2016-11-24 12 views
0

Logcat appiedことcanotどのラインを修正する必要があるのか​​不明です。LogcatエラーコンストラクタDataAdapterで

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

onResponse()メソッド内で変更する必要があるので、あなたのDataAdapterクラスは、new DataAdapter(data);のようなオブジェクトを作成するコンストラクタを持っていませんadapter = new DataAdapter(data);からadapter = new DataAdapter(getApplicationContext(), data);まで、またはDataAdapterクラスにコンストラクタを指定してください。