如何在应用程序启动时通过解析自动检索数据?

目前我只能在点击刷新按钮时检索数据。如何在应用程序启动时通过解析自动检索数据?

如何在每次启动应用程序或每次在页面之间切换时自动从解析中检索数据? 谢谢。

相关代码:

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>

回答:

只需推动相关方法,我想这些:

updatelocalData(); 

updateData();

到活动的onCreate()onStart(),或onResume()取决于你的需求。 如果你在onCreate()这样做,确保所有的UI元素,包括EditText seteditTxt所依赖的方法都已经初始化。

以上是 如何在应用程序启动时通过解析自动检索数据? 的全部内容, 来源链接: utcz.com/qa/261618.html

回到顶部