2013-12-18 28 views
0

我曾經用iOS語言進行編程,每個UIView類都有自己的UIViewController類來管理/填充視圖本身。我正在嘗試編寫一個簡單的android應用程序,它從url解析JSONArray,然後填充四個視圖,但我不知道如何爲每個視圖實現一個類,並將它們的字符串參數傳遞給它們以填充它們。你能告訴我什麼是實現應用邏輯的最佳方式?我有swipable-tabs的新項目,然後我必須使用碎片。這些片段是否與iOS中的UIView相同?請幫幫我。在android中填充/管理視圖的更好方法?

我這樣做了,我想知道它是否正確。我創建了一個帶有空白活動的新項目,並以導航類型的形式創建了「可滾動選項卡+輕掃」。

我的主要活動:

公共類MyMainActvity擴展FragmentActivity {

private static String url = "http://www.myurl.it"; 

static JSONObject jObj = null; 
/** 
* The {@link android.support.v4.view.PagerAdapter} that will provide 
* fragments for each of the sections. We use a 
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which 
* will keep every loaded fragment in memory. If this becomes too memory 
* intensive, it may be best to switch to a 
* {@link android.support.v4.app.FragmentStatePagerAdapter}. 
*/ 
SectionsPagerAdapter mSectionsPagerAdapter; 

/** 
* The {@link ViewPager} that will host the section contents. 
*/ 
ViewPager mViewPager; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main_activity); 

    new JSONParse().execute(); 

    // Create the adapter that will return a fragment for each of the three 
    // primary sections of the app. 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main_activity, menu); 
    return true; 
} 

/** 
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
* one of the sections/tabs/pages. 
*/ 
public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     // getItem is called to instantiate the fragment for the given page. 
     // Return a DummySectionFragment (defined as a static inner class 
     // below) with the page number as its lone argument. 

     switch (position) { 
     case 0: 
      { 
       HomeSection homeFrag= new HomeSection(); 
       homeFrag.newInstance(jObj); 
       return homeFrag; 
      } 
     case 1: 
     { 
      ServiceSection servFrag= new ServiceSection(); 
      servFrag.newInstance(jObj); 
      return servFrag; 
     } 
     case 2: 
      { 
       Fragment fragment = new DummySectionFragment(); 
       Bundle args = new Bundle(); 
       args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); 
       fragment.setArguments(args); 
       return fragment; 
      } 
     case 3: 
     { 
      Fragment fragment = new DummySectionFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); 
      fragment.setArguments(args); 
      return fragment; 
     } 
     } 
     return null; 
    } 

    @Override 
    public int getCount() { 
     // Show 4 total pages. 
     return 4; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     Locale l = Locale.getDefault(); 
     switch (position) { 
     case 0: 
      return getString(R.string.title_section1).toUpperCase(l); 
     case 1: 
      return getString(R.string.title_section2).toUpperCase(l); 
     case 2: 
      return getString(R.string.title_section3).toUpperCase(l); 
     case 3: 
      return getString(R.string.title_section4).toUpperCase(l); 
     } 
     return null; 
    } 
} 

/** 
* A dummy fragment representing a section of the app, but that simply 
* displays dummy text. 
*/ 
public static class DummySectionFragment extends Fragment { 
    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
    public static final String ARG_SECTION_NUMBER = "section_number"; 

    public DummySectionFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(
       R.layout.fragment_dummy, container, false); 
     TextView dummyTextView = (TextView) rootView 
       .findViewById(R.id.section_label); 
     dummyTextView.setText(Integer.toString(getArguments().getInt(
       ARG_SECTION_NUMBER))); 
     return rootView; 
    } 
} 

private class JSONParse extends AsyncTask<String, String, JSONArray> { 
    private ProgressDialog pDialog; 
    @Override 
    protected void onPreExecute() { 

     super.onPreExecute(); 
     pDialog = new ProgressDialog(MyMainActivity.this); 
     pDialog.setMessage("Getting Data ..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 
    @Override 
    protected JSONArray doInBackground(String... args) { 

     JSONParser jParser= new JSONParser(); 
     JSONArray json =jParser.getJSONFromUrl(url); 

     return json; 
    } 
    @Override 
    protected void onPostExecute(JSONArray json) { 

     pDialog.dismiss(); 
     Log.d("JSONARRAY:", json.toString()); 

     try { 
      JSONObject json_data = json.getJSONObject(0); 
      jObj= json_data; 

      mSectionsPagerAdapter = new SectionsPagerAdapter(
        getSupportFragmentManager()); 

      // Set up the ViewPager with the sections adapter. 
      mViewPager = (ViewPager) findViewById(R.id.pager); 
      mViewPager.setAdapter(mSectionsPagerAdapter); 
     } 

     catch(JSONException exception) { 

      Log.e("ERROR", exception.getMessage()); 
     } 
    } 
} 

}

這是一個我的一個子類片段:

公共類HomeSection擴展片段{

JSONObject _jObj; 
public HomeSection(){} 

public void newInstance(JSONObject jObj) { 
    Bundle args = new Bundle(); 

    _jObj= jObj; 

    try{ 
     String content= _jObj.getString("descrizione"); 
     args.putString("description", content); 
    } 
    catch(JSONException exception){ 

     Log.e("ERROR JSON HOME", exception.getMessage()); 
    } 

    // Put any other arguments 
    this.setArguments(args); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View rootView = inflater.inflate(
      R.layout.fragment_dummy, container, false); 
    TextView dummyTextView = (TextView) rootView 
      .findViewById(R.id.section_label); 
    try { 
     dummyTextView.setText(_jObj.getString("descrizione")); 
    } 
    catch(JSONException exception){ 

     Log.e("ERROR JSON HOME", exception.getMessage()); 
    } 

    return rootView; 
} 

}

它的工作原理,但我想如果它是一種正確的方式填充我的應用程序中的意見。然後我不知道如果在HomeSection類中,我在「newIstance」方法中創建的Bundle是非常有必要的,因爲我在方法onCreateView中的視圖中設置了我的文本。請問你能幫幫我嗎?謝謝

回答

0

編號片段不等同於UIView。 UIView的等價物是Android View類。但是,這在大多數情況下不會直接使用。

與iOS不同,Android不使用純粹的MVC模式。它使用更像MVP模式的東西(請參閱MVC pattern on AndroidWhich design patterns are used on Android?)。這裏,該Activity充當主視圖容器,並且是應用程序的主要入口點。

基本上,你可以使用View類的子類,如TextView的,ImageView的等之一,並把他們的活動或片段中。

活動/片段會使您的視圖膨脹(在解析XML之後使對象不在其中)並將其展示給用戶。然後,用戶可以與視圖交互,這些觸摸事件將由Activity/Fragment分配到相應的視圖,或者由Activity/Fragment本身根據您的邏輯消耗。

我建議你通過這個優秀的Android Bootcamp教程系列,這將真正幫助你學習所有的基本知識和「Android方式」的做事。

+0

我編輯我的問題,如果你可以看看,並告訴我你的意見,我會很高興 – user3107388

+0

我建議你把你的問題的第二部分,使之成爲新的問題來代替。這樣它就會達到目的,你會得到更好的答案。保持原來的問題原樣。 –