I'm having the following classes/interfaces:
public class GenericViewModel<T extends AbstractDatabaseObject> {
private Class<?> type;
#SuppressWarnings("unchecked")
public GenericViewModel(Class<?> cl) {
type = cl;
}
}
and a specialization:
public class PersonViewModel extends GenericViewModel<Person> implements IPersonViewModel{
public PersonViewModel() {
super(Person.class);
}
}
Now, my problem is in the presenter:
public class GenericPresenter implements IGenericView.IGenericViewListener {
private GenericViewModel<AbstractDatabaseObject> model;
private IGenericView view;
public GenericPresenter(GenericViewModel<AbstractDatabaseObject> model, IGenericView view) {
this.model = model;
this.view = view;
view.addListener(this);
}
}
To be more precise, I cannot call the constructor of the super class with the given arguments:
public class PersonPresenter extends GenericPresenter {
PersonViewModel model;
IPersonView view;
public PersonPresenter(PersonViewModel model, IPersonView view) {
super(model, view); // Here is the problem. No such constructor in superclass found
// IGenericView i = (IGenericView) view; <-- this seems to work
// GenericViewModel<AbstractDatabaseObject> m = model; <-- this doesn't
}
}
What do I have to change?
Try to change the GenericPresenter class in this way:
private GenericViewModel<? extends AbstractDatabaseObject> model;
private IGenericView view;
public GenericPresenter(GenericViewModel<? extends AbstractDatabaseObject> model,
IGenericView view) {
this.model = model;
this.view = view;
view.addListener(this);
}
Related
I am building a custom component in Java and trying to display that in React Native but the component is not appearing. I've been looking through forums and documentation for solutions but can't find a solution. I am not sure if it's an issue on the native side or the React Native side. Any help or advice on improvement is highly appreciated.
Bottom Sheet View
public class BottomSheetView extends NestedScrollView {
private BottomSheetBehavior bSheetBehavior;
public BottomSheetView(Context context) {
super(context);
init();
}
private void init() {
inflate(getContext(), R.layout.bottom_sheet, this);
CoordinatorLayout coordinaterLayout = findViewById(R.id.coordinate_layout);
View bottomSheet = coordinaterLayout.findViewById(R.id.bottom_sheet_component);
bSheetBehavior = BottomSheetBehavior.from(bottomSheet);
bSheetBehavior.setHideable(false);
bSheetBehavior.setPeekHeight((int) PixelUtil.toPixelFromDIP(200));
}
#Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
}
}
Bottom Sheet Manager
public class BottomSheetManager extends ViewGroupManager<BottomSheetView> {
public static final String REACT_CLASS = "BottomSheet";
private BottomSheetView bottomSheet;
#Override
public String getName() {
return REACT_CLASS;
}
#Override
protected BottomSheetView createViewInstance(ThemedReactContext reactContext) {
return new BottomSheetView(reactContext);
}
}
Bottom Sheet Package
public class BottomSheetPackage implements ReactPackage {
private Activity mActivity = null;
BottomSheetPackage(Activity activity) {
mActivity = activity;
}
public BottomSheetPackage() {
}
#Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = Collections.emptyList();
return modules;
}
#Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Arrays.<ViewManager>asList(new BottomSheetManager());
}
}
React Native
class BottomSheet extends Component {
render() {
return (<BottomSheet style={{height: 200}}/>);
}
}
I have created a custom UI component TextInputPro that renders an EditText. I also have a native module that I can send events like focus() to.
My files are
|-- TextInputProPackage.java <--- packages all together
|-- TextInputProManager.java <--- creates a view (EditText)
|-- TextInputProModule.java <--- gets events from JS
How can TextInputProModule.java communicate with the View created by TextInputProManager.java?
TextInputProPackage.java:
public class TextInputProPackage implements ReactPackage {
#Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
// MODULE CREATED HERE
return Arrays.<NativeModule>asList(
new TextInputProModule(reactContext)
);
}
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
//VIEW CREATED HERE
return Arrays.<ViewManager>asList(
new TextInputProManager()
);
}
}
TextInputProManager.java:
public class TextInputProManager extends SimpleViewManager<EditText> {
public static final String REACT_CLASS = "TextInputPro";
public EditText editText;
#Override
public String getName() {
return REACT_CLASS;
}
#Override
public EditText createViewInstance(ThemedReactContext context){
editText = new EditText(context);
editText.setText("hello world");
return editText;
}
}
TextInputProModule.java:
public class TextInputProModule extends ReactContextBaseJavaModule {
public static final String REACT_CLASS = "TextInputPro";
private static ReactApplicationContext reactContext = null;
public TextInputProModule(ReactApplicationContext context) {
super(context);
reactContext = context;
}
#Override
public String getName() {
return REACT_CLASS;
}
#ReactMethod
public void focus () {
// WHERE ARE YOU EDITTEXT?
}
}
In my head, I have to make a global variable in Package.java and pass it down to both constructors, but I couldn't make that work
Let's say I have the following interfaces:
public interface MvpView { }
public interface MvpPresenter<V extends MvpView> {
void attach(V view);
}
Then a base class that implements MvpPresenter
public class BasePresenter<V extends MvpView> implements MvpPresenter<V> {
private V view;
#Override
public void attach(V view) {
this.view = view;
}
}
And finally the following class that extends BasePresenter
public abstract class BaseFragment<P extends MvpPresenter> implements MvpView {
P presenter;
public void someMethod() {
presenter.attach(this);
}
}
While this compiles it's not safe because there is no guarantee that implementations of the class will implement the correct MvpView (the one that P presenter uses). In order to be safe I've left out the code of someMethod and every implementation must fill it with the exact same code presenter.attach(this)
What I'd like to do but I don't know how is something like the following:
public abstract class BaseFragment<V extends MvpView, P extends MvpPresenter<V>> implements V {
P presenter;
public void someMethod() {
presenter.attach(this);
}
}
Is there any way to achieve this?
Declare BaseFragment like this.
abstract class BaseFragment<P extends MvpPresenter<R>, R extends BaseFragment<P,R>> implements MvpView {
P presenter;
public void someMethod() {
presenter.attach(getThis());
}
abstract R getThis();
}
}
and use the extensions like this,
class MyFragment extends BaseFragment<MvpPresenter<MyFragment>, MyFragment> {
#Override
MyFragment getThis() {
return this;
}
}
This compiles:
public abstract class BaseFragment<P extends MvpPresenter<BaseFragment>> implements MvpView {
P presenter;
public void someMethod() {
presenter.attach(this);
}
}
But I haven't found a way to actually subclass it. I am not sure if MvpView is actually required for anything. Maybe something like this will be useful:
public interface MvpPresenter<V> {
void attach(V view);
}
public class BasePresenter<V> implements MvpPresenter<V> {
private V view;
#Override
public void attach(V view) {
this.view = view;
}
}
public abstract class BaseFragment<R extends BaseFragment> {
MvpPresenter<R> presenter;
public void someMethod() {
presenter.attach(getThis());
}
abstract R getThis();
}
public class MyBaseFragment extends BaseFragment<MyBaseFragment> {
#Override
MyBaseFragment getThis() {
return this;
}
}
Based on boobalan gnanasekaran and user158037 answers, I did it like this:
public interface MvpView { }
public interface MvpPresenter<V extends MvpView> {
void attach(V view);
}
public class BasePresenter<V extends MvpView> implements MvpPresenter<V> {
private V view;
#Override
public void attach(V view) {
this.view = view;
}
public V getView() {
return view;
}
}
public abstract class BaseFragment<V extends MvpView, P extends MvpPresenter<V>> {
P presenter;
protected abstract V getThis();
public void someMethod() {
presenter.attach(getThis());
}
public P getPresenter() { return presenter; }
}
And an example implementation
public interface MoviesContract {
interface Presenter extends MvpPresenter<MoviesContract.View> {
void loadMovies();
}
interface View extends MvpView {
void onMoviesLoaded();
}
}
public class MoviesPresenter extends BasePresenter<MoviesContract.View>
implements MoviesContract.Presenter {
#Override
public void loadMovies() {
getView.onMoviesLoaded();
}
}
public class MoviesFragment extends BaseFragment<MoviesContract.View, MoviesContract.Presenter>
implements MoviesContract.View {
#Override
public MoviesContract.View getThis() {
return this;
}
public loadMovies() {
getPresenter().loadMovies();
}
#Override
public void onMoviesLoaded() {
//
}
}
I am trying to parse the following json and display it in ListView using BaseAdapter, Android. Here the objects 4 and 1 are dynamic.I know how to create a model class if those values are not dynamic. I tried to get the json using following way and it doesn't return any value. Is there anything wrong in Model.java? or Should I change format of json?
{
"effect_list":[
{
"4":[
{
"effects_id":"18",
"effects_name":"Band 1"
},
{
"effects_id":"19",
"effects_name":"Band 2"
}
],
"1":[
{
"effects_id":"1",
"effects_name":"Background Blur"
},
{
"effects_id":"4",
"effects_name":"Blemish Removal"
}
]
}
]
}
Model.java
public class Model{
#SerializedName("effect_list")
#Expose
List<Map<String,List<EffectList>>> effectlist;
public List<Map<String, List<EffectList>>> getEffectlist() {
return effectlist;
}
public void setEffectlist(List<Map<String, List<EffectList>>> effectlist) {
this.effectlist = effectlist;
}
}
EffectList.java
public class EffectList {
#SerializedName("effects_id")
#Expose
private String effectsId;
#SerializedName("effects_name")
#Expose
private String effectsName;
//GETTERS AND SETTERS
}
MyContactAdapter2.java
public class MyContactAdapter2 extends BaseAdapter {
ArrayList<Map<String, List<EffectList>>> contactList;
Context context;
private LayoutInflater mInflater;
public MyContactAdapter2(Context context, ArrayList<Map<String, List<EffectList>>> data) {
this.context = context;
this.mInflater = LayoutInflater.from(context);
contactList = data;
}
#Override
public int getCount() {
return 0;
}
#Override
public List<HashMap<String, List<EffectList>>> getItem(int position) {
return (List<HashMap<String, List<EffectList>>>) contactList.get(position);
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder1 vh1;
if (convertView == null) {
View view = mInflater.inflate(R.layout.get_layout_row_view, parent, false);
vh1 = ViewHolder1.create((RelativeLayout) view);
view.setTag(vh1);
} else {
vh1 = (ViewHolder1) convertView.getTag();
}
EffectList item = (EffectList) getItem(position);
// vh.textViewName.setText(item.getEffectsId());
vh1.textViewName.setText(item.getEffectsName());
vh1.textViewEmail.setText(item.getEffectsId());
// Picasso.with(context).load(item.getProfilePic()).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(vh.imageView);
return vh1.rootView;
}
private static class ViewHolder1 {
public final RelativeLayout rootView;
public final ImageView imageView;
public final TextView textViewName;
public final TextView textViewEmail;
private ViewHolder1(RelativeLayout rootView, ImageView imageView, TextView textViewName, TextView textViewEmail) {
this.rootView = rootView;
this.imageView = imageView;
this.textViewName = textViewName;
this.textViewEmail = textViewEmail;
}
public static MyContactAdapter2.ViewHolder1 create(RelativeLayout rootView) {
ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);
TextView textViewName = (TextView) rootView.findViewById(R.id.textViewName);
TextView textViewEmail = (TextView) rootView.findViewById(R.id.textViewEmail);
return new MyContactAdapter2.ViewHolder1(rootView, imageView, textViewName, textViewEmail);
}
}
}
Is there anything wrong in MyContactAdapter2?
I think you should change this method
#Override
public int getCount() {
return contactList.size();
}
Hope This Help
Have a look at this
I am also having same problem regarding parsing with GSON, but achievd by making StringRequest<> instead of GSONRequest<>
Please check my answer.
You can create POJO Model class from JSON from this website
http://www.jsonschema2pojo.org/
I made this from your json! though your json should be simpler !
-----------------------------------com.example.EffectList.java-----------------------------------
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class EffectList {
#SerializedName("4")
#Expose
private List<com.example._4> _4 = null;
#SerializedName("1")
#Expose
private List<com.example._1> _1 = null;
public List<com.example._4> get4() {
return _4;
}
public void set4(List<com.example._4> _4) {
this._4 = _4;
}
public List<com.example._1> get1() {
return _1;
}
public void set1(List<com.example._1> _1) {
this._1 = _1;
}
}
-----------------------------------com.example.Example.java-----------------------------------
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
#SerializedName("effect_list")
#Expose
private List<EffectList> effectList = null;
public List<EffectList> getEffectList() {
return effectList;
}
public void setEffectList(List<EffectList> effectList) {
this.effectList = effectList;
}
}
-----------------------------------com.example._1.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class _1 {
#SerializedName("effects_id")
#Expose
private String effectsId;
#SerializedName("effects_name")
#Expose
private String effectsName;
public String getEffectsId() {
return effectsId;
}
public void setEffectsId(String effectsId) {
this.effectsId = effectsId;
}
public String getEffectsName() {
return effectsName;
}
public void setEffectsName(String effectsName) {
this.effectsName = effectsName;
}
}
-----------------------------------com.example._4.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class _4 {
#SerializedName("effects_id")
#Expose
private String effectsId;
#SerializedName("effects_name")
#Expose
private String effectsName;
public String getEffectsId() {
return effectsId;
}
public void setEffectsId(String effectsId) {
this.effectsId = effectsId;
}
public String getEffectsName() {
return effectsName;
}
public void setEffectsName(String effectsName) {
this.effectsName = effectsName;
}
}
I'm setting up my first Vaadin application with Vaadin 7.5.6 and the official Vaadin Spring 1.0.0. I want to use the MVP pattern but I'm asking myself how the components work together. Because I'm new to MVP i don't want to use any Addons, so i tried to set it up by myself.
So if I'm right, the LoginViewPresenter will give me the view over presenterInstance.getView(). This is already working fine, but how should i access to the presenter over the view? When i want to do a logic operation for my view i should do it in the presenter class. But how to call a presenter method from a view Buttonclicklistener?
My second question is if I have the UIScope annotation over my presenter class, when does Spring instantiate a new object from this class? I thougt as long as the UI exists. But after generating a random string in the constructor I'm printing out the content of the randomString variable (in the UI.class init() method) but there is always a new value.
Regards
LoginViewPresenter.java
#SpringComponent
#UIScope
public class LoginViewPresenter implements Serializable
{
private static final long serialVersionUID = 6286518141570430211L;
#Autowired
private LoginView view;
public final String randomString;
public LoginViewPresenter()
{
randomString = Utils.generateRandomString(8);
}
#PostConstruct
public void init()
{
}
public LoginView getView()
{
return view;
}
public void setView(LoginView view)
{
this.view = view;
}
}
LoginView.java
#SuppressWarnings("serial")
#UIScope
#SpringView(name = LoginView.NAME)
public class LoginView extends VerticalLayout implements View
{
public static final String NAME = "LoginView";
#PostConstruct
private void init()
{
}
#Override
public void enter(ViewChangeEvent event)
{
}
}
Your view should'nt be aware of the presenter. It should fire events, and your presenter can listen to them.
Here is how I do it:
LoginView.java
#SuppressWarnings("serial")
#UIScope
#SpringView(name = LoginView.NAME)
public class LoginView extends VerticalLayout implements View {
public static final String NAME = "LoginView";
#Autowired
private transient Collection<LoginViewListener> loginViewListeners;
#PostConstruct
private void init() {
...
Button b = new Button("click me");
b.addClickListener(e -> loginViewListeners.forEach(l -> l.eventFired()));
addComponent(b);
...
loginViewListeners.forEach(listener -> listener.viewInitialized(this));
}
#Override
public void enter(ViewChangeEvent event)
{
}
public interface LoginViewListener {
void viewInitialized(LoginView view);
void eventFired();
}
}
LoginViewPresenter.java
#SpringComponent
#UIScope
public class LoginViewPresenter implements LoginViewListener, Serializable {
private static final long serialVersionUID = 6286518141570430211L;
private LoginView view;
public final String randomString;
public LoginViewPresenter() {
randomString = Utils.generateRandomString(8);
}
#PostConstruct
public void init() {
}
public LoginView getView() {
return view;
}
public void setView(LoginView view) {
this.view = view;
}
#Override
public void viewInitialized(LoginView v) {
setView(v);
}
#Override
void eventFired() {
...
}
}
Does your randomString still have always a new value with this design?