Java 如何使用按钮迭代ArrayList来更改Textview的值?

Java 如何使用按钮迭代ArrayList来更改Textview的值?,java,android,android-fragments,arraylist,textview,Java,Android,Android Fragments,Arraylist,Textview,我有一个片段,它使用Filereader将.txt文件读取并解析到片段中,并将该数据存储为一个名为“指令”的定制ArrayList。我想通过使用前进按钮或后退按钮在Arraylist上迭代来显示Textview中Arraylist的每个索引的值。我发现的问题是,我不能简单地通过在onclick方法中增加I的值来增加arraylist的索引。我想知道是否有不同的方法可以采取 下面是代码 public class pneumothorax_fr_flashcard_view extends Frag

我有一个片段,它使用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;
        }

    }
}
公共类气胸\u fr\u flashcard\u视图扩展片段实现view.OnClickListener{
私有静态最终字符串标记=“flashcard视图”;
fileReader=newFileReader();/*这是文件解析器*/
int i=0;
/*建立上下文-这是将文件传递到片段并将其作为arraylist解析所必需的*/
私有上下文;
@凌驾
public void onAttach(@NonNull上下文){
super.onAttach(上下文);
mContext=上下文;
}
@凌驾
公共无效连接(){
super.onDetach();
mContext=null;
}
@可空
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState){
视图=充气机。充气(右侧布局。气胸\u抽认卡\u视图,容器,假);
/*初始化ArrayList并加载已解析的文件*/
ArrayList instructions=reader.loadFile(mContext,“sparthorax_copy.txt”);
/*初始化textview并设置初始字符串*/
TextView flashcardBox=(TextView)view.findViewById(R.id.flashcardBox);
flashcardBox.setText(instructions.get(i.toString());
/*初始化按钮*/
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(此);
backwardButton.setOnClickListener(此);
yesButton.setOnClickListener(这个);
noButton.setOnClickListener(this);
返回视图;
}
@凌驾
公共void onClick(视图v){
开关(v.getId()){
案例R.id.Forward按钮:
Toast.makeText(getActivity(),“已单击下一步按钮”,Toast.LENGTH_SHORT.show();
i++;//我在这里做什么//
打破
案例R.id.backward按钮:
Toast.makeText(getActivity(),“已单击后退按钮”,Toast.LENGTH_SHORT.show();
打破
案例R.id.yesButton:
Toast.makeText(getActivity(),“单击了Yes按钮”,Toast.LENGTH_SHORT.show();
打破
案例R.id.noButton:
Toast.makeText(getActivity(),“未单击任何按钮”,Toast.LENGTH_SHORT.show();
打破
违约:
打破
}
}
}

我将更改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;
}
@Nullable
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState){
视图=充气机。充气(右侧布局。气胸\u抽认卡\u视图,容器,假);
/*初始化ArrayList并加载已解析的文件*/
ArrayList instructions=reader.loadFile(mContext,“sparthorax_copy.txt”);
/*初始化textview并设置初始字符串*/
TextView flashcardBox=(TextView)view.findViewById(R.id.flashcardBox);
flashcardBox.setText(instructions.get(i.toString());
/*初始化按钮*/
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->{
//添加某物以处理指示
flashcardBox.setText(instructions.get(i.toString());
});
backwardButton.setOnClickListener(v->{
我--;

//添加一些东西来处理iplase显示“fileReader”的样子以及fileReader.InstructionList其实并不重要,因为类只是给了你一个arraylist,我遇到的问题主要是考虑一种迭代的方法,而不是迭代不起作用