英文:
Java Generic from Activity to fragment to adapter
问题
我正在开发 Android 应用程序,已经设计了一个通用适配器,参数化适配器将继承该适配器。
public abstract class CarAdapter<T> extends RecyclerView.Adapter<CarAdapter<T>.BaseCarViewHolder> {
protected DialogMakerCar<T> dialogMaker;
protected Context context;
// ...
}
子类:
public class CarItemAdapter extends CarDialogAdapter<Item> {
public CarItemAdapter(Context context, DialogMakerCar<Item> dialogMaker) {
super(dialogMaker, context);
}
}
我在一个片段中调用了这个适配器,而该片段也需要是通用的,以便我可以从活动中调用此片段和适配器以供自定义对象使用。
// 调用适配器的片段,但是它不允许我使用<T>通用类型
adapter = new CarItemAdapter(getContext(), dialogMaker);
gridRecycler.setHasFixedSize(true);
gridRecycler.setLayoutManager(adapter.getLayoutManager());
如果需要更多细节,请告诉我,谢谢。
英文:
I am working in android app, have designed a generic adapter and parameterized adapter will inherit this.
public abstract class CarAdapter<T> extends RecyclerView.Adapter<CarAdapter<T>.BaseCarViewHolder> {
protected DialogMakerCar<T> dialogMaker;
protected Context context;
......
child class
public class CarItemAdapter extends CarDialogAdapter<Item> {
public CarItemAdapter(Context context, DialogMakerCar<Item> dialogMaker) {
super(dialogMaker, context);
}
I am calling this adapter in a fragment and that fragment is also need to be generic so that i can call this fragment and adapter for custom objects from activity.
//Fragment to call adapter but its not letting me to use <T> generic
adapter = new CarItemAdapter(getContext(),dialogMaker);
gridRecycler.setHasFixedSize(true);
gridRecycler.setLayoutManager(adapter.getLayoutManager());
Please let me know if you need more details, Thanks
答案1
得分: 1
以下是翻译好的内容:
我不是 Android 开发者,所以不能给你一个真实的示例。让我们定义如下接口(我不知道你实际需要的方法,所以只是举了一个简单的例子)
public interface ICarAdapter {
public <T> DialogMakerCar<T> getDialogMaker();
public Context getContext();
}
你可以定义 T 继承自哪个类,以避免在其他类中使用时出现警告。
现在你需要在你定义的类中使用这个接口:
public class CarItemAdapter extends DialogMakerCar<Item> implements ICarAdapter{
private DialogMakerCar<Item> dialogMakerCar;
private Context context;
public CarItemAdapter(Context cntxt, DialogMakerCar<Item> dialogMaker) {
dialogMakerCar = dialogMaker;
context = cntxt;
}
/** 这将会给你一个警告,如果在接口中定义 T 继承了 Item 实现的接口,可以避免这个警告。*/
@Override
public DialogMakerCar<Item> getDialogMaker() {
return dialogMakerCar;
}
@Override
public Context getContext() {
return context;
}
}
在片段类中:
public class Fragment {
public <T> ICarAdapter createAdapter(T item) {
// 你可以根据需要进行管理,使用 switch、定义枚举或传递所需的类。
if (item instanceof Item) {
return new CarItemAdapter(getContext(),dialogMaker);
}
return null;
}
然后当你想要使用它时:
public class Activity {
Fragment fragment;
ICarAdapter adapter;
public Activity() {
fragment = new Fragment();
Item item = new Item();
adapter = fragment.createAdapter(item);
adapter.getContext();
adapter.getDialogMaker();
}
}
希望至少对你有所帮助。
其他实现方法:
https://dzone.com/articles/java-generic-factory
https://stackoverflow.com/a/47464801/6708879
https://stackoverflow.com/questions/6093363/generic-factory-with-unknown-implementation-classes
<details>
<summary>英文:</summary>
I'm not an Android dev, so can't really give you a real example.
Lets define the interface as following ( I don't know the real method you need so just made a simple example)
public interface ICarAdapter {
public <T> DialogMakerCar<T> getDialogMaker();
public Context getContext();
}
You can defined what the T extends from to avoid warnings when used in the other classes.
Now you need to use this interface in the classes you defined:
public class CarItemAdapter extends DialogMakerCar<Item> implements ICarAdapter{
private DialogMakerCar<Item> dialogMakerCar;
private Context context;
public CarItemAdapter(Context cntxt, DialogMakerCar<Item> dialogMaker) {
dialogMakerCar = dialogMaker;
context = cntxt;
}
/** This will give you a warning, can be avoided
if you define in the interface that T extendes
an interface that Item implements.*/
@Override
public DialogMakerCar<Item> getDialogMaker() {
return dialogMakerCar;
}
@Override
public Context getContext() {
return context;
}
}
In the fragment class:
public class Fragment {
public <T> ICarAdapter createAdapter(T item) {
//You can manage this as you want,
//with a switch, defining an Enum or passing the Class that you want.
if (item instanceof Item) {
return new CarItemAdapter(getContext(),dialogMaker);
}
return null;
}
An then when you want to use it:
public class Activity {
Fragment fragment;
ICarAdapter adapter;
public Activity() {
fragment = new Fragment();
Item item = new Item();
adapter = fragment.createAdapter(item);
adapter.getContext();
adapter.getDialogMaker();
}
Hope at least it helps you a bit.
Other ways to do this:
https://dzone.com/articles/java-generic-factory
https://stackoverflow.com/a/47464801/6708879
https://stackoverflow.com/questions/6093363/generic-factory-with-unknown-implementation-classes
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论