0

这是我的联系课程:

联系类:
包com.gwt.sample.gwtdemoproject.shared;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;




public class Contact implements Serializable {

    private List<Contact> contactlist = new ArrayList<>();

    private static final long serialVersionUID = 1L;
    private String email;
    private String number;
    private String name;
    private String place;
    private Long  id;

    public Contact(String name, String number, String email, String place) {
        this.name = name;
        this.number = number;
        this.email = email;
        this.place = place;
    }

    public String getname() {
        return name;
    };

    public String getnumber() {

        return number;
    }

    public String getemail() {

        return email;
    }

    public String getplace() {

        return place;
    }

    public void setname(String name) {
        this.name = name;

    }

    public void setemail(String state) {
        this.email = state;
    }

    public void setplace(String place) {
        this.place = place;
    }

    public void setnumber(String number) {
        this.number = number;
    }
    public Long getid(){
        return this.id;
    }
    public Contact(){}

    public boolean removedata( Contact contact){
        int i = contactlist.size();
        if(contact==null ? i==0 : contact.equals(i));
        {
            contactlist.remove(contact);
        }
        return true;
        }

    }

服务:

    package com.gwt.sample.gwtdemoproject.client.Service;

import java.util.List;

import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
import com.gwt.sample.gwtdemoproject.shared.Contact;

@RemoteServiceRelativePath("contactdata")

public interface ContactDataService extends RemoteService{

    public  List<Contact> getData();
    public  Contact addData(Contact contact);
    public  Contact removeData(Contact contact);


}

我的服务异步:

    package com.gwt.sample.gwtdemoproject.client.Service;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwt.sample.gwtdemoproject.shared.Contact;

public interface ContactDataServiceAsync {

void getData(AsyncCallback<List<Contact>> callback);
void addData(Contact contact, AsyncCallback<Contact> callback);
void removeData( Contact contact,AsyncCallback<Contact> callback);


}

我的服务实现

    package com.gwt.sample.gwtdemoproject.server;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.gwt.sample.gwtdemoproject.client.Service.ContactDataService;
import com.gwt.sample.gwtdemoproject.shared.Contact;

public class ContactDataServiceImpl extends RemoteServiceServlet implements
        ContactDataService {

    private static final long serialVersionUID = 1L;
    List<Contact> contactlist = new ArrayList<>();

    public ContactDataServiceImpl() {
    }
    @Override
    public List<Contact> getData() {
        return contactlist;
    }
    @Override
    public Contact addData(Contact contact) {
        contactlist.add(contact);
        return contact;
    }
     @Override
     public Contact removeData(Contact contact) {
         contactlist.remove(contact);
        return contact;
     }
     public List<Contact> getlist(){
        return contactlist; 
     }


}

我的 gui/客户

    table = new CellTable<Contact>();

        TextColumn<Contact> nameColumn = new TextColumn<Contact>() {
            @Override
            public String getValue(Contact contact) {
                return contact.getname();
            }
        };

        TextColumn<Contact> emailColumn = new TextColumn<Contact>() {
            @Override
            public String getValue(Contact contact) {
                return contact.getemail();
            }
        };
        //
        TextColumn<Contact> numberColumn = new TextColumn<Contact>() {
            @Override
            public String getValue(Contact contact) {
                return contact.getnumber();
            }
        };
        //
        Column<Contact, String> placeColumn = new Column<Contact, String>(
                new EditTextCell()) {
            @Override
            public String getValue(Contact object) {
                return object.getplace();
            }
        };
        ButtonCell btncell = new ButtonCell();
        //
        Column<Contact, String> deleteColumn = new Column<Contact, String>(
                btncell) {

            @Override
            public String getValue(Contact object) {

                return "X";
            }
        };

        table.addColumn(nameColumn, "Name");
        table.addColumn(numberColumn, "Mobile");
        table.addColumn(emailColumn, "Email");
        table.addColumn(placeColumn, "City");
        table.addColumn(deleteColumn, "");
        // getting list
        Myservice = (ContactDataServiceAsync) GWT
                .create(ContactDataService.class);
        ServiceDefTarget serviceDef = (ServiceDefTarget) Myservice;
        serviceDef.setServiceEntryPoint(GWT.getModuleBaseURL() + "contactdata");

        final ContactDataServiceAsync service = (ContactDataServiceAsync) GWT
                .create(ContactDataService.class);
        final AsyncCallback<List<Contact>> getcallback = new AsyncCallback<List<Contact>>() {
            @Override
            public void onSuccess(List<Contact> result) {
                dataProvider.addDataDisplay(table);
                List<Contact> list = dataProvider.getList();
                for (Contact contact : result) {
                    list.add(contact);
                }
            }

            @Override
            public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());
            }
        };
        service.getData(getcallback);

        // Delete Row
            deleteColumn.setFieldUpdater(new FieldUpdater<Contact, String>() {

            @Override
                public void update(int index, Contact object, String value) {
                cont = object;

                    Myservice.removeData(cont, new AsyncCallback<Contact>() {

                    @Override
                        public void onFailure(Throwable caught) {
                        Window.alert(caught.getMessage());
                    }
                    @Override
                        public void onSuccess(Contact result) {
                        List<Contact> list = dataProvider.getList();
                        list.remove(result);
                        table.redraw();
                        int sizeoflist = list.size();
                        System.out.println(sizeoflist);

                    }
                });

                }
            });
        // add row
        addBtn.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                savedata();
            }
        });

        // root
        vpanel.setWidth("100%");
        vpanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
        hpanel.setWidth("100%");
        table.setWidth("100%");
        hpanel.add(name);
        hpanel.add(number);
        hpanel.add(email);
        hpanel.add(place);
        hpanel.add(addBtn);
        vpanel.add(table);
        vpanel.add(hpanel);

        RootPanel.get().add(vpanel);
    }

    protected void savedata() {

        cont = new Contact(name.getText(), number.getText(), email.getText(),
                place.getText());

        Myservice.addData(cont, new AsyncCallback<Contact>() {

            @Override
            public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());
            }

            @Override
            public void onSuccess(Contact result) {
                List<Contact> list = dataProvider.getList();
                list.add(result);
                table.redraw();
            }
        });
    }

输出:没有错误,但我无法从服务器中删除记录(刷新时它不起作用)

我可以通过以下代码从 celltable 中删除

// Delete Row 
        deleteColumn.setFieldUpdater(new FieldUpdater<Contact, String>() {

            @Override
            public void update(int index, Contact object, String value) {
                dataProvider.getList().remove(object);
                dataProvider.refresh();
                table.redraw();

            }
        });
4

1 回答 1

0

我根据您的代码创建了一个小示例(由于警告/错误问题,我不得不进行更改……),它对我有用。

你可以试试:

@Override
 public void onSuccess(Contact result) {
    List<Contact> list = dataProvider.getList();
    list.remove(cont); //Try with this reference
    table.redraw();
    int sizeoflist = list.size();
    System.out.println(sizeoflist);
}

原因是您从服务器返回一个不同的对象(您可以 @Override 联系类中的hasCodeequals方法,否则尽管内容相同,但对象是不同的。请记住,客户端,服务器(两个列表都是不同的!))。

注意:查看您服务器的代码,因为我不确定它是否在执行您想要的操作。

快乐编码!

于 2013-06-11T12:19:43.593 回答