繁体   English   中英

解析:解析查询 <ParseObject> 从onListItemClick中获取链接2个数据库

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

我在解析中有两个数据库:1)Subject 2)Subject_section

我想使用指针检索数据。 在我的“主题”表中,有一个“节”指针指向表“ Subject_section”。 如何使用onListItemClick检索并显示“主题”表中的数据和“指针”主题中的数据。

我的对象包含指向其他对象的指针,以返回那些(显然)已填充的内部对象。

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

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

我的代码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);
}

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

我的问题是在课堂上显示结果。 我无法从Subject_detail.java获取ParseObject指针。

如何获得指向的指针并从“ Subject_section”类中检索数据。

我在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());

  }

您需要调用fetchIfNeeded() 可能无法从指针获取信息,尤其是在您首次访问指针时。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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