gpt4 book ai didi

java - Android:从 Activity 到其他 fragment 选项卡的接口(interface)不起作用

转载 作者:行者123 更新时间:2023-12-02 02:34:21 24 4
gpt4 key购买 nike

我的应用程序中有 3 个选项卡,我通过 InterfaceMainActivity 中的提示(由 float 操作按钮调用)更新每个选项卡。根据显示的选项卡,提示有所不同。

我想在提示中单击“确定”后立即更新当前显示的选项卡,但是发生的情况是只有第一个选项卡中的界面正在工作,因此只有选项卡 1 得到更新。

当我单击第二个选项卡并从 MainActivity 的 FAB 调用提示时,在第二个选项卡的提示中单击“确定”后,第二个选项卡不会更新,尽管这在第一个选项卡是时有效显示。

我该如何解决这个问题?请帮忙。

这是我的代码:

主要 Activity 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
}

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) {

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();
}


}

查看资金:

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

==========================================

最佳答案

你的这段代码没有获得正确的制表符位置...它总是给出 0 位置,这就是为什么每次只执行第一种情况.. intposition = tabLayout.getSelectedTabPosition();

使用 OnTabSelectedListener...即

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
@Override
public void onTabSelected(TabLayout.Tab tab){
int position = tab.getPosition();
}
});

使用上面的代码您将获得正确的选项卡位置....那么你的开关盒将落入正确的大小写,你将得到正确的结果...

关于java - Android:从 Activity 到其他 fragment 选项卡的接口(interface)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46619391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com