ParseQueryAdapter 不获取项目

ParseQueryAdapter not fetching the items

我有一个 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 一起,我的解析模型中也有以下方法 class。

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

解析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);
    }
}

在聊天中与我分享代码后,我相信这是最适合您的解决方案: 将您的 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); 
  } 
}

现在,如果您注意到您的方法 returns 无效。但是需要接口。所以当你调用这个方法时你可以设置接口,当它被调用时它会包含你的数据 例如得到 headers:

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