S
S
syxme2020-08-09 15:12:44
OOP
syxme, 2020-08-09 15:12:44

How to declare a Generic class variable without knowing its type?

There is an abstract class

public abstract class Adapter<T> {
            public abstract T onCreateViewHolder(ViewGroup parent);
            public abstract void onBindViewHolder(T holder, int position);
            public abstract int getItemCount();
}


I use it like this:
public class AdapterAudioList: Adapter<VListView> {

        PlayList list;
        public AdapterAudioList(PlayList list){
            this.list = list;
        }
        public override VListView onCreateViewHolder(ViewGroup parent) {
            return new VListView();
        }

        public override void onBindViewHolder(VListView holder, int position) {
            holder.setLabel(list.getAudio(position).artist);
            holder.setLabel2(list.getAudio(position).title);
        }
        public override int getItemCount() {
            return 500;
        }
    }


initialize
AdapterAudioList listx = new AdapterAudioList(list);
recyclerView1.setAdapter(listx);


private Adapter adapter;
 public void setAdapter(Adapter adb) {
            adapter = adb;
}

Right now everything is not working, I tried to inherit from the empty Adapter class and cast it, but I got null.
The question is how to do it right?
I also tried to do this, but it still didn’t cast, maybe I didn’t cast correctly)
public abstract class BaseElement :UserControl {}

public abstract class Adapter<T>:BaseElement {
            public abstract T onCreateViewHolder(ViewGroup parent);
            public abstract void onBindViewHolder(T holder, int position);
            public abstract int getItemCount();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2020-08-09
@yarosroman

You do not declare a universal variable. But you can create a geneticist and work with him through reflection.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question