2014-12-05 65 views
0

我有两个数据库中解析: 1)除 2)Subject_section解析:解析查询<ParseObject>从onListItemClick获得连接2数据库

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

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

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

    } 

回答

0

代码需要调用fetchIfNeeded()。指针中的信息可能无法获取,特别是当您第一次访问指针时。

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

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