我的 recyclerview 自定义适配器无法正常工作
My customAdapter for recycler view is not working
recycler 视图不显示是什么问题?
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
private ProgressDialog dialog;
List<ModelRecycler> newslist;
RequestQueue requestQueue;
private static final String API_URL = "https://newsapi.in/newsapi/news.php?key=qEhn6RWpLRebtTmkOpQcr7CxDKRGHi&category=tamil_state";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=findViewById(R.id.recyclerView);
newslist = new ArrayList<>();
requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, API_URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try{
JSONArray array = response.getJSONArray("News");
for(int i=0;i<array.length();i++){
JSONObject c = array.getJSONObject(i);
String image = c.getString("image");
Log.d("App", "Log : " + c.getString("image"));
String title = c.getString("title");
Log.d("App", "Log : " + c.getString("title"));
String description = c.getString("description");
Log.d("App", "Log : " + c.getString("description"));
String published_date = c.getString("published_date");
Log.d("App", "Log : " + c.getString("published_date"));
String url = c.getString("url");
Log.d("App", "Log : " + c.getString("url"));
// Toast.makeText(MainActivity.this, "Log : " + c.getString("published_date"), Toast.LENGTH_SHORT).show();
ModelRecycler modelRecycler = new ModelRecycler(image,title,description,published_date,url);
newslist.add(modelRecycler);
}
}catch (JSONException e){
Log.d("Error", "Json parsing error: " + e.getMessage());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Error Occured: " + error, Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(jsonObjectRequest);
LinearLayoutManager l1 = new LinearLayoutManager(getApplicationContext());
l1.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(l1);
MyAdapter myAdapter = new MyAdapter(this, newslist);
recyclerView.setAdapter(myAdapter);
}
}
MyAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context mContext;
private List<ModelRecycler> mData;
public MyAdapter (Context mContext, List<ModelRecycler> mData) {
this.mContext = mContext;
this.mData = mData;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v;
LayoutInflater inflater = LayoutInflater.from(mContext);
v = inflater.inflate(R.layout.list_item, parent, false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.news_title.setText(mData.get(position).getTitle());
holder.news_description.setText(mData.get(position).getDescription());
holder.news_time.setText(mData.get(position).getTime());
Glide.with(mContext)
.load(mData.get(position).getImage())
.into(holder.news_image);
}
@Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView news_title, news_description, news_time;
ImageView news_image;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
news_title = itemView.findViewById(R.id.news_title);
news_description = itemView.findViewById(R.id.news_description);
news_time = itemView.findViewById(R.id.news_time);
news_image = itemView.findViewById(R.id.news_image);
}
}
}
ModelRecycler.java
public class ModelRecycler {
String image, title, description, time, url;
public ModelRecycler(String image, String title, String description, String time, String url) {
this.image = image;
this.title = title;
this.description = description;
this.time = time;
this.url = url;
}
public ModelRecycler() {
}
public String getImage() {
return image;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public String getTime() {
return time;
}
public String getUrl() {
return url;
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.technoapps.tamilnews">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TamilNews">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
list_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#F8FFFFFF">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="190dp"
android:background="#fff"
android:elevation="8dp"
android:padding="10dp">
<TextView
android:id="@+id/news_title"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Lorem Ipsum Dolor Amet, Consectetur Adipiscing Elit Ipsum Dolor Amet"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/news_description"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_below="@+id/news_title"
android:layout_marginTop="10dp"
android:text="There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form..."
android:textSize="14dp" />
<TextView
android:id="@+id/news_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/news_description"
android:layout_marginTop="10dp"
android:text="30 min ago"
android:textSize="12dp"
android:textStyle="bold" />
<ImageView
android:id="@+id/news_image"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/news_title"
android:scaleType="centerCrop"
android:src="@drawable/search" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
我正在从 json 获取数据并尝试在回收站视图中显示。日志显示我正在获取数据,但没有显示回收器视图。
我该如何解决这个问题?
请使用下面的代码,
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
private ProgressDialog dialog;
List<ModelRecycler> newslist;
RequestQueue requestQueue;
private static final String API_URL = "https://newsapi.in/newsapi/news.php?key=qEhn6RWpLRebtTmkOpQcr7CxDKRGHi&category=tamil_state";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=findViewById(R.id.recyclerView);
newslist = new ArrayList<>();
requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, API_URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try{
JSONArray array = response.getJSONArray("News");
for(int i=0;i<array.length();i++){
JSONObject c = array.getJSONObject(i);
String image = c.getString("image");
Log.d("App", "Log : " + c.getString("image"));
String title = c.getString("title");
Log.d("App", "Log : " + c.getString("title"));
String description = c.getString("description");
Log.d("App", "Log : " + c.getString("description"));
String published_date = c.getString("published_date");
Log.d("App", "Log : " + c.getString("published_date"));
String url = c.getString("url");
Log.d("App", "Log : " + c.getString("url"));
// Toast.makeText(MainActivity.this, "Log : " + c.getString("published_date"), Toast.LENGTH_SHORT).show();
ModelRecycler modelRecycler = new ModelRecycler(image,title,description,published_date,url);
newslist.add(modelRecycler);
LinearLayoutManager l1 = new LinearLayoutManager(getApplicationContext());
l1.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(l1);
MyAdapter myAdapter = new MyAdapter(this, newslist);
recyclerView.setAdapter(myAdapter);
}
}catch (JSONException e){
Log.d("Error", "Json parsing error: " + e.getMessage());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Error Occured: " + error, Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(jsonObjectRequest);
}
}
recycler 视图不显示是什么问题?
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
private ProgressDialog dialog;
List<ModelRecycler> newslist;
RequestQueue requestQueue;
private static final String API_URL = "https://newsapi.in/newsapi/news.php?key=qEhn6RWpLRebtTmkOpQcr7CxDKRGHi&category=tamil_state";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=findViewById(R.id.recyclerView);
newslist = new ArrayList<>();
requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, API_URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try{
JSONArray array = response.getJSONArray("News");
for(int i=0;i<array.length();i++){
JSONObject c = array.getJSONObject(i);
String image = c.getString("image");
Log.d("App", "Log : " + c.getString("image"));
String title = c.getString("title");
Log.d("App", "Log : " + c.getString("title"));
String description = c.getString("description");
Log.d("App", "Log : " + c.getString("description"));
String published_date = c.getString("published_date");
Log.d("App", "Log : " + c.getString("published_date"));
String url = c.getString("url");
Log.d("App", "Log : " + c.getString("url"));
// Toast.makeText(MainActivity.this, "Log : " + c.getString("published_date"), Toast.LENGTH_SHORT).show();
ModelRecycler modelRecycler = new ModelRecycler(image,title,description,published_date,url);
newslist.add(modelRecycler);
}
}catch (JSONException e){
Log.d("Error", "Json parsing error: " + e.getMessage());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Error Occured: " + error, Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(jsonObjectRequest);
LinearLayoutManager l1 = new LinearLayoutManager(getApplicationContext());
l1.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(l1);
MyAdapter myAdapter = new MyAdapter(this, newslist);
recyclerView.setAdapter(myAdapter);
}
}
MyAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context mContext;
private List<ModelRecycler> mData;
public MyAdapter (Context mContext, List<ModelRecycler> mData) {
this.mContext = mContext;
this.mData = mData;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v;
LayoutInflater inflater = LayoutInflater.from(mContext);
v = inflater.inflate(R.layout.list_item, parent, false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.news_title.setText(mData.get(position).getTitle());
holder.news_description.setText(mData.get(position).getDescription());
holder.news_time.setText(mData.get(position).getTime());
Glide.with(mContext)
.load(mData.get(position).getImage())
.into(holder.news_image);
}
@Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView news_title, news_description, news_time;
ImageView news_image;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
news_title = itemView.findViewById(R.id.news_title);
news_description = itemView.findViewById(R.id.news_description);
news_time = itemView.findViewById(R.id.news_time);
news_image = itemView.findViewById(R.id.news_image);
}
}
}
ModelRecycler.java
public class ModelRecycler {
String image, title, description, time, url;
public ModelRecycler(String image, String title, String description, String time, String url) {
this.image = image;
this.title = title;
this.description = description;
this.time = time;
this.url = url;
}
public ModelRecycler() {
}
public String getImage() {
return image;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public String getTime() {
return time;
}
public String getUrl() {
return url;
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.technoapps.tamilnews">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TamilNews">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
list_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#F8FFFFFF">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="190dp"
android:background="#fff"
android:elevation="8dp"
android:padding="10dp">
<TextView
android:id="@+id/news_title"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Lorem Ipsum Dolor Amet, Consectetur Adipiscing Elit Ipsum Dolor Amet"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/news_description"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_below="@+id/news_title"
android:layout_marginTop="10dp"
android:text="There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form..."
android:textSize="14dp" />
<TextView
android:id="@+id/news_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/news_description"
android:layout_marginTop="10dp"
android:text="30 min ago"
android:textSize="12dp"
android:textStyle="bold" />
<ImageView
android:id="@+id/news_image"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/news_title"
android:scaleType="centerCrop"
android:src="@drawable/search" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
我正在从 json 获取数据并尝试在回收站视图中显示。日志显示我正在获取数据,但没有显示回收器视图。
我该如何解决这个问题?
请使用下面的代码,
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
private ProgressDialog dialog;
List<ModelRecycler> newslist;
RequestQueue requestQueue;
private static final String API_URL = "https://newsapi.in/newsapi/news.php?key=qEhn6RWpLRebtTmkOpQcr7CxDKRGHi&category=tamil_state";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=findViewById(R.id.recyclerView);
newslist = new ArrayList<>();
requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, API_URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try{
JSONArray array = response.getJSONArray("News");
for(int i=0;i<array.length();i++){
JSONObject c = array.getJSONObject(i);
String image = c.getString("image");
Log.d("App", "Log : " + c.getString("image"));
String title = c.getString("title");
Log.d("App", "Log : " + c.getString("title"));
String description = c.getString("description");
Log.d("App", "Log : " + c.getString("description"));
String published_date = c.getString("published_date");
Log.d("App", "Log : " + c.getString("published_date"));
String url = c.getString("url");
Log.d("App", "Log : " + c.getString("url"));
// Toast.makeText(MainActivity.this, "Log : " + c.getString("published_date"), Toast.LENGTH_SHORT).show();
ModelRecycler modelRecycler = new ModelRecycler(image,title,description,published_date,url);
newslist.add(modelRecycler);
LinearLayoutManager l1 = new LinearLayoutManager(getApplicationContext());
l1.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(l1);
MyAdapter myAdapter = new MyAdapter(this, newslist);
recyclerView.setAdapter(myAdapter);
}
}catch (JSONException e){
Log.d("Error", "Json parsing error: " + e.getMessage());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Error Occured: " + error, Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(jsonObjectRequest);
}
}