2014-11-25 60 views
0

我是Android和Google Guide的新手,我在這部分:http://developer.android.com/training/basics/data-storage/databases.htmlAndroid - 如何堆疊動態添加視圖?

是的,它是關於dabatase,但把它在實踐中,在我的虛擬學習程序,我創建了專賣店「曲線」一個簡單的系統有東西在數據庫中存儲。基本上,我創建了一個Activity來填充一些文本字段並將其存儲在數據庫中。還有一個叫做ProfileDao的類來完成CRUD的工作。

我已經達到了要使用LinearLayout創建活動以顯示所添加的所有配置文件的點。問題是我不能像XML那樣在代碼中堆疊視圖。

下面是一些代碼: 的ProfileFragment類:

public final class ProfileFragment extends Fragment { 
    private String _name; 
    private String _gender; 
    private int _age; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     final View retVal = inflater.inflate(R.layout.profile_fragment, container, false); 
     final TextView _nameField = (TextView) retVal.findViewById(R.id.profile_fragment_name); 
     final TextView _genderField = (TextView) retVal.findViewById(R.id.profile_fragment_gender); 
     final TextView _ageField = (TextView) retVal.findViewById(R.id.profile_fragment_age); 

     _nameField.setText(_name); 
     _genderField.setText(_gender); 
     _ageField.setText(String.valueOf(_age)); 

     return retVal; 
    } 

    public void setName(String name) { _name = name; } 
    public void setGender(String gender) { _gender = gender;} 
    public void setAge(int age) { _age = age; } 

    public String getName() { return _name; } 
    public String getGender() { return _gender; } 
    public int getAge() { return _age; } 
} 

我的意圖最初是使用這個片段dinamically把它放在一個的LinearLayout,但我很快就發現,我不能做到這一點,使這個類只是一個util返回一個視圖。

的SeeProfilesActivity:

public class SeeProfilesActivity extends Activity { 
    private static final String TAG = makeLogTag(SeeProfilesActivity.class); 

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

     final ProfileDao profDao = new ProfileDao(new ProfileDbHelper(this)); 
     final List<Profile> profiles = profDao.getProfiles(); 
     LOGD(TAG, "PROFILES RETRIEVED"); 

     if(profiles.size() == 0) { 
      final TextView thereisNotProfiles = new TextView(this); 
      thereisNotProfiles.setText(getString(R.string.there_is_not_profiles)); 
      thereisNotProfiles.setTextSize(35); 

      LOGD(TAG, "NO PROFILES, SO JUST DISPLAY THERE IS NOT"); 

      setContentView(thereisNotProfiles); 
      return; 
     } 

     addFragmentProfiles(profiles); 
    } 

    private void addFragmentProfiles(List<Profile> profiles) { 
     final LinearLayout itLayout = (LinearLayout) ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0); 
     itLayout.setOrientation(LinearLayout.VERTICAL); 
     View currView; 

     for(Profile prof : profiles) { 
      final ProfileFragment profFrag = new ProfileFragment(); 

      profFrag.setName(prof.getName()); 
      profFrag.setGender(prof.getGender()); 
      profFrag.setAge(prof.getAge()); 

      LOGD(TAG, "WELL, ONE PROFILE WAS RETRIEVED"); 

      currView = profFrag.onCreateView(getLayoutInflater(), itLayout, null); 
      itLayout.addView(currView);//here is my problem 
      LOGD(TAG, "SUCCESS IN DISPLAY"); 
     } 

    } 
} 

如何重現疊加出現在代碼中的LinearLayout XML意見的性能如何?

一些反對前:

  • 我知道你們當中有些人會說有一個更好的方式來做到這一點,主要是experient開發者(也許說我可以使用的ListView與適配器是的,我知道,但是。我還沒有達到這部分指南)。但是,拿到其實我是一個很新鮮的初學者,真想用手做此享受最大一個什麼樣的學習。

回答

0

更改碎片佈局「profile_fragment」高度是wrap_content,不填充或匹配父項。 我的英文不是很好,我希望你能理解....

R.Layout.profile_fragment:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" // This is your problem. 
    android:orientation="vertical" > 
+0

= OOOOO,神了!有效!哈哈,對於這個問題感到抱歉。我的錯誤只是看代碼。 – stacker 2014-11-25 03:16:54