Rayyan
Rayyan

Reputation: 220

Load Parse data into a fragment list view

I am building an app using android studio and it is a navigation drawer application with fragments. I was successfully able to connect my app to Parse.com and it receives notifications wonderfully. However, I am not able to successfully implement the following feature :

I have a Parse.com object which contains a "name" column. I am able to load the data within that column from Parse.com BUT I can't place that data into a list view.

Here is my Alerts.java page that fetches that data from Parse.com:

package com.rayyan_nasr.apple.besgb.activity;

import com.parse.ParseClassName;
import com.parse.ParseObject;
import com.parse.ParseQuery;

/**
 * Created by Apple on 10/25/15.
*/
@ParseClassName("Recipe")
public class Alerts extends ParseObject {

public Alerts(){

}

public String getTitle() {
    return getString("name");
}

public void setTitle(String title) {
    put("name", title);
}

}

Here is my application class which registers my Alerts.java class (MyApplication.java) :

package com.rayyan_nasr.apple.besgb.activity;

import android.app.Application;
import android.content.res.Configuration;

import com.parse.Parse;
import com.parse.ParseClassName;
import com.parse.ParseInstallation;
import com.parse.ParseObject;

/**
* Created by Apple on 10/13/15.
*/
public class MyApplication extends Application {

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}
@Override
public void onCreate() {
    super.onCreate();
    ParseObject.registerSubclass(Alerts.class);
    Parse.initialize(this, "xxxxxx", "ccccc");
    ParseInstallation.getCurrentInstallation().saveInBackground();

}
@Override
public void onLowMemory() {
    super.onLowMemory();
}
@Override
public void onTerminate() {
    super.onTerminate();
}
}

Here is the fragment I wish to populate with my Parse.com data, Parse.java (and I have commented out the line that is giving me an error) :

package com.rayyan_nasr.apple.besgb.activity;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.rayyan_nasr.apple.besgb.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Apple on 10/27/15.
 */
public class Parse extends Fragment {

public ArrayList<String> foos = new ArrayList<String>();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view =  inflater.inflate(R.layout.fragment_alerts, container, false);



    Toast.makeText(getActivity(), "Loading...", Toast.LENGTH_LONG).show();

    ParseQuery<Alerts> query = ParseQuery.getQuery(Alerts.class);
    query.findInBackground(new FindCallback<Alerts>() {
        @Override
        public void done(List<Alerts> results, ParseException e) {
            if (e == null) {
                for (int i = 0; i < results.size(); i++) {
                    String c = results.get(i).getString("name");
                    foos.add(c);
                }
            } else {
                Toast.makeText(getActivity(), "Error",
                        Toast.LENGTH_LONG).show();
            }
        }
    });

    ListView lv = (ListView) view.findViewById(R.id.listView);  //I'm getting an error in the below code from (this.. foos);
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, foos);
    lv.setAdapter(arrayAdapter);

    return view;

}

}

Finally here is my layout file, fragment_alerts.XML :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listView"
    android:layout_gravity="center_horizontal" />
</LinearLayout>

I've tried Parse's MealSpotting tutorial and what not but nothing seems to be working for me and I am desperate for a solution and really If someone does have an answer, maybe show some code or at least point to some tutorial that could help. Thanks in advance..

Cheers.

Upvotes: 0

Views: 1111

Answers (1)

Ved
Ved

Reputation: 1077

You can not pass context as this in Fragment class. You should pass getActivity() in place of this.

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, foos);

EDIT : Your ParseObject subclass name and @ParseClassName annotation name does not match.

@ParseClassName("Recipe")
public class Alerts extends ParseObject { 

Also when retrieving data in done() method of findInBackground() callback if use parse subclass object then retrieve records by parse subclass method name. Try like this :

ParseQuery<Alerts> query = ParseQuery.getQuery(Alerts.class);
query.findInBackground(new FindCallback<Alerts>() {
    @Override
    public void done(List<Alerts> results, ParseException e) {
        if (e == null) {
            for (int i = 0; i < results.size(); i++) {
                String c = results.get(i).getTitle();
                foos.add(c);
            }
        } else {
            Toast.makeText(getActivity(), "Error",
                    Toast.LENGTH_LONG).show();
        }
    }
});

Upvotes: 1

Related Questions