0

使用这里的Android项目,RestaurantListActivity显示了Backendless中RestaurantsRestaurant表中检索到的列表,到目前为止没有问题。

问题: 这两天我一直在尝试在代码中的其他地方检索餐馆列表,或者使用该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 );

非常感谢您的帮助!

4

3 回答 3

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;

}

}

希望这可以帮到你 :)

于 2016-03-16T20:04:26.010 回答
1

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

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

于 2016-03-15T07:43:12.327 回答
0

创建一个可序列化的类

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

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

由于handleResponse在 (2) 中调用 add 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());
于 2016-03-16T11:47:18.260 回答