【发布时间】:2018-03-19 01:35:02
【问题描述】:
我的应用中有 3 个选项卡,我从 MainActivity 到 Interface 中的提示(由浮动操作按钮调用)更新每个选项卡。提示根据显示的选项卡而有所不同。
我想在提示中单击“确定”后立即更新当前显示的选项卡,但是发生的情况是只有第一个选项卡中的界面在工作,因此只有选项卡 1 得到更新。
当我单击第二个选项卡并从 MainActivity 的 FAB 调用提示时,在第二个选项卡的提示中单击“确定”后,第二个选项卡不会更新,尽管这在显示第一个选项卡时有效。
我该如何解决这个问题?请帮忙。
这是我的代码:
MainActivity FAB:
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.floatingActionButton_main2);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = tabLayout.getSelectedTabPosition();
switch (position) {
case 0:
// first tab is selected
AddBudget_Main(); //refresh fragment contents
break;
case 1:
// second tab is selected
/* DOES NOT WORK - 2nd TAB NOT BEING UPDATED*/
addFund_Prompt_Main(); //refresh fragment contents
break;
case 2:
// third tab is selected
break;
}
MainActivity 接口:
public void setPopupListener(PopupListener popupListener) {
this.popupListener = popupListener;
}
public interface PopupListener {
void onDialogClick(String value); //String value
}
片段标签:
@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) {
Toast.makeText(getActivity(), value, Toast.LENGTH_LONG).show();
if (value == "settings_tab") { //settings_tab = 2nd fragment. (different content of value variable according to the fragment)
viewFunds(view); //refresh fragment display
}
}
});
}
addFund_Prompt_Main:
public void addFund_Prompt_Main() {
int cnt;
Cursor res = myDb.getConfigData();
cnt = res.getCount();
if (cnt == 40) {
Toast.makeText(context, "Fund limit of 40 already reached, " +
"delete some funds to be able to enter new items", Toast.LENGTH_LONG).show();
} else {
//code 2
// get prompts.xml view
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.prompts, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
final NumberPicker newPercentage = (NumberPicker) promptsView
.findViewById(R.id.AddPercentage);
newPercentage.setMinValue(0);
newPercentage.setMaxValue(100);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// get user input and set it to result
// edit text
//result.setText(userInput.getText());
//write to database
String getInput;
getInput = userInput.getText().toString();
getInput = userInput.getText().toString().trim();
if (getInput.matches(" ")) {
Toast.makeText(context, "Cannot create empty fund name", Toast.LENGTH_LONG).show();
} else if (getInput.matches("")) {
Toast.makeText(context, "Cannot create empty fund name", Toast.LENGTH_LONG).show();
} else {
//int cntInserted;
//cntInserted=0;
boolean isInserted = myDb.insertFund(userInput.getText().toString().trim(), String.valueOf(newPercentage.getValue()));
if (isInserted == true) {
Toast.makeText(context, "Fund successfully added", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "ERROR: Fund not added", Toast.LENGTH_LONG).show();
}
popupListener.onDialogClick("settings_tab");
//viewFunds(rootview);
//create dynamic edittext to mainactivity
}
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
}
viewFunds:
public View viewFunds(final View rootview) { //display funds dynamically in config layout | L
Cursor res2 = myDb.getConfigData();
res2.moveToFirst();
//showMessage("Number of rows",Integer.toString(res2.getCount()));
LinearLayout linearLayout = (LinearLayout) rootview.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);
//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.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llh.setLayoutParams(lp_llh);
llh.setBackgroundColor(Color.parseColor("#12ba15"));
linearLayout.addView(llh);
NumberPicker numberPicker = new NumberPicker(getActivity());
numberPicker.setMinValue(0);
numberPicker.setMaxValue(100);
LinearLayout.LayoutParams lp_np = new LinearLayout.LayoutParams(70, LinearLayout.LayoutParams.WRAP_CONTENT);
numberPicker.setLayoutParams(lp_np);
numberPicker.setGravity(Gravity.CENTER_VERTICAL);
//showMessage("value",res2.getString(3));
numberPicker.setValue(Integer.parseInt(res2.getString(2))); //
TextView textView = new TextView(getActivity());
textView.setText(res2.getString(1));
llh.addView(textView);
linearLayout.addView(numberPicker);
//create dynamic button
final Button buttonD = new Button(getActivity());
final Button buttonD2 = new Button(getActivity());
buttonD.setLayoutParams(lp2);
buttonD.setText("-");
buttonD.setId(Integer.valueOf(res2.getString(0))); //get id from id of corresponding fund row
buttonD2.setLayoutParams(lp3);
buttonD2.setText("Edit");
buttonD2.setId(Integer.valueOf(res2.getString(0)) + 100); //add 100 to separate id of edit from delete button
ids[i] = Integer.valueOf(res2.getString(0)); //get ids and store to array
buttonD.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
//showMessage("button id", Integer.toString(buttonD.getId()));
// get prompts.xml view
LayoutInflater li = LayoutInflater.from(getActivity());
View promptsView = li.inflate(R.layout.confirm_delete_prompt, null); //assign to layout
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getActivity());
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
/*final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);*/
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
boolean isDelete = myDb.deleteData(String.valueOf(buttonD.getId()));
if (isDelete == true) {
Toast.makeText(getActivity(), "Fund Deleted", Toast.LENGTH_LONG).show();
viewFunds(rootview);
} else {
Toast.makeText(getActivity(), "Fund Not Deleted", Toast.LENGTH_LONG).show();
}
//create dynamic edittext to mainactivity
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
//deleteData();
//Cursor res = myDb.getConfigData();
//cnt=res.getCount();
}
}
);
//edit button
buttonD2.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
int x;
x = buttonD2.getId() - 100;
//showMessage("button id",String.valueOf(x));
Cursor res3 = myDb.getFundName(x);
res3.moveToFirst();
//showMessage("btn id",String.valueOf(buttonD2.getId()));
// get prompts.xml view
LayoutInflater li = LayoutInflater.from(getActivity());
View promptsView = li.inflate(R.layout.update_fund_details, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getActivity());
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editText_Fundname);
final NumberPicker newPct = (NumberPicker) promptsView
.findViewById(R.id.numberPicker_editPct);
//FundName=res3.getF
userInput.setText(res3.getString(1));
newPct.setMinValue(0);
newPct.setMaxValue(100);
newPct.setValue(Integer.valueOf(res3.getString(2)));
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// get user input and set it to result
//write to database
String getInput, getInput2;
getInput = userInput.getText().toString().trim();
getInput2 = String.valueOf(newPct.getValue());
if (getInput.matches(" ")) {
Toast.makeText(getActivity(), "Cannot create empty fund name", Toast.LENGTH_LONG).show();
} else if (getInput.matches("")) {
Toast.makeText(getActivity(), "Cannot create empty fund name", Toast.LENGTH_LONG).show();
} else if (Double.compare(Double.valueOf(getInput2), 0) == 0) {
Toast.makeText(getActivity(), "Cannot create scheduled expense with no amount", Toast.LENGTH_LONG).show();
} else {
if (CheckTotalPercentage(Integer.valueOf(getInput2), String.valueOf(buttonD2.getId()))) {
Toast.makeText(getActivity(), "Total savings funds should not exceed 90% of income", Toast.LENGTH_LONG).show();
} else {
boolean isUpdated = myDb.updateConfigName(buttonD2.getId() - 100, String.valueOf(userInput.getText()), String.valueOf(newPct.getValue()));
if (isUpdated == true)
Toast.makeText(getActivity(), "Data Updated", Toast.LENGTH_LONG).show();
else
Toast.makeText(getActivity(), "Data not Updated", Toast.LENGTH_LONG).show();
viewFunds(rootview);
}
}
//create dynamic edittext to mainactivity
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
}
);
llh.addView(buttonD); //delete
llh.addView(buttonD2); //edit
//linearLayoutD.addView(button);
res2.moveToNext();
}
//return scrollView;
return rootview;
}
viewFunds 示例展示:
=========================================
基金 1 [编辑按钮] [删除按钮]
金额:100
基金 2 [编辑按钮] [删除按钮]
金额:200
基金 3 [编辑按钮] [删除按钮]
金额:300
=========================================
【问题讨论】:
-
你可以为
addFund_Prompt_Main();添加代码吗,只是想看看你如何将数据传递给第二个片段 -
我只是在问题中添加了代码,请参见上文。谢谢。
-
我不认为你真的需要这个接口。只需在所有片段中添加
onDataUpdated(String updatedValue)之类的方法即可。在添加到选项卡之前创建所有片段的引用,然后在提示 ok 时在当前片段上调用 fragment.onDataUpdated(String updatedValue)并在onDataUpdated(String updatedValue)方法中更新该片段。我假设您使用tabLayout.getSelectedTabPosition();获得了正确的片段如果您想要更详细的解释作为答案,请告诉我。 -
是的,
getSelectedTabPosition工作正常。您可以通过添加答案来提供示例代码吗?谢谢
标签: java android android-fragments interface android-fragmentactivity