Populate listview in a fragment

Hi Mark,

Migrating from Parse so I need a little help.
Here’s the deal:

Need to populate listview from 4 columns “Ime” “Datum” “Opis” and fourth is with image. Created adapter and class with getters and setters. What is the code to retrieve data to string(s)?

Pretty new to programming…

Here is adapter:
public class DesavanjaAdapter extends ArrayAdapter<Desavanja> {
private LayoutInflater mInflater;
private int mResource;
public DesavanjaAdapter( Context context, int resource, List<Desavanja> desavanja)
{
super( context, resource, desavanja );
mResource = resource;
mInflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
}
@Override
public View getView( int position, View convertView, ViewGroup parent ) {
View view = convertView == null ? mInflater.inflate( mResource, parent, false ) : convertView;
TextView datum = (TextView) view.findViewById( R.id.textDatum );
TextView ime = (TextView) view.findViewById( R.id.textIme );
TextView opis = (TextView) view.findViewById( R.id.textOpis );
Desavanja item = getItem(position);
datum.setText(item.getDatum());
ime.setText( item.getIme());
opis.setText( item.getOpis() );
return view;
}
}
Here is getters and setters:
public class Desavanja {
private String datum, ime, opis;
public String getDatum() {
return this.datum;
}
public void setDatum() {
this.datum = datum;
}
public String getIme() {
return this.ime;
}
public void setIme(String ime) {
this.ime = ime;
}
public String getOpis() {
return this.opis;
}
public void setOpis(String opis) {
this.opis = opis;
}
}
And here is fragment where I want to put data from database:
public class TabFragmentDesavanja extends Fragment {
private List<Desavanja> svaDesavanja = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_fragment_desavanja, container, false);
ListView listview = (ListView) rootView.findViewById(R.id.list);
BackendlessDataQuery query = new BackendlessDataQuery();
QueryOptions queryOptions = new QueryOptions();
queryOptions.addRelated(“Ime”);
queryOptions.addRelated(“Opis”);
queryOptions.addRelated(“Datum”);
query.setQueryOptions(queryOptions);
//Backendless.Data.of(Desavanja.class).find(query); <-----when this is active, app can’t connect to server
DesavanjaAdapter adapter = new DesavanjaAdapter(getActivity(), R.layout.list_item_desavanja, svaDesavanja);
listview.setAdapter(adapter);
return rootView;
}
}

public void setDatum() {
this.datum = datum;
}
public void setIme(String ime) {
this.ime = ime;
}
public void setOpis(String opis) {
this.opis = opis;
}
This is never used

This is a synchronous API call which is not allowed on the main UI thread:

Backendless.Data.of(Desavanja.class).find(query);

Change it to the async format or create a separate thread where you can invoke sync APIs. The response for the method will contain Desavanja objects.

Regards,
Mark

That was fast!

Ok, I change to this:

Backendless.Data.of( Desavanja.class).find(new AsyncCallback<BackendlessCollection<Desavanja>>() {
@Override
public void handleResponse(BackendlessCollection<Desavanja> desavanjaBackendlessCollection) {

}

@Override
public void handleFault(BackendlessFault fault) {

}
});

and now app works, but listview is not populated

The response is on the handleResponse method. You need to take the objects from the collection you receive and populate the listview there.

Here’s what I did but it’s still blank:

Backendless.Data.of(Desavanja.class).find(new AsyncCallback<BackendlessCollection<Desavanja>>() {
@Override
public void handleResponse(BackendlessCollection<Desavanja> desavanjaList) {

    svaDesavanja = new ArrayList&lt;Desavanja&gt;();
    BackendlessDataQuery query = new BackendlessDataQuery("Desavanja");
    QueryOptions queryOptions = new QueryOptions();
    query.setQueryOptions(queryOptions);
    
    for (Desavanja category : svaDesavanja) {

        Desavanja desavanja = new Desavanja();

        category.setDatum(desavanja.getDatum());
        category.setIme(desavanja.getIme());
        category.setOpis(desavanja.getOpis());

        svaDesavanja.add(desavanja);
    }
    DesavanjaAdapter adapter = new DesavanjaAdapter(getActivity(), svaDesavanja);
    listview.setAdapter(adapter);
}
@Override
public void handleFault(BackendlessFault backendlessFault) {
}

});

Of course it will be empty, you are iterating over an empty collection… You need to use desavanjaList instead. That’s where your data is.

Thanks Mark, got it working!