ParseQueryAdapter没有获取项目

时间:2015-01-03 17:22:54

标签: android parse-platform

我有一个ListView,我正在使用Parse.com获取数据,我正在使用ParseQueryAdapter来泵送ListView。但我没有看到适配器提取的数据。

这是我的代码..

public class MainActivity extends ActionBarActivity {

    private ArrayList<Date> dateHeaders;
    private HashMap<Date,ArrayList<Sessions>> dateContent;

    private ParseQueryAdapter<Sessions> sessionsQueryAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("test", "query");
        Parse.enableLocalDatastore(this);
        Parse.initialize(this, "xxx", "xxx");
        ParseObject.registerSubclass(Sessions.class);

        //sessionHeaderFetch();
        ParseQueryAdapter.QueryFactory<Sessions>  factory = new ParseQueryAdapter.QueryFactory<Sessions>(){
            @Override
            public ParseQuery<Sessions> create() {
                ParseQuery<Sessions> query = Sessions.getQuery();
                query.include("sessionId");
                query.include("Location");
                Log.d("test", "query");
                return query;
            }
        };

        sessionsQueryAdapter = new ParseQueryAdapter<Sessions>(this, factory){
            public View getItemView(Sessions sessions, View view, ViewGroup parent){
                if(view == null){
                    view = View.inflate(getContext(), R.layout.session_list_item, null);
                }
                Log.d("test", "adapter");
                TextView teamName = (TextView) view.findViewById(R.id.teamName);
                TextView location = (TextView) view.findViewById(R.id.location);
                TextView time = (TextView) view.findViewById(R.id.netsTime);
                TextView slots = (TextView) view.findViewById(R.id.slots);
                TextView bookedSlots = (TextView) view.findViewById(R.id.players);
                TextView openSlots = (TextView) view.findViewById(R.id.openSlots);
                teamName.setText(sessions.getSessionId());
                location.setText(sessions.getLocation());
                time.setText((CharSequence) sessions.getNetsDate());
                slots.setText(sessions.getSlots());
                bookedSlots.setText(sessions.getOpenSlots());
                openSlots.setText(sessions.getOpenSlots());
                return view;
            }
        };

        sessionsQueryAdapter.setAutoload(false);
        sessionsQueryAdapter.setPaginationEnabled(false);

        ListView listView = (ListView) findViewById(R.id.expandableListView);
        sessionsQueryAdapter.setTextKey("sessionId");
        listView.setAdapter(sessionsQueryAdapter);

    }
}

以及其他getter和setter我在Parse Model类中也有以下方法。

public static ParseQuery<Sessions> getQuery() {
        Log.d("test", "inside query");
        return ParseQuery.getQuery(Sessions.class);
    }

解析课

@ParseClassName("Sessions")
public class Sessions extends ParseObject {
    public Sessions(){

    }

    public String sessionId;
    public String location;
    public String netNo;
    public String userId;
    public int slots;
    public int openSlots;
    public Date netsDate;

    public String getSessionId() {
        return getString("SessionId");
    }

    public void setSessionId(String sessionId) {
        put("SessionId", sessionId);
    }

    public String getLocation() {
        return getString("Location");
    }

    public void setLocation(String location) {
        put("Location", location);
    }

    public String getNetNo() {
        return getString("NetNo");
    }

    public void setNetNo(String netNo) {
        put("NetNo", netNo);
    }

    public String getUserId() {
        return getString("UserId");
    }

    public void setUserId(String userId) {
        put("UserId", userId);
    }

    public int getSlots() {
        return getInt("Slots");
    }

    public void setSlots(int slots) {
        put("Slots", slots);
    }

    public int getOpenSlots() {
        return getInt("OpenSlots");
    }

    public void setOpenSlots(int openSlots) {
        put("OpenSlots", openSlots);
    }

    public Date getNetsDate() {
        return getDate("NetsDate");
    }

    public void setNetsDate(Date netsDate) {
        put("NetsDate", netsDate);
    }

    public static ParseQuery<Sessions> getQuery() {
        Log.d("test", "inside query");
        return ParseQuery.getQuery(Sessions.class);
    }
}

1 个答案:

答案 0 :(得分:1)

在聊天中与我分享您的代码后,我相信这是最适合您的解决方案: 将您的parseIO更改为:

public class ParseIO {  

public void sessionFetch(Date headers, final ParseIOListener<Sessions> listener){ 
     ParseQuery<Sessions> query = ParseQuery.getQuery(Sessions.class); 
     query.whereEqualTo("NetsDate",headers); 
     final ArrayList<Sessions> sessionsData = new ArrayList<Sessions>(); 
     query.findInBackground(new FindCallback<Sessions>() { 
       @Override 
       public void done(List<Sessions> sessionsObjects, com.parse.ParseException e) { 
            if (e == null) { 
               for (Sessions session : sessionsObjects) { 
                  sessionsData.add(session); 
               } 
               listener.onDataRetrieved(sessionsData); 
            } else { 
                listener.onDataRetrieveFail(e); 
            } 
         } 
     });  
 } 

 public void sessionHeaderFetch(final ParseIOListener<Date> listener){ 

      Log.d("test", "session fetch entry"); 
      ParseQuery<Sessions> query = ParseQuery.getQuery(Sessions.class); 
      final ArrayList<Date> sessionHeaders = new ArrayList<Date>(); 
      query.findInBackground(new FindCallback<Sessions>() { 
           @Override 
           public void done(List<Sessions> sessionsObjects, com.parse.ParseException e) { 
                if (e == null) { 
                    Log.d("test", "Retrieved -- hello" + sessionsObjects.size() + " sessions"); 
                    for (Sessions session : sessionsObjects) { 
                        sessionHeaders.add(session.getNetsDate()); 
                    } 
                    Log.d("test", "Parse fetched headers" + sessionHeaders.size()); 
                    listener.onDataRetrieved(sessionHeaders); 
                } else { 
                   Log.d("test", "Error: " + e.getMessage()); 
                   listener.onDataRetrieveFail(e); 
                } 
            } 
       }); 

  } 

  public interface ParseIOListener<T>{ 
       void onDataRetrieved(ArrayList<T> listOfData); 
       void onDataRetrieveFail(ParseException e); 
  } 
}

现在,如果您注意到您的方法返回void。但需要一个界面。因此,当您调用此方法时,您可以设置界面,并在调用它时将包含您的数据 所以例如获取标题:

 sessionHeaderFetch(new ParseIOListener<Date>() { 
     @Override 
     public void onDataRetrieved(ArrayList<Date> listOfData) { 
         //here you have your listOfData
     } 

     @Override 
     public void onDataRetrieveFail(ParseException e) { 
         //error. Log it out and handle it here
     } 
 });