Backendless: I can not access retrieved data

落花浮王杯 提交于 2019-12-04 22:03:15

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

(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.

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