Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know there might be some similar questions to this but this is totally different from them. I have an android studio project in which I have a RecyclerView with CardView. When I define the adapter in the MainActivity.java, it gives me the error:
Adapter cannot be applied to (package_name, java.utill.List<package_name>)

Here the main activity code:
Java
package com.example.app;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.recyclerview.widget.LinearLayoutManager;
    import androidx.recyclerview.widget.RecyclerView;
    
    import android.os.Bundle;
    
    import org.json.JSONArray;
    import org.json.JSONObject;
    
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.List;
    
    public class className extends AppCompatActivity {
    
        RecyclerView viewer;
        package_adapter adapter;
        List<item> itemList = new ArrayList <> ();
        @Override
        protected void onCreate( Bundle savedInstanceState ) {
            super.onCreate (savedInstanceState);
            setContentView (R.layout.layout);
            viewer = findViewById (R.id.viewer);
            viewer.setHasFixedSize (true);
            viewer.setLayoutManager (new LinearLayoutManager(this));
    
            itemLoader ();
        }
        public void itemLoader(){
            String data = "";
            String name = "";
            String creator = "";
            int likes;
            try {
    
                URL url = new URL("https://www.unicas-official.com/questions.json");
                HttpURLConnection htc = (HttpURLConnection) url.openConnection();
                InputStream inputStream = htc.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader (inputStream));
                String raw = "";
                while (raw != null){
                    raw = br.readLine();
                    data = data+raw;
                }
                JSONArray jsonArray = new JSONArray (data);
    
                JSONObject jsonObject=jsonArray.getJSONObject (0);
                name=name + jsonObject.get ("name");
                creator=creator + jsonObject.get ("creator");
                likes= Integer.parseInt (jsonObject.get ("likes").toString ());
                itemList.add (
                        new item (
                                "https://notin",
                                name,
                                creator,
                                likes
                        )
                );
                adapter = new package_adapter (className.this, itemList);
                viewer.setAdapter (adapter);
    
            }catch (Exception e){
    
            }
        }
    }

The adapter class code:

Java
package com.example.hometutor;
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import org.w3c.dom.Text;
    
    import java.util.List;
    
    import androidx.annotation.NonNull;
    import androidx.recyclerview.widget.RecyclerView;
    
    public class package_adapter extends RecyclerView.Adapter<package_adapter.packageholder> {
        private Context context;
        private List <item> itemList;
        @NonNull
        @Override
        public packageHolder onCreateViewHolder( @NonNull ViewGroup parent , int viewType ) {
            LayoutInflater inflater = LayoutInflater.from (context);
            View view = inflater.inflate (R.layout.item, null);
            return new packageHolder (view);
        }
    
        @Override
        public void onBindViewHolder( @NonNull packageHolder holder , int position ) {
            item packs = itemList.get (position);
            holder.package_name.setText (packs.getName());
            holder.package_creator.setText (packs.getCreator_name ());
            holder.package_got_likes_count.setText (String.valueOf (packs.getLikes ()));
        }
    
        @Override
        public int getItemCount() {
            return itemList.size ();
        }
    
        class packageHolder extends RecyclerView.ViewHolder{
            ImageView package_ico;
            TextView package_name, package_creator, package_got_likes_count;
            packageHolder( @NonNull View itemView ) {
                super (itemView);
    
                package_ico = itemView.findViewById (R.id.package_imgage);
                package_name = itemView.findViewById (R.id.package_name);
                package_creator = itemView.findViewById (R.id.creator);
                package_name = itemView.findViewById (R.id.likes);
            }
        }
    }



I am so frustrated with this error. Please Help me!! Thank you in advance

What I have tried:

I have everything I could. I tried adding the constructor but it did'nt returned me the result
Posted
Updated 2-Jun-20 4:26am
v3
Comments
Unicas 2-Jun-20 6:58am    
Sorry for the bad formatting because I am new to this site
Richard MacCutchan 2-Jun-20 7:17am    
Please edit your question and add <pre> tags around your code. You can add them easily by using the code button above the edit window.
David Crow 2-Jun-20 10:21am    
"I know there might be some similar questions to this but this is totally different from them."

It can't be both.

"When I define the adapter in the MainActivity.java, it gives me the error:
Adapter cannot be applied to (package_name, java.utill.List<package_name>)"

What line is producing this error? Is it a compile-time error, or an exception being thrown at run-time?

Is it intentional that your JSON file does not contain name, creator, or likes fields?

You might want to consider moving your itemLoader() code to a separate thread to avoid a NetworkOnMainThreadException being thrown.

1 solution

You DO need to add your own constructor. The message is pretty explicit -- you are making an invalid call when you do
adapter = new package_adapter (className.this, itemList);
RecyclerView.Adapter does not have a constructor that takes parameters.

Based on the rest of the class, your constructor should be something like
public package_adapter (Context ctx, List<item> items)
{
itemList = items;
context = ctx;
}

You say you "tried adding the constructor but it didn't return me the result" -- what does that mean? What error or unexpected behavior are you getting? I'm guessing it's probably an error in either your parsing to get the values used to construct an item, or something in the item class itself (no code shown). Use your debugger and see what each item looks like. What you'e shown of the adapter class looks okay.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900