我刚接触android,为此进行了数周的研究,但我仍然找不到使它起作用的方法。 :(

我的主要活动中有4个片段选项卡(动态创建)。我从“主要活动”的提示中输入金额,并将其保存到数据库。我想在提示符下按OK按钮以将保存的金额保存到数据库并将其显示在第一个选项卡片段上之后,更新第一个选项卡中的值。

如何刷新第一个标签中的值?

在我的片段中,我有这段代码来显示数据。但这仅在最初显示数据,而在我从“主要活动”中输入金额后才显示。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  final View rootview = inflater.inflate(R.layout.home_tab, container, false);
  super.onCreate(savedInstanceState);
  myDb = new DatabaseHelper(getContext());
  viewBudget(rootview);
  return rootview;
}
public View viewBudget(final View rootview) {
  //get values from database and display in dynamic views
}


非常感谢您的回答。先感谢您。

编辑:这是我完整的viewBudget代码:



public View viewBudget(final View rootview) { //display funds dynamically in config layout | LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState
  //Cursor res = myDb.getAllData();
  //String[] textArray = {"One", "Two", "Three", "Four"};
  Double budget, totalpct, temp_pct;

  Cursor res2 = myDb.getConfigData();
  res2.moveToFirst();

  Cursor res3 = myDb.getLatestIncome();
  res3.moveToFirst();
  //showMessage("Number of rows",Integer.toString(res2.getCount()));

  Cursor res4 = myDb.getTotalExpenes();
  res4.moveToFirst();

  //view contents of config table
  if (res2.getCount() == 0) {
    // show message
    //showMessage("Error","Nothing found");
    //return;
  }

  final ScrollView scrollView = (ScrollView) rootview.findViewById(R.id.scroll_budget);
  LinearLayout linearLayout = (LinearLayout) rootview.findViewById(R.id.ll_budget);

  LinearLayout llh_texts = new LinearLayout(getActivity());
  llh_texts.setOrientation(LinearLayout.HORIZONTAL);
  LinearLayout llh_texts2 = new LinearLayout(getActivity());
  llh_texts2.setOrientation(LinearLayout.HORIZONTAL);
  LinearLayout llh_texts3 = new LinearLayout(getActivity());
  llh_texts3.setOrientation(LinearLayout.HORIZONTAL);
  LinearLayout llh_texts4 = new LinearLayout(getActivity());
  llh_texts4.setOrientation(LinearLayout.HORIZONTAL);

  LinearLayout.LayoutParams lp_llh_texts = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
  llh_texts.setLayoutParams(lp_llh_texts);

  //final View linearLayout = getActivity().findViewById(R.id.ll_config);
  linearLayout.removeAllViews(); //clear layout first - LINE WITH ISSUE
  linearLayout.setGravity(Gravity.CENTER);

  //LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
  //LinearLayout.LayoutParams lp3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

  //compute for value of budget
  budget = 0.0;
  totalpct = 0.0;

  linearLayout.addView(llh_texts); //add button h.linearlayout to parent linearlayout inside scrollview

  TextView textBudget = new TextView(getActivity());
  TextView labelBudget = new TextView(getActivity());
  labelBudget.setText("Budget: ");
  llh_texts.addView(labelBudget);
  llh_texts.addView(textBudget);

  TextView textIncome = new TextView(getActivity());
  TextView labelIncome = new TextView(getActivity());
  labelIncome.setText("Income: ");
  linearLayout.addView(llh_texts2);
  llh_texts2.addView(labelIncome);
  llh_texts2.addView(textIncome);

  TextView textTotalFunds = new TextView(getActivity());
  TextView labelFunds = new TextView(getActivity());
  labelFunds.setText("Total Savings Funds: ");
  linearLayout.addView(llh_texts3);
  llh_texts3.addView(labelFunds);
  llh_texts3.addView(textTotalFunds);

  TextView textTotalExpenses = new TextView(getActivity());
  TextView labelExpenses = new TextView(getActivity());
  labelExpenses.setText("Total Scheduled Expenses: ");
  linearLayout.addView(llh_texts4);
  llh_texts4.addView(labelExpenses);
  llh_texts4.addView(textTotalExpenses);

  //create dynamic objects inside scrollview and dynamic linear layout - horizontal
  for (int i = 0; i < res2.getCount(); i++) {
    LinearLayout llh = new LinearLayout(getActivity());
    llh.setOrientation(LinearLayout.HORIZONTAL);
    LinearLayout.LayoutParams lp_llh = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    llh.setLayoutParams(lp_llh);
    //llh.setLayoutParams(lp4,llh.setOrientation(LinearLayout.HORIZONTAL); //for length and width

    linearLayout.addView(llh);

    //LinearLayout.LayoutParams lp_np = new LinearLayout.LayoutParams(70, LinearLayout.LayoutParams.WRAP_CONTENT);

    temp_pct = (Double.valueOf(res2.getString(2)) / 100) * Double.valueOf(res3.getString(3));

    EditText editText = new EditText(getActivity());
    editText.setText(String.valueOf(temp_pct));
    editText.setEnabled(false);

    EditText editText2 = new EditText(getActivity());
    editText2.setText(String.valueOf(res2.getString(2)) + "%");
    editText2.setEnabled(false);

    //get total of funds
    totalpct = totalpct + temp_pct;

    //showMessage("value",res2.getString(3));
    TextView textView = new TextView(getActivity());
    textView.setText(res2.getString(1));

    llh.addView(textView);

    llh.addView(editText);
    llh.addView(editText2);

    res2.moveToNext();
  }

  //return scrollView;
  //create budget text and content
  budget = Double.valueOf(res3.getString(3)) - (totalpct + res4.getDouble(0));
  textBudget.setText(String.valueOf(budget));
  textIncome.setText(String.valueOf(res3.getString(3)));
  textTotalFunds.setText(String.valueOf(totalpct));
  textTotalExpenses.setText(String.valueOf(res4.getDouble(0)));

  return rootview;
}





编辑:根据布瓦涅什的回答,将代码添加到第三片段标签:



@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setHasOptionsMenu(true); //Make sure you have this line of code.
  setUserVisibleHint(false);
}

//@Override /*<-- this returned  "method does not override method in its superclass" so I commented out*/
public void setUserVisibleHint(final View view, boolean isVisibleToUser) {
  super.setUserVisibleHint(isVisibleToUser);
  if (isVisibleToUser) {
    //get data from database and refresh view.
    viewFunds(view); //this method refreshes the rootview of the 3rd fragment
  }
}

@Override
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
  super.onViewCreated(view, savedInstanceState);
  ((Main2Activity) getActivity()).setPopupListener(new Main2Activity.PopupListener() {
    @Override
    public void onDialogClick(String value) {
      //After clicking dialog ok button in Activity
      // you will get value here.
      //viewFunds(view);
      setUserVisibleHint(view, true);
    }
  });
}

最佳答案

您可以通过界面来完成。

在活动中创建接口对象,然后从片段中设置对象。

在您的活动中,像这样创建Lister对象:

public class MyActivity extends AppCompatActivity {

    private PopupListener popupListener;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Your dialog code.
         setPositiveButton("Ok", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                 popupListener.onDialogClick(your_value);
              }
         })
    }

    public void setPopupListener(PopupListener popupListener) {
        this.popupListener = popupListener;
    }

    public interface PopupListener{
        void onDialogClick(String value);
    }
}


在您的片段中,您必须像这样设置侦听器:

public class MyFragment extends Fragment{

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ((MyActivity)getActivity()).setPopupListener(new MyActivity.PopupListener() {
            @Override
            public void onDialogClick(String value) {
                //After clicking dialog ok button in Activity
                // you will get value here.
            }
        });
    }
}


要更新viewPager的其他片段内容,请在所有viewPager片段中覆盖以下方法。

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
      //get data from database and refresh view.
    }
}


在此之前,在片段onCreate()中添加以下代码。

public void onCreate(@Nullable Bundle savedInstanceState) {
    setUserVisibleHint(false);
}


希望能帮助到你:)

10-04 10:20
查看更多