在A片段点击碎片B按钮时更新的TextView [英] update TextView in fragment A when clicking button in fragment B
问题描述
我有两个片段在同一个活动坐在并排。当我在正确的片段(片段B)轻触一个按钮,我需要一个TextView左片段更新(片段A)。我已经到处寻找做到这一点的最好办法,但似乎没有任何工作,我的需要。可能有人可能给我的我会怎样code这样的例子?片段A是通过XML布局设置,并且B片段被载入编程到容器中。我曾尝试实现这一点通过使用碎片中的一个方法来更新文本,并呼吁从父活动的方法,该方法。然后,我呼吁从B片段父活动的方法。
I have two fragments sitting side by side in the same activity. When I touch a button in the right fragment (fragment B), I need a TextView in the left fragment to update (fragment A). I have looked all over for the best way to do this, but nothing seems to work for my needs. Could someone possibly give me an example of how I would code this? Fragment A is set through the XML layout, and fragment B gets loaded programmatically into a container. I have tried accomplishing this by using a method in fragment A to update the text, and calling on that method from a method in the parent activity. I then call on the method in the parent activity from fragment B.
这是在code在B片段声明的接口和调用方法在界面
This is the code in fragment B that declares the interface and calls a method in the interface
AttackCards attackCards;
public interface AttackCards {
public void deckSize();
}
public void onAttach(DeckBuilder deckBuilder) {
super.onAttach(deckBuilder);
attackCards = (AttackCards) deckBuilder;
}
attackCards.deckSize(); //this is in my onclick methods
这是在code中实现接口,并呼吁在A片段的方法,活性
This is the code in the activity that implements the interface and calls the method in fragment A
public class DeckBuilder extends Activity implements AttackCards{
public void deckSize() {
DeckBuilderFragment deckBuilderFragment = (DeckBuilderFragment)getFragmentManager().
findFragmentById(R.id.deckbuilder_fragment);
deckBuilderFragment.deckSize();
}
这是具有附加的共享preferences价值的内容
This is the method in fragment A that appends the textview with the contents of a shared preferences value
public void deckSize() {
deckSize = (TextView) getView().findViewById(R.id.decksize);
final SharedPreferences defaultDeck = getActivity()
.getSharedPreferences("defaultDeck", Context.MODE_PRIVATE);
deckSize.setText(String.valueOf(defaultDeck.getInt("decksize", 0)));
}
可悲的是这种尝试触摸按钮时,只需给我带来一个空指针。我得到一个空指针在
Sadly this attempt simply brings me a nullpointer when touching a button. I am getting a null pointer at
attackCards.deckSize(); //this is in my onclick methods
可能有人请帮助我如何做到这一点的例子是否正确?
Could someone please help me out with an example of how to do this correctly?
推荐答案
您可以定义在接口片段
B,并实现它的MainActivity。然后在回调方法(onClickOnB在这种情况下)设置在的TextView
的文本。你应该获得的TextView
在活动的参考
的的onCreate()
在的setContentView()
。这样做是因为片段
A是静态的。否则,您可以创建在片段的公共方法
A这样你就可以得到片段的引用设置为从回调中的文本
A和调用这样的方法。
You can define an interface in Fragment
B and implement it on the MainActivity. Then on the callback method (onClickOnB in this case) set the text on the TextView
. You should obtain a reference of the TextView
in the Activity
's onCreate()
after setContentView()
. This works because Fragment
A is static. Otherwise, you can create a public method inside Fragment
A so you can set the text from inside the callback by getting a reference of Fragment
A and calling such method.
片段
B
public class FragmentB extends Fragment implements onClickListener{
ClickOnB listener;
public void setOnFragmentBClickListener(ClickOnB listener){
this.listener = listener;
}
@Override
public void onClick(View v){
//stringMessage is a `String` you will pass to `Fragment` A to update its `TextView`
listener.onClickOnB(stringMessage);
}
interface ClickOnB{
public void onClickOnB(String message);
}
}
MainActivity
public class MainActivity extends Activity implements ClickOnB{
@Override
protected onCreate(Bundle savedInstanceState){
//Get a reference of `Fragment` B somewhere in your code after you added it dynamically and set the listener.
((FragmentB)getFragmentManager().findFragmentByTag("FragmentB")).setOnFragmentBClickListener(this);
}
@Override
public void onClickOnB(String message){
//Set the text to the `TextView` here (I am assuming you get a reference of the `TextView` in onCreate() after inflating your layout.
mTextView.setText(message);
}
}
这篇关于在A片段点击碎片B按钮时更新的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!