这是实现 RecyclerView 的正确方式吗?

huangapple go评论84阅读模式
英文:

Is this the right way to implement recyclerview?

问题

I am working with restful API using retrofit, the data I want has a lot of objects so I decided to use RecyclerView because it's is much more powerful and flexible but this the first time I use customized RecyclerView and I don't know if this is the right way to use it anyway this what I did but the app wont start !

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>

row.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: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

<details>
<summary>英文:</summary>

I am working with restful API using retrofit, the data I want has a lot of objects so I decided to use `RecyclerView` because it&#39;s is much more powerful and flexible but this the first time I use customized  `RecyclerView` and I don&#39;t know if this is the right way to use it anyway this what I did but the app wont start !

**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(&quot;TLSv1.2&quot;);
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(&quot;https://siteweb/&quot;)
.addConverterFactory(GsonConverterFactory.create())
.build();
CoronaLmaoNinjaCountries coronaLmaoNinjaCountries = retrofit.create(CoronaLmaoNinjaCountries.class);
Call&lt;List&lt;Detail&gt;&gt; call = coronaLmaoNinjaCountries.getPosts();
call.enqueue(new Callback&lt;List&lt;Detail&gt;&gt;() {
@Override
public void onResponse(Call&lt;List&lt;Detail&gt;&gt; call, Response&lt;List&lt;Detail&gt;&gt; response) {
if (!response.isSuccessful()) {
textViewResult.setText(&quot;Code: &quot; + response.code());
return;
}
List&lt;Detail&gt; details = response.body();
DetailAdapter detailAdapter = new DetailAdapter(Allcountries.this,details);
recyclerView.setAdapter(detailAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(Allcountries.this));
}
@Override
public void onFailure(Call&lt;List&lt;Detail&gt;&gt; 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&lt;Detail&gt; countries;
Context ct;
public DetailAdapter(Context ct,List&lt;Detail&gt; 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>

**row.xml**
``` &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:orientation=&quot;vertical&quot; android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;&gt;
&lt;androidx.cardview.widget.CardView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
tools:ignore=&quot;MissingConstraints&quot;&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;&gt;
&lt;TextView
android:id=&quot;@+id/countryname&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginBottom=&quot;21dp&quot;
android:text=&quot;TextView&quot;
app:layout_constraintBottom_toTopOf=&quot;@+id/deathsr&quot;
tools:layout_editor_absoluteX=&quot;176dp&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/cases&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginEnd=&quot;101dp&quot;
android:layout_marginRight=&quot;101dp&quot;
android:text=&quot;TextView&quot;
app:layout_constraintEnd_toStartOf=&quot;@+id/deathsr&quot;
tools:layout_editor_absoluteY=&quot;40dp&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/recovered&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;101dp&quot;
android:layout_marginLeft=&quot;101dp&quot;
android:text=&quot;TextView&quot;
app:layout_constraintStart_toEndOf=&quot;@+id/deathsr&quot;
tools:layout_editor_absoluteY=&quot;40dp&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/deathsr&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;TextView&quot;
tools:layout_editor_absoluteX=&quot;176dp&quot;
tools:layout_editor_absoluteY=&quot;40dp&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
&lt;/androidx.cardview.widget.CardView&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;``` ```

CoronaLmaoNinjaCountries.java

import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
public interface CoronaLmaoNinjaCountries {
@GET(&quot;countries&quot;)
Call&lt;List&lt;Detail&gt;&gt; getPosts();
}

logcat error:

example.testcor E/dalvikvm: Could not find class &#39;androidx.core.view.ViewCompat$2&#39;, 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 &#39;android.view.WindowInsets&#39;, 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 &#39;android.view.WindowInsets&#39;, 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 &#39;android.view.View$OnUnhandledKeyEventListener&#39;, 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 &#39;androidx.core.view.ViewCompat$1&#39;, 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 &#39;android.graphics.drawable.RippleDrawable&#39;, 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 &#39;android.view.textclassifier.TextClassificationManager&#39;, 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 &#39;android.app.AppOpsManager&#39;, 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 &#39;android.app.AppOpsManager&#39;, referenced from method off.a
03-16 12:46:51.588 8193-8193/com.example.testcor E/dalvikvm: Could not find class &#39;com.google.android.gms.org.conscrypt.TrustManagerImpl&#39;, 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 &#39;com.google.android.gms.org.conscrypt.TrustManagerImpl&#39;, 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 &#39;javax.net.ssl.SNIServerName&#39;, 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 &#39;javax.net.ssl.SNIHostName&#39;, 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 &#39;javax.net.ssl.SNIHostName&#39;, 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)

答案1

得分: 1

首先确保您以正确的方式进行了JSON解析,使用@SerializedName("key_name")将键名设置为它,然后确保您为recyclerView进行了正确的初始化。

1- 初始化RecyclerView

RecyclerView recyclerView = findViewById(R.id.rv);

2- 设置LayoutManager

recyclerView.setLayoutManager(new LinearLayoutManager(this)); //(必需)

3- 创建适配器,然后将其传递给RecyclerView

recyclerView.setAdapter(adapter);
英文:

First make sure that you did the JSON parsing in the right way , use @SerializedName("key_name") and set the key name to it , Then make sure that you made the right initialization for the recyclerView

1- init recyclerview

RecyclerView recyclerView = findViewById(R.id.rv);

2- set the LayoutManager

recyclerView.setLayoutManager(new LinearLayoutManager(this)); //(Required)

3- create the adapter the pass it to the recyclerview

recyclerView.setAdapter(adapter);

huangapple
  • 本文由 发表于 2020年3月16日 19:24:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/60705076.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定