2015-04-16 44 views
1

目前我只能在點擊刷新按鈕時檢索數據。如何在應用程序啓動時通過解析自動檢索數據?

如何在每次啓動應用程序或每次在頁面之間切換時自動從解析中檢索數據? 謝謝。

相關代碼:

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> 
+0

有一個基本的活動和寫你的邏輯,所以當你在活動之間導航時,它會加載解析內容 –

+0

謝謝。基地活動在?數據顯示在片段上 - 這是主要活動。 @HarshaVardhan –

+0

好。仍然可以創建基本片段和onstart片段調用基本活動的方法 –

回答

0

只需推動相關方法,我想這些:

updatelocalData(); 
    updateData(); 

到活動的onCreate()onStart(),或onResume()取決於你的需求。 如果你在onCreate()這樣做,確保所有的UI元素,包括EditText seteditTxt所依賴的方法都已經初始化。

+0

當我加: seteditTxt =(EditText)findViewById(R.id。seteditTxt); updatelocalData(); updateData(); 在我的onCreate()它給我一個在seteditTxt的第一個updatelocalData()方法錯誤說: 嘗試調用虛擬方法'void android.widget.EditText.setText(java.lang.CharSequence)'null對象參考 –

+0

你可以發佈onCreate的代碼嗎?您也可以嘗試調用onStart中的方法 – inmyth

+0

使用onCreate的代碼更新我的代碼。 –

相關問題