Я работаю с restful API, используя модернизацию, в данных, которые мне нужны, много объектов, поэтому я решил использовать RecyclerView
, потому что он гораздо более мощный и гибкий, но это первый раз, когда я использую настроенный RecyclerView
, и я не знаю, если это правильный способ использовать его в любом случае это то, что я сделал, но приложение не запустится!
Allcountries. java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.widget.TextView;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.security.ProviderInstaller;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import javax.net.ssl.SSLContext;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class Allcountries extends AppCompatActivity {
RecyclerView recyclerView;
TextView textViewResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_allcountries);
try {
ProviderInstaller.installIfNeeded(getApplicationContext());
SSLContext sslContext;
sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(null, null, null);
sslContext.createSSLEngine();
} catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException
| NoSuchAlgorithmException | KeyManagementException e) {
e.printStackTrace();
}
textViewResult = findViewById(R.id.countryname);
recyclerView = findViewById(R.id.rv);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://siteweb/")
.addConverterFactory(GsonConverterFactory.create())
.build();
CoronaLmaoNinjaCountries coronaLmaoNinjaCountries = retrofit.create(CoronaLmaoNinjaCountries.class);
Call<List<Detail>> call = coronaLmaoNinjaCountries.getPosts();
call.enqueue(new Callback<List<Detail>>() {
@Override
public void onResponse(Call<List<Detail>> call, Response<List<Detail>> response) {
if (!response.isSuccessful()) {
textViewResult.setText("Code: " + response.code());
return;
}
List<Detail> details = response.body();
DetailAdapter detailAdapter = new DetailAdapter(Allcountries.this,details);
recyclerView.setAdapter(detailAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(Allcountries.this));
}
@Override
public void onFailure(Call<List<Detail>> call, Throwable t) {
textViewResult.setText(t.getMessage());
}
});
}}
DetailAdapter. java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class DetailAdapter extends RecyclerView.Adapter<DetailAdapter.MyViewHolder>{
List<Detail> countries;
Context ct;
public DetailAdapter(Context ct,List<Detail> countries) {
this.countries = countries;
this.ct=ct;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(ct);
View view = inflater.inflate(R.layout.row, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.country.setText(countries.get(position).getCountry());
holder.cases.setText(countries.get(position).getCases());
holder.recovered.setText(countries.get(position).getRecovered());
holder.deaths.setText(countries.get(position).getDeaths());
}
@Override
public int getItemCount() {
return countries.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView country,cases,recovered,deaths;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
country = itemView.findViewById(R.id.countryname);
cases = itemView.findViewById(R.id.cases);
recovered = itemView.findViewById(R.id.recovered);
deaths = itemView.findViewById(R.id.deathsr);
}
}
}
Detail. java
public class Detail {
private String country;
private int cases;
private int deaths;
private int recovered;
public String getCountry() {
return country;
}
public int getCases() {
return cases;
}
public int getDeaths() {
return deaths;
}
public int getRecovered() {
return recovered;
}
}
Activity_Allcountries. xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Allcountries">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
ряд. xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/countryname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="21dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/deathsr"
tools:layout_editor_absoluteX="176dp" />
<TextView
android:id="@+id/cases"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="101dp"
android:layout_marginRight="101dp"
android:text="TextView"
app:layout_constraintEnd_toStartOf="@+id/deathsr"
tools:layout_editor_absoluteY="40dp" />
<TextView
android:id="@+id/recovered"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="101dp"
android:layout_marginLeft="101dp"
android:text="TextView"
app:layout_constraintStart_toEndOf="@+id/deathsr"
tools:layout_editor_absoluteY="40dp" />
<TextView
android:id="@+id/deathsr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
tools:layout_editor_absoluteX="176dp"
tools:layout_editor_absoluteY="40dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>``` ```
CoronaLmaoNinjaCountries. java
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
public interface CoronaLmaoNinjaCountries {
@GET("countries")
Call<List<Detail>> getPosts();
}
ошибка logcat:
example.testcor E/dalvikvm: Could not find class 'androidx.core.view.ViewCompat$2', referenced from method androidx.core.view.ViewCompat.addOnUnhandledKeyEventListener
03-16 12:46:51.237 8193-8193/com.example.testcor E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method androidx.core.view.ViewCompat.dispatchApplyWindowInsets
03-16 12:46:51.267 8193-8193/com.example.testcor E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method androidx.core.view.ViewCompat.onApplyWindowInsets
03-16 12:46:51.267 8193-8193/com.example.testcor E/dalvikvm: Could not find class 'android.view.View$OnUnhandledKeyEventListener', referenced from method androidx.core.view.ViewCompat.removeOnUnhandledKeyEventListener
03-16 12:46:51.277 8193-8193/com.example.testcor E/dalvikvm: Could not find class 'androidx.core.view.ViewCompat$1', referenced from method androidx.core.view.ViewCompat.setOnApplyWindowInsetsListener
03-16 12:46:51.317 8193-8193/com.example.testcor E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method androidx.appcompat.widget.AppCompatImageHelper.hasOverlappingRendering
03-16 12:46:51.417 8193-8193/com.example.testcor E/dalvikvm: Could not find class 'android.view.textclassifier.TextClassificationManager', referenced from method androidx.appcompat.widget.AppCompatTextClassifierHelper.getTextClassifier
03-16 12:46:51.487 8193-8193/com.example.testcor E/dalvikvm: Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.wrappers.PackageManagerWrapper.zzb
03-16 12:46:51.578 8193-8193/com.example.testcor E/dalvikvm: Could not find class 'android.app.AppOpsManager', referenced from method off.a
03-16 12:46:51.588 8193-8193/com.example.testcor E/dalvikvm: Could not find class 'com.google.android.gms.org.conscrypt.TrustManagerImpl', referenced from method com.google.android.gms.org.conscrypt.Conscrypt.isConscrypt
03-16 12:46:51.598 8193-8193/com.example.testcor E/dalvikvm: Could not find class 'com.google.android.gms.org.conscrypt.TrustManagerImpl', referenced from method com.google.android.gms.org.conscrypt.Conscrypt.toConscrypt
03-16 12:46:51.818 8193-8193/com.example.testcor E/dalvikvm: Could not find class 'javax.net.ssl.SNIServerName', referenced from method com.google.android.gms.org.conscrypt.Platform.getSniHostnameFromParams
03-16 12:46:51.818 8193-8193/com.example.testcor E/dalvikvm: Could not find class 'javax.net.ssl.SNIHostName', referenced from method com.google.android.gms.org.conscrypt.Platform.setParametersSniHostname
03-16 12:46:51.818 8193-8193/com.example.testcor E/dalvikvm: Could not find class 'javax.net.ssl.SNIHostName', referenced from method com.google.android.gms.org.conscrypt.Platform.setParametersSniHostname
03-16 12:46:52.318 8193-8193/com.example.testcor E/RecyclerView: No adapter attached; skipping layout
03-16 12:46:52.358 8193-8193/com.example.testcor E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.testcor.Allcountries$1.onFailure(Allcountries.java:77)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$2.run(ExecutorCallAdapterFactory.java:79)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)