Backendless:我无法访问检索到的数据

时间:2016-03-14 16:34:14

标签: android backendless

使用Android项目hereRestaurantListActivity显示Backendless中Restaurants表中检索到的Restaurant列表,目前尚无问题。

问题: 我这两天一直在尝试检索代码中其他地方的餐馆列表,或者使用totalRestaurants列表。

我试过了:

1-此代码导致NullPointerException:

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

2-在Github中使用相同的代码:我可以在括号内访问totalRestaurants列表,但是在这段代码之后,它显示totalRestaurants列表为空。

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-在Backendless网站中使用自动加载方法进行轻微编辑:同样的事情再次发生。在回调中,我可以打印Restaurant对象的名称,然后将每个对象添加到copylist,但在代码copylist的末尾是空的!

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 );

非常感谢您的帮助!

3 个答案:

答案 0 :(得分:2)

好的,这就是我的所作所为:

带有getter和setter的TaxiList类

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;
}

}

接下来是适配器

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;
}

}

最后,这就是你从Backendless(对我来说,片段中)检索内容的方法

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;

}

}

希望这可以帮助你:)

答案 1 :(得分:1)

(1)请提供堆栈跟踪,如果没有任何细节,很难诊断问题

(2)和(3)请记住handleResponse方法的执行是异步的。如果在调用“handleResponse”方法之前尝试访问“restaurants”或“copyList”,则数据就不会存在。

答案 2 :(得分:0)

创建一个可服用的类

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

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

由于(2)中的handleResponse调用添加addMoreItems。编辑它以填充copylist并移至另一个名为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);
  }

内部NewActivity我打印copylist尺寸

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