【问题标题】:How to change the value of a Textview using buttons to iterate through an ArrayList?如何使用按钮更改 Textview 的值以遍历 ArrayList?
【发布时间】: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


【解决方案1】:

我会更改 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;
}

这样你就可以访问你的指令数组了。

实现您想要的另一种方法是添加一个包含指令ArrayList 的类变量,但如果您只使用它来显示TextView 中的值,我会坚持第一个解决方案。

【讨论】:

    猜你喜欢
    • 2019-04-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多