将Parse数据加载到片段列表视图中

时间:2015-10-31 15:22:15

标签: android android-fragments parse-platform android-listfragment

我正在使用android studio构建一个应用程序,它是一个带有片段的导航抽屉应用程序。我成功地将我的应用程序连接到Parse.com,它收到奇妙的通知。但是,我无法成功实现以下功能:

我有一个包含“名称”列的Parse.com对象。我能够从Parse.com加载该列中的数据但是我无法将该数据放入列表视图中。

这是我的Alerts.java页面,它从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);
}

}

这是我的应用程序类,它注册了我的Alerts.java类(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();
}
}

这是我希望用我的Parse.com数据Parse.java填充的片段(我已经注释掉了导致错误的行):

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;

}

}

最后这是我的布局文件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>

我已经尝试了Parse的MealSpotting教程,什么都没有,但似乎什么都没有为我工作,我迫切需要一个解决方案,真的如果有人确实有答案,可能会显示一些代码或至少指向一些教程可以救命。提前谢谢..

干杯。

1 个答案:

答案 0 :(得分:1)

您无法在Fragment类中将上下文作为this传递。您应该通过getActivity()代替this

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

编辑: 您的ParseObject子类名称和@ParseClassName注释名称不匹配。

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

另外,如果使用解析子类对象,则在done()回调的findInBackground()方法中检索数据,然后通过解析子类方法名称检索记录。试试这样:

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