我更像是iOS开发人员,所以传递java变量似乎是一个挑战。我抬头看过价值和参考,没有一个对我有用。我似乎无法理解为什么。由于我的应用程序解释起来有点复杂,我将其分解为更简单,实际的问题。我的应用程序有一个android微调器,有三个类别。我想要发生的是根据用户选择的内容简单地更改textview上的文本。在我的碎片视图中,我从基础知识开始,通过调用xml文件中的按钮和textview。
public class AskQuestions extends Fragment {
private Button selectAnswer;
private TextView displayAnswer;
private Spinner spinner;
public String result;
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public void onCreate(Bundle savedInstabceState){
super.onCreate(savedInstabceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Fetching layout
View rootView = inflater.inflate(R.layout.fragment_questions, container, false);
selectAnswer = (Button) rootView.findViewById(R.id.button1);
displayAnswer = (TextView) rootView.findViewById(R.id.textView1);
spinner = (Spinner) rootView.findViewById(R.id.planets_spinner);
//Setting spinner to custom class I created
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
//Button clicks when the user selects an item of the spinner, and changes the text
//based on what item is selected.
selectAnswer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//This is where I am confused.
result = getResult();
displayAnswer.setText(result);
}
});
return rootView;
}
}
行。这主要是我的麻烦所在。如何根据用户从此课程中选择的内容更改文本视图?
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
//Calling object in to display in the view
AskQuestions question = new AskQuestions();
//When a user selects an item off of the spinner, the position changes
//accordingly (spinner works just fine, changing the text on the textview from this class
//seems to be most of my trouble.
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
switch (pos) {
case 0:
//This does not work when a user selects an item, and clicks a button to review the result
question.setResult("You selected index zero");
break;
case 1:
//Does not work
question.setResult("You selected index one");
break;
case 2:
//Does not work
question.setResult("You selected index 2");
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:2)
我不是Android开发人员,但您正在这样做:
AskQuestions question = new AskQuestions();
创建用户界面中不存在的新AskQuestions
对象,因此您正在更改未显示的对象。
您需要从AskQuestions
引用接口的CustomOnItemSelectedListener
对象才能修改该UI对象。 @DerGolem的答案向您展示了一种方法; - )
此致
答案 1 :(得分:2)
最简单的选择是将监听器变为匿名类:
public class AskQuestions extends Fragment {
private Button selectAnswer;
private TextView displayAnswer;
private Spinner spinner;
public String result;
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public void onCreate(Bundle savedInstabceState){
super.onCreate(savedInstabceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Fetching layout
View rootView = inflater.inflate(R.layout.fragment_questions, container, false);
selectAnswer = (Button) rootView.findViewById(R.id.button1);
displayAnswer = (TextView) rootView.findViewById(R.id.textView1);
spinner = (Spinner) rootView.findViewById(R.id.planets_spinner);
//Setting spinner to custom class I created
spinner.setOnItemSelectedListener(new OnItemSelectedListener (){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
switch (pos) {
case 0:
displayAnswer.setText("You selected index 0");
break;
case 1:
displayAnswer.setText("You selected index one");
break;
case 2:
displayAnswer.setText("You selected index 2");
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
//Button clicks when the user selects an item of the spinner, and changes the text
//based on what item is selected.
selectAnswer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//This is where I am confused.
result = getResult();
displayAnswer.setText(result);
}
});
return rootView;
}
}