我试图将整数从7个不同的片段发送到第8个片段,该片段将被发送到SQLite数据库。不过目前,我什么也无法发送到另一个片段。
MiscTab.java
package com.example.test.test;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MiscTab extends Fragment {
private static final String TAG = "MiscTab";
private View root;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup @Nullable Bundle savedInstanceState) {
// Find the root view, and put in into a variable for later use by other functions
this.root = inflater.inflate(R.layout.misc_tab, container, false); //creates this.setupButton(R.id.misc_dead_bot_int, R.id.misc_dead_bot_plus, 0, 1, 1);
this.setupButton(R.id.misc_dead_bot_int, R.id.misc_dead_bot_minus, 0, 1, -1);
this.setupButton(R.id.misc_disabled_bot_int, R.id.misc_disabled_bot_plus, 0, 1, 1);
this.setupButton(R.id.misc_disabled_bot_int, R.id.misc_disabled_bot_minus, 0, 1, -1);
this.setupButton(R.id.misc_tipped_bot_int, R.id.misc_tipped_bot_plus, 0, 1, 1);
this.setupButton(R.id.misc_tipped_bot_int, R.id.misc_tipped_bot_minus, 0, 1, -1);
this.setupButton(R.id.misc_didnt_show_int, R.id.misc_didnt_show_plus, 0, 1, 1);
this.setupButton(R.id.misc_didnt_show_int, R.id.misc_didnt_show_minus, 0, 1, -1);
return this.root;
}
private void setupButton(int textID, int buttonID, final int minimum, final int maximum, final int stepValue) {
// Find the button from the view
Button button = this.root.findViewById(
// Find the buttons textview from the view
final TextView text = this.root.findViewById(
// Set the buttons actions to increase the count by the step value
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View
// Find the text views current value. If there is an error, assume 0
int initialCount;
try {
initialCount = Integer.parseInt(text.getText().toString());
} catch (Exception e) {
e.printStackTrace();
initialCount = 0;
}
// Calculate the final count value
int finalCount = initialCount + stepValue;
// Check if it matches the bounds
if (finalCount > maximum) {
finalCount = maximum;
}
if (finalCount < minimum) {
finalCount = minimum;
}
// Update the text to display the new final count
text.setText(String.valueOf(finalCount));
}
});
}
}
MainActivity.java中的相关代码
Bundle arguments = new Bundle();
arguments.putInt(MiscTab.misc_dead_bot_int);
当我尝试将MiscTab.misc_dead_bot_int
传递给putInt的参数时,它告诉我无法解决它。我认为这是因为它没有在任何地方初始化,而是仅跟随XML中的字符串。我不确定如何将这些值转换为可发送格式。我尝试将其发送到字符串,但是我得到的唯一输出值却是一个大数字。
答案 0 :(得分:0)
我不清楚您的问题。您正在尝试从片段中检索此(MiscTab.misc_dead_bot_int)值,对吗?无论如何,您上面的Bundle示例具有错误的参数。确保您通过正确。 bundle函数putInt具有两个参数,它们是键值对。我想你错过了一把钥匙。尝试如下
在您的主要活动中
Bundle bundle = new Bundle();
bundle.putInt(YOUR_KEY, MiscTab.misc_dead_bot_int);
在你的片段中
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
int misc_dead_bot_int = getArguments().getInt(YOUR_KEY);
}