目前,我只能在单击刷新按钮时检索数据。

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

相关代码:

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()方法中给我一个错误:
尝试在空对象引用上调用虚拟方法'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();


根据您的需要添加到Activity的onCreate()onStart()onResume()
如果在onCreate()中执行此操作,请确保方法依赖的所有UI元素(包括EditText seteditTxt)都已初始化。

10-01 20:17
查看更多