Backendless: I can not access retrieved data

这一生的挚爱 提交于 2019-12-12 09:27:51

问题


Using the Android project here, RestaurantListActivity shows a list of retrieved Restaurants from Restaurant table in Backendless and there is no problem so far.

The problem: I have been trying these two days to retrieve the list of restaurants somewhere else in the code, or use the totalRestaurants list.

I tried:

1- This code causes NullPointerException:

Backendless.Data.of( Restaurant.class ).findFirst();

2- using the same code in Github: I can access totalRestaurants list inside brackets, but after this block of code it shows me that totalRestaurants list is empty.

Backendless.Data.of( Restaurant.class ).find( query, new LoadingCallback<BackendlessCollection<Restaurant>>( this, getString( R.string.loading_restaurants ), true )
    {
      @Override
      public void handleResponse( BackendlessCollection<Restaurant> restaurantsBackendlessCollection )
      {
        restaurants = restaurantsBackendlessCollection;

        addMoreItems( restaurantsBackendlessCollection );

        super.handleResponse( restaurantsBackendlessCollection );
      }
    } );

3- Using auto load approach in the Backendless site with slight edits: the same thing happens again. Inside the callback I can print the name of Restaurant's object and I add each object to copylist, but at the end of the code copylistis empty!

LoadingCallback<BackendlessCollection<Restaurant>> callback=new LoadingCallback<BackendlessCollection<Restaurant>>(this, getString( R.string.loading_restaurants ), true)
      {
          @Override
          public void handleResponse( BackendlessCollection<Restaurant> restaurants )
          {
              System.out.println( "Loaded " + restaurants.getCurrentPage().size() + " restaurant objects" );
              System.out.println( "Total restaurants in the Backendless storage - " + restaurants.getTotalObjects() );

              Iterator<Restaurant> iterator=restaurants.getCurrentPage().iterator();

              while( iterator.hasNext() )
              {
                  Restaurant restaurant=iterator.next();
                  copylist.add(restaurant);
                  System.out.println( "\nRestaurant name = " + restaurant.getName() );
              }
          }
      };
      Backendless.Data.of( Restaurant.class ).find( callback );

Your help is much appreciated!


回答1:


Ok, here's what I did:

TaxiList class with getters and setters

public class TaxiList {

private String ime; //in database
private String telefon1; //in database
private String telefon2; //in database

public String getIme(String ime) {
    return this.ime;
}

public void setIme(String ime) {
    this.ime = ime;
}

public String getTelefon1(String telefon1) {
    return this.telefon1;
}

public void setTelefon1(String telefon1) {
    this.telefon1 = telefon1;
}

public String getTelefon2(String telefon2) {
    return this.telefon2;
}

public void setTelefon2(String telefon2) {
    this.telefon2 = telefon2;
}

}

Next is adapter

public class TaxiAdapter extends BaseAdapter {

private final LayoutInflater inflater;
private List<TaxiList> taxiLists = null;

public TaxiAdapter(Context context,List<TaxiList> taxiLists) {

    Context mContext = context;
    this.taxiLists = taxiLists;
    inflater = LayoutInflater.from(mContext);

}

public class ViewHolder {

    TextView ime;
    TextView telefon1;
    TextView telefon2;

}

@Override
public int getCount() {
    return taxiLists.size();
}

@Override
public TaxiList getItem(int position) {
    return taxiLists.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

public View getView(final int position, View view, ViewGroup parent) {
    final ViewHolder holder;
    if (view == null) {
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.one_row_taxi, parent, false);

        holder.ime = (TextView) view.findViewById(R.id.textIme);
        holder.telefon1 = (TextView) view.findViewById(R.id.textTelefon1);
        holder.telefon2 = (TextView) view.findViewById(R.id.textTelefon2);


        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }

    holder.ime.setText(taxiLists.get(position).getIme("ime"));
    holder.telefon1.setText(taxiLists.get(position).getTelefon1("telefon1"));
    holder.telefon2.setText(taxiLists.get(position).getTelefon2("telefon2"));


    return view;
}

}

And finally, this is how you retrieve stuff from Backendless (for me, in a fragment)

public class Taxi extends Fragment {

private ListView listview;
private ProgressBar progressBar;
private List<TaxiList> taxiLists = new ArrayList<>();

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_taxi, container, false);

    listview = (ListView) rootView.findViewById(R.id.listview);

    progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);

    progressBar.setVisibility(View.VISIBLE);

    BackendlessDataQuery query = new BackendlessDataQuery();

    QueryOptions queryOptions = new QueryOptions();

    String whereClause = "created";

    queryOptions.addSortByOption(whereClause);

    query.setQueryOptions(queryOptions);

    Backendless.Data.of(TaxiList.class).find(query, new AsyncCallback<BackendlessCollection<TaxiList>>() {

        @Override
        public void handleResponse(BackendlessCollection<TaxiList> taxiListBackendlessCollection) {

            TaxiList taxiList;
            for (TaxiList total : taxiListBackendlessCollection.getCurrentPage()) {

                taxiList = new TaxiList();
                taxiList.setIme(total.getIme("ime"));
                taxiList.setTelefon1(total.getTelefon1("telefon1"));
                taxiList.setTelefon2(total.getTelefon2("telefon2"));

                taxiLists.add(taxiList);

                TaxiAdapter adapter = new TaxiAdapter(getActivity(), taxiLists);

                listview.setAdapter(adapter);

                progressBar.setVisibility(View.GONE);

            }
        }

        @Override
        public void handleFault(BackendlessFault backendlessFault) {
        }
    });

    return rootView;

}

}

Hope this can help you :)




回答2:


(1) Please provide a stack trace, it would be hard to diagnose the problem without any details

(2) and (3) Keep in mind that the execution of the handleResponse method is asynchronous. If you try accessing "restaurants" or "copyList" before the "handleResponse" method is called, the data simply will not be there.




回答3:


Create a serilizable class

public class SharedClass implements Serializable{
    public List<String> copylist = new ArrayList<>();

    void additem(String s){
        copylist.add(s);
    }
}

Since handleResponse in (2) calls add addMoreItems. Edit it to fill the copylist and move to another Activity called NewActivity.

private void addMoreItems( BackendlessCollection<Restaurant> nextPage )
  {
    totalRestaurants.addAll( nextPage.getCurrentPage() );
    Intent restaurantListingIntent = new Intent( this, NewActivity.class );

    SharedClass sharedlist=new SharedClass();
    for(int i=0;i<totalRestaurants.size();i++){
        sharedlist.additem(totalRestaurants.get(i).getName());
        System.out.println("Added "+totalRestaurants.get(i).getName());
    }

    restaurantListingIntent.putExtra("sharedlist", sharedlist);
    startActivity(restaurantListingIntent);
  }

Inside NewActivity I print the copylist size

Intent intent=getIntent();
SharedClass sharedObject= (SharedClass) intent.getSerializableExtra("sharedlist");
System.out.println("copy list size"+ sharedObject.copylist.size());


来源:https://stackoverflow.com/questions/35993055/backendless-i-can-not-access-retrieved-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!