以编程方式Android刷新/更新EditText和LinearLayout不起作用

时间:2014-12-06 14:49:35

标签: android android-layout android-edittext android-linearlayout

我需要一些帮助来更新LinearLayout,其中包含许多以编程方式添加的EditText框。添加新信息后,我首先删除所有带有removeAllViews()的框,从干净的平板开始。 我也有标签布局,所以通过单击其中一个选项卡,我运行一个get / setter(sg类),我选择此选项卡后,我将从此代码中访问该选项卡。我可以在日志打印输出中看到应该放入EditText的内容是正确的......但是在我的应用程序的EditText框中,名称和值不会更新。 我试图使用invalidate()但它没有用。有什么好的提示吗?

以下是代码的一部分:

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("0123456789.,"));
            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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

感谢您的帮助。 我设法解决了它。 我为解决这个问题所做的是将代码添加到AsyncTask中(在后执行中...因为它运行在我猜测的主要部分上)然后以&#34; myLinearLayout.invalidate()结束以确保&#34 ;。 现在它有效:)