0

我需要一些帮助,以更新LinearLayout与大量EditText框以编程方式添加。添加新信息时,首先使用removeAllViews()删除所有框以从干净的平板开始。 我也有选项卡布局,所以通过点击其中一个选项卡,我运行一个get/setter(sg类),我稍后从此代码访问此选项卡时选择。我可以在Log打印输出中看到应该放入EditText的内容是正确的......但是在我的应用程序的EditText框中,名称和值不会更新。 我试图使用invalidate(),但它没有工作。任何好的小费?Android刷新/更新EditText和LinearLayout以编程方式不起作用

下面是代码的一部分:

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
    rootView = inflater.inflate(R.layout.tab4_layout, container, false); 

    ... 
    ... 
    ... 

    //To remove all current views 
    myLinearLayout.removeAllViews(); 
    //myLinearLayout.invalidate(); //<--I tried that 
    for(int i : valueArrayNew){ 
     if(i==0) { 
      editableTextView = new EditText(context); 
      editableTextView.setHint(colNames[valueArrayNew.get(i)]); 
      editableTextView.setTextSize(20); 
      editableTextView.setId(valueArrayNew.get(i)); 
      editableTextView.setHintTextColor(getResources().getColor(R.color.lightgrey)); 
      editableTextView.setLayoutParams(params); 
      editableTextView.setText(""+sg.getName()); //<--The name is only updated the first time this code is run. 
      Log.d("HAZE", "Name: " + sg.getName());  //This is not the same name the second time I run and add text to the EditText. This printout is correct and prints the wanted name. 
      myLinearLayout.addView(editableTextView); 
     }else { 
      editableTextView = new EditText(context); 
      editableTextView.setHint(colNames[valueArrayNew.get(i)]); 
      editableTextView.setTextSize(20); 
      editableTextView.setId(valueArrayNew.get(i)); 
      editableTextView.setHintTextColor(getResources().getColor(R.color.lightgrey)); 
      editableTextView.setKeyListener(DigitsKeyListener.getInstance(".,")); 
      editableTextView.setLayoutParams(params); 
      editableTextView.setText(""+sg.getValueForNumber(i)); //<--The value is only updated the first time this code is run. 
      myLinearLayout.addView(editableTextView); 
     } 
    } 
    //myLinearLayout.invalidate(); //<--I tried that 

    ... 
    ... 
    ... 

    return rootView; 
} 




public class MyTabs extends Activity { 

// Declaring our tabs and the corresponding fragments. 
public ActionBar.Tab Tab1, Tab2, Tab3,Tab4; 
Fragment FragmentTab1 = new com.haze.purple.tabs.firstFragmentTab(); 
Fragment FragmentTab2 = new com.haze.purple.tabs.secondFragmentTab(); 
Fragment FragmentTab3 = new com.haze.purple.tabs.thirdFragmentTab(); 
Fragment FragmentTab4 = new com.haze.purple.tabs.fourthFragmentTab(); 
private static Context context; 
ActionBar actionBar; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout._fragment); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

    // Asking for the default ActionBar element that our platform supports. 
    actionBar = getActionBar(); 

    // Screen handling while hiding ActionBar icon. 
    actionBar.setDisplayShowHomeEnabled(false); 

    // Screen handling while hiding Actionbar title. 
    actionBar.setDisplayShowTitleEnabled(false); 

    // Creating ActionBar tabs. 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    Tab1 = actionBar.newTab().setText(R.string.tab1_name); 
    Tab2 = actionBar.newTab().setText(R.string.tab2_name); 
    Tab3 = actionBar.newTab().setText(R.string.tab3_name); 
    Tab4 = actionBar.newTab().setText(R.string.tab4_name); 

    // Setting tab listeners. 
    Tab1.setTabListener(new TabListener(FragmentTab1)); 
    Tab2.setTabListener(new TabListener(FragmentTab2)); 
    Tab3.setTabListener(new TabListener(FragmentTab3)); 
    Tab4.setTabListener(new TabListener(FragmentTab4)); 
    actionBar.addTab(Tab1); 
    actionBar.addTab(Tab2); 
    actionBar.addTab(Tab3); 
    actionBar.addTab(Tab4); 
    ... 
    ... 

....和XML文件是这样的:

A LinearLayout holds it all in in that a ScrollView and in that ScrollView I have another LinearLayout. IN the last LinearLayout I add all EditText text boxes. 

LinearLayout 
    ScrollView 
     LinearLayout 
+0

如何将fragment添加到'fragmentManager' – 2014-12-06 14:56:32

+0

嗨我添加片段到oncreate在我的主要活动中的操作栏。我在该活动中也有一个setTabListener。 – user2034859 2014-12-06 15:29:40

+0

发布我的容器活动的代码,因为我怀疑当你添加片段给fragmentmanager时,你会使用add。当你点击标签时,U需要替换片段,所以它会从开始重新创建,你可以看到新的数据。 – 2014-12-07 03:43:07

回答

0

感谢您的全力帮助。 我设法解决它。 我所做的解决这个问题的方法是将代码添加到AsyncTask中(在执行后执行...因为它运行在主要的Thue上)然后以“myLinearLayout.invalidate()来结束”。 现在它的工作原理:)

0

你可以尝试从其父视图中删除myLinearLayout和然后以编程方式创建一个新的LinearLayout,然后动态添加字段。最后将新布局设置为其父视图

+0

我真的不知道该怎么做。我现在也试过了:rootView.invalidate(); 一个奇怪的是,我添加了这段代码并加班我切换按钮所有视图都正确更新,但不仅仅是oncreateView ...所以我必须按下按钮: Switch onOffSwitch =(Switch)rootView.findViewById(R .id.on_off_switch); onOffSwitch.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){ \t @覆盖 \t公共无效onCheckedChanged(CompoundButton buttonView,布尔器isChecked){ \t ... \t相同的代码作为oncreateView \t ... \t} }); – user2034859 2014-12-06 19:06:13