[英]How to change the value of a Textview using buttons to iterate through an ArrayList?
I have a fragment that uses a Filereader to read and parse a .txt file into the fragment and store this data as a custom ArrayList called "instructions".我有一个片段,它使用 Filereader 读取 .txt 文件并将其解析到片段中,并将此数据存储为名为“指令”的自定义 ArrayList。 I want to display the value of each index of the Arraylist in a Textview by iterating over the Arraylist using a forward button or backward button.我想通过使用前进按钮或后退按钮遍历 Arraylist 来在 Textview 中显示 Arraylist 的每个索引的值。 The problem I am finding is that I cannot increment the index of the arraylist simply by incrementing the value of i in the onclick method.我发现的问题是我不能简单地通过在 onclick 方法中增加 i 的值来增加 arraylist 的索引。 I was wondering if there is a different approach to be taken.我想知道是否有不同的方法可以采取。
Here is the code below这是下面的代码
public class pneumothorax_fr_flashcard_view extends Fragment implements View.OnClickListener {
private static final String TAG = "flashcard view";
fileReader reader = new fileReader(); /* this is the file parser*/
int i = 0;
/*establishing context - this is needed to pass the file into the fragment and parse it as an arraylist */
private Context mContext;
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
mContext = context;
}
@Override
public void onDetach() {
super.onDetach();
mContext = null;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.pneumothorax_flashcard_view,container,false);
/*initialise the ArrayList and load in the parsed file*/
ArrayList <fileReader.InstructionList> instructions = reader.loadFile(mContext,"Pneumothorax_copy.txt");
/*initialise the textview and set the initial string*/
TextView flashcardBox = (TextView) view.findViewById(R.id.flashcardBox);
flashcardBox.setText(instructions.get(i).toString());
/*initialise buttons*/
ImageButton forwardButton = (ImageButton) view.findViewById(R.id.forwardButton);
ImageButton backwardButton = (ImageButton) view.findViewById(R.id.backwardButton);
ImageButton yesButton = (ImageButton) view.findViewById(R.id.yesButton);
ImageButton noButton = (ImageButton) view.findViewById(R.id.noButton);
forwardButton.setOnClickListener(this);
backwardButton.setOnClickListener(this);
yesButton.setOnClickListener(this);
noButton.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.forwardButton:
Toast.makeText(getActivity(), "Next button has been clicked", Toast.LENGTH_SHORT).show();
i++; //WHAT DO I DO HERE???//
break;
case R.id.backwardButton:
Toast.makeText(getActivity(), "Back button has been clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.yesButton:
Toast.makeText(getActivity(), "Yes button has been clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.noButton:
Toast.makeText(getActivity(), "No button has been clicked", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
I would change the onCreateView method:我会改变 onCreateView 方法:
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.pneumothorax_flashcard_view,container,false);
/*initialise the ArrayList and load in the parsed file*/
ArrayList <fileReader.InstructionList> instructions = reader.loadFile(mContext,"Pneumothorax_copy.txt");
/*initialise the textview and set the initial string*/
TextView flashcardBox = (TextView) view.findViewById(R.id.flashcardBox);
flashcardBox.setText(instructions.get(i).toString());
/*initialise buttons*/
ImageButton forwardButton = (ImageButton) view.findViewById(R.id.forwardButton);
ImageButton backwardButton = (ImageButton) view.findViewById(R.id.backwardButton);
ImageButton yesButton = (ImageButton) view.findViewById(R.id.yesButton);
ImageButton noButton = (ImageButton) view.findViewById(R.id.noButton);
forwardButton.setOnClickListener(v -> {
//add sth to handle i>instructions.length
flashcardBox.setText(instructions.get(i).toString());
});
backwardButton.setOnClickListener(v -> {
i--;
//add sth to handle i<0
flashcardBox.setText(instructions.get(i).toString());
});
yesButton.setOnClickListener(this);
noButton.setOnClickListener(this);
return view;
}
This way you have access to your instructions array.这样您就可以访问您的指令数组。
Another way to achieve what you want would be to add a class variable that holds the instructions ArrayList
but if you only use it to show the value in the TextView
, I would stick to the first solution.实现您想要的另一种方法是添加一个包含指令ArrayList
的类变量,但如果您只使用它来显示TextView
的值,我会坚持第一个解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.