0

App Daten mit J Sohn vom Server lesen und zeigen, dass Daten in der Kartenansicht und alle Dinge funktionieren gut .aber ich will Bild hinzufügen und Bild vom Server zu lesen in Kartenansicht. Verwenden Sie Glide-Bibliothek für dies tun, aber wenn diese Bibliothek hinzufügen meine App zu zwingen, zu stoppen. hier ist mein Code:meine JSON-Codes funktionieren gut, aber wenn Glide für die Analyse Bilder App Kraft Steile

MainActivity:

public class MainActivity extends AppCompatActivity { 

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

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_page); 
     initViews(); 
    } 
    private void initViews(){ 
     recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view); 
     recyclerView.setHasFixedSize(true); 
     RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext()); 
     recyclerView.setLayoutManager(layoutManager); 
     loadJSON(); 
    } 
    private void loadJSON(){ 
     Retrofit retrofit = new Retrofit.Builder() 
       .baseUrl("http://memaraneha.ir/") 
       .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()); 
      } 
     }); 
    } 
} 

JSONResponse:

public class JSONResponse { 
    private AndroidVersion[] android; 

    public AndroidVersion[] getAndroid() { 
     return android; 
    } 
} 

Dataadapter:

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


    public DataAdapter(ArrayList<AndroidVersion> android) { 
     this.android = android; 
    } 


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

    @Override 
    public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) { 

     viewHolder.tv_name.setText(android.get(i).getName()); 
     viewHolder.tv_version.setText(android.get(i).getVer()); 
     viewHolder.tv_api_level.setText(android.get(i).getApi()); 


     // load image into imageview using glide 
     Glide.with(context).load("http://memaraneha.ir/Erfan/images/"+android.get(i).getPic()) 
       .placeholder(R.drawable.truiton) 
       .error(R.drawable.truiton) 
       .into(viewHolder.tv_image); 
    } 

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

    public class ViewHolder extends RecyclerView.ViewHolder{ 
     private TextView tv_name,tv_version,tv_api_level; 
     public ImageView tv_image; 
     public ViewHolder(View view) { 
      super(view); 

      tv_name = (TextView)view.findViewById(R.id.tv_name); 
      tv_version = (TextView)view.findViewById(R.id.tv_version); 
      tv_api_level = (TextView)view.findViewById(R.id.tv_api_level); 
      tv_image= (ImageView) view.findViewById(R.id.img); 

     } 
    } 

} 

AndroidVersion:

public class AndroidVersion { 
    private String ver; 
    private String name; 
    private String api; 
    private String pic; 

    public String getVer() { 
     return ver; 
    } 

    public String getName() { 
     return name; 
    } 

    public String getApi() { 
     return api; 
    } 
    public String getPic(){ 
     return pic; 
    } 
} 

RequestInterface:

public interface RequestInterface { 

    @GET("Erfan/ret.php") 
    Call<JSONResponse> getJSON(); 
} 

card_row.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.CardView 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     card_view:cardCornerRadius="5dp"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="10dp" 
      android:orientation="vertical"> 

      <ImageView 
       android:id="@+id/img" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 

      <TextView 
       android:id="@+id/tv_name" 
       android:layout_marginTop="10dp" 
       android:layout_marginBottom="10dp" 
       android:textSize="18sp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textStyle="bold" /> 
      <TextView 
       android:id="@+id/tv_version" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 
      <TextView 
       android:id="@+id/tv_api_level" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 

     </LinearLayout> 
    </android.support.v7.widget.CardView> 

</LinearLayout> 

main_page.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/card_recycler_view" 
     android:scrollbars="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

</LinearLayout> 

und das ist mein json:

{ "android": 
[ 
{ "ver": "1.5", "name": "Cupcake", "api": "API level 3","pic":"pic1.jpg" }, 
{ "ver": "1.6", "name": "Donut", "api": "API level 4","pic":"pic2.jpg" }, 
{ "ver": "2.0 - 2.1", "name": "Eclair", "api": "API level 5 - 7","pic":"pic3.jpg" }, 
{ "ver": "2.2", "name": "Froyo", "api": "API level 8","pic":"pic4.jpg" }, 
{ "ver": "2.3", "name": "Gingerbread", "api": "API level 9 - 10","pic":"pic5.jpg" }, 
{ "ver": "3.0 - 3.2", "name": "Honeycomb", "api": "API level 11 - 13","pic":"pic6.jpg" }, 
{ "ver": "4.0", "name": "Ice Cream Sandwich", "api": "API level 14 - 15","pic":"pic7.jpg" }, 
{ "ver": "4.1 - 4.3", "name": "JellyBean", "api": "API level 16 - 18","pic":"pic8.jpg" }, 
{ "ver": "4.4", "name": "KitKat", "api": "API level 19","pic":"pic9.jpg" }, 
{ "ver": "5.0 - 5.1", "name": "Lollipop", "api": "API level 21 - 22","pic":"pic10.jpg" }, 
{ "ver": "6.0", "name": "Marshmallow", "api": "API level 23","pic":"pic11.jpg" } 
] 
} 

ich denke, Mein Problem ist in Data Adapter, wenn Glide hinzufügen ... wenn jemand mir bitte helfen kann

+0

Fügen Sie Ihr Protokoll oder Ausnahme – Amir

+0

FATAL AUSNAHME: Haupt java.lang.IllegalArgumentException: Kann keine Last auf einem Null-Kontext com.bumptech.glide starten. manager.RequestManagerRetriever.get (RequestManagerRetriever.java:83) com.bumptech.glide.Glide.with (Glide.java:609) com.beispiel.DataAdapter.onBindViewHolder (DataAdapter.java:44) com.example.ret .DataAdapter.onBindViewHolder (DataAdapt er.java:19) – erfan

Antwort

1

Ändern Sie den Adapter Constructor und übergeben Sie Context zu ihm. Wie Ihr Log zeigt, Glide möchten auf Context zugreifen, aber es ist nicht initialisiert.

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

initialisieren Ihren Adapter mit:

adapter = new DataAdapter(MainActivity.this,data); 
+0

danke für die hilfe :) – erfan

+0

@erfan ihre willcome;) – Amir

Verwandte Themen