目前我只能在點擊刷新按鈕時檢索數據。如何在應用程序啓動時通過解析自動檢索數據?
如何在每次啓動應用程序或每次在頁面之間切換時自動從解析中檢索數據? 謝謝。
相關代碼:
public class MyActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
String eventTxt;
EditText seteditTxt;
ParseObject Events = new ParseObject("Events");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
seteditTxt = (EditText) findViewById(R.id.seteditTxt);
updatelocalData();
updateData();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
seteditTxt = (EditText) findViewById(R.id.seteditTxt);
int id = item.getItemId();
if (id == R.id.action_refresh) {
updatelocalData();
updateData();
}
if (id == R.id.action_example) {
eventTxt = seteditTxt.getText().toString();
Events.put("EventName", eventTxt);
Events.saveEventually(new SaveCallback() {
public void done(ParseException e) {
if (e == null) {
// Saved successfully.
Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();
} else {
// The save failed.
Toast.makeText(getApplicationContext(), "Failed to Save", Toast.LENGTH_SHORT).show();
}
}
});
Events.pinInBackground();
}
return super.onOptionsItemSelected(item);
}
public void updateData() {
ParseQuery <ParseObject> query = ParseQuery.getQuery("Events");
query.getInBackground("d51GM3syxp", new GetCallback <ParseObject>() {
public void done(ParseObject Events, ParseException e) {
if (e == null) {
String Txt = Events.getString("EventName");
seteditTxt.setText(Txt);
} else {
// something went wrong
System.out.print("Error in parse retrieving");
}
}
});
}
public void updatelocalData() {
ParseQuery <ParseObject> query = ParseQuery.getQuery("Events");
query.fromLocalDatastore();
query.getInBackground("d51GM3syxp", new GetCallback <ParseObject>() {
public void done(ParseObject Events, ParseException e) {
if (e == null) {
String Txt = Events.getString("EventName");
seteditTxt.setText(Txt);
} else {
// something went wrong
System.out.print("Error in locally retrieving");
}
}
});
}
}
當我添加:
seteditTxt = (EditText) findViewById(R.id.seteditTxt);
updatelocalData();
updateData();
在我的onCreate(
)這讓我在seteditTxt第一updatelocalData()方法的錯誤說: 嘗試在null對象引用上調用虛方法'void android.widget.EditText.setText(java.lang.CharSequence)' -
activity_my.xml文件:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MyActivity" android:background="@drawable/background">
<RelativeLayout android:id="@+id/container" android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.mycompany.e_planner.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
片段xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.mycompany.e_planner.Calendar1"
android:background="#7dffe7f5">
<EditText
android:layout_width="match_parent"
android:layout_height="56dp"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/seteditTxt"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#7dfff6ec"
android:autoText="false"
android:text=" " />
</RelativeLayout>
有一個基本的活動和寫你的邏輯,所以當你在活動之間導航時,它會加載解析內容 –
謝謝。基地活動在?數據顯示在片段上 - 這是主要活動。 @HarshaVardhan –
好。仍然可以創建基本片段和onstart片段調用基本活動的方法 –