简体   繁体   English

我的回收站视图的 customAdapter 不工作

[英]My customAdapter for recycler view is not working

What is the problem causing the recycler view not to show?导致回收站视图不显示的问题是什么?

MainActivity.java 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我的适配器.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 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;
    }

}

Manifest显现

<?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>

I am getting the data from json and trying to show in a recycler view.我正在从 json 获取数据并尝试在回收站视图中显示。 Log shows that I am getting the data but the recycler View is not showing.日志显示我正在获取数据,但没有显示回收器视图。

How can I solve this issue?我该如何解决这个问题?

Please use below code,请使用下面的代码,

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);

       
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM