简体   繁体   中英

Parse: Parse Query<ParseObject> getting from onListItemClick linking 2 database

I have two Database in Parse: 1)Subject 2)Subject_section

I want to retrieve data using the pointer. In my "Subject" table, there is a "section" pointer to table "Subject_section". How do I use onListItemClick to retrieve and show my data inside "Subject" table and data inside my "pointer" Subject_section.

My object contains pointers to other objects to return those (apparently) internal objects populated.

http://i.imgur.com/Y9upOGP.png

http://i.imgur.com/bTl6vju.png

My code Subject_detail.java:

 ParseQuery<ParseObject> query = ParseQuery.getQuery("Subject");
    query.include("Subject_section");
        @Override
        public void done(List<ParseObject> postList, ParseException e) {
            if (e == null) {
                posts.clear();
                Toast.makeText(getApplicationContext(), "Retrieve successfully!",
                        Toast.LENGTH_SHORT).show();
                for (ParseObject post : postList) {
                  //  ParseObject section=post.getParseObject("Subject_section");

                    Subject subject = new Subject(
                            post.getString("Subject_code"),
                            post.getString("Subject_name"),
                            post.getParseObject("Subject_section"));

                    posts.add(subject);

                }
                ((ArrayAdapter<Subject>) getListAdapter()).notifyDataSetChanged();
            }
        }
    });
     protected void onListItemClick(ListView l, View v, int position, long id){
    super.onListItemClick(l,v,position,id);

    ParseObject item=(ParseObject) l.getAdapter().getItem(position);
    String section=item.getParseObject("Subject_section").toString();

    Subject subject=posts.get(position);      
    Intent intent=new Intent(this,Edit_Subjects.class);
    intent.putExtra("subjectCode",subject.getSubject_code());
    intent.putExtra("subjectName",subject.getSubject_name());
    intent.putExtra("SubjectSection",section);

    startActivity(intent);
}

My code in subject.java

public class Subject {
private String Subject_code;
private String Subject_name;
private ParseObject Subject_section;

Subject(String subjectCode,String subjectName,ParseObject subjectSection){
Subject_code=subjectCode;
Subject_name=subjectName;
Subject_section=subjectSection;

public String getSubject_code(){
    return Subject_code;
}
public void setSubject_code(String code){
    this.Subject_code=code;
}

public String getSubject_name(){return Subject_name;}
public void setSubject_name(String name){ this.Subject_name=name;}

public ParseObject getSubject_section(){return Subject_section;}
public void setSubject_section(ParseObject subject_section) {
    Subject_section = subject_section;
}

public String toString (){
return this.getSubject_code();
}
}

My problem is at my class showing the result. I could not get the ParseObject pointer from Subject_detail.java.

How do I get the pointer pointing and retrieve data from "Subject_section" class.

My code in Edit_Subject.java

private EditText subject_code;
private EditText subject_name;
private EditText section;
private Subject subject;
private Subject_section subject_section;
@OVerride
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_subjects);
Intent intent=this.getIntent();
subject_code =(EditText)findViewById(R.id.subject_code);
subject_name=(EditText)findViewById(R.id.subject_name);
section=(EditText)findViewById(R.id.section);

 if (intent.getExtras()!=null){
 subject=new Subject(intent.getStringExtra("subjectCode"),
                     intent.getStringExtra("subjectName"),
                     intent.getStringExtra("Subject_section"));//Error here getting ParseObject

 subject_code.setText(subject.getSubject_code());
 subject_name.setText(subject.getSubject_name());
 section1.setText(subject_section.getSection_1());
 section2.setText(subject_section.getSection_2());
 section3.setText(subject_section.getSection_3());
 section4.setText(subject_section.getSection_4());

  }

You need to call fetchIfNeeded() . Information from the pointer may not be fetched, especially when you access it for the first time.

ParseObject child = parentParseObject.getParseObject("child");

try {
    child.fetchIfNeeded(); // fetches information from Parse
} catch (ParseException ex) {ex.printStackTrace();}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM