【发布时间】:2021-01-27 00:40:00
【问题描述】:
我有一个片段,它使用 Filereader 读取 .txt 文件并将其解析到片段中,并将此数据存储为称为“指令”的自定义 ArrayList。我想通过使用前进按钮或后退按钮遍历 Arraylist 来在 Textview 中显示 Arraylist 的每个索引的值。我发现的问题是我不能简单地通过增加 onclick 方法中 i 的值来增加 arraylist 的索引。我想知道是否可以采取不同的方法。
下面是代码
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;
}
}
}
【问题讨论】:
-
请展示“fileReader”的外观以及fileReader.InstructionList
-
没关系,因为这个类只是给你一个arraylist,我遇到的问题主要是在考虑一种迭代的方法,而不是迭代不起作用跨度>
标签: java android android-fragments arraylist textview