如何在Recycler View中获取TextView并将其发送到另一个 Activity 并将其添加到arraylist值

2019年3月4日 14点热度 0条评论

我无法在RecyclerView中获得像“缺席”,“当前”和“借口”这样的每个Textview,但是TextView只有一个ID。如何获得TextView中的所有RecyclerView。这是我的Activity中的代码。

这是我的应用程序的屏幕截图。

这是我的MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_adapter);
    final TextView dates,blocks;
    blocks=findViewById(R.id.blocks);
    dates=findViewById(R.id.dates);
    database =FirebaseDatabase.getInstance();
    submit=findViewById(R.id.sumbitBrn);
    final String block = getIntent().getExtras().getString("Block");
    final String date = getIntent().getExtras().getString("Date");
    blocks.setText(block);
    dates.setText(date);
    swipeRefreshLayout = findViewById(R.id.refresh);

    exampleList =  new ArrayList<>();
    values = new ArrayList<>();

    myReff = FirebaseDatabase.getInstance().getReference("Section").child(block);
    myReff.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot students:dataSnapshot.getChildren()){
                Students students1 = students.getValue(Students.class);
                exampleList.add(students1);
            }
        }   

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });



    mRecyclerView = findViewById(R.id.recyclerView);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(this);
    mAdapter = new ExampleAdapter(exampleList);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(mAdapter);

    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mRecyclerView = findViewById(R.id.recyclerView);
            mRecyclerView.setHasFixedSize(true);
            mLayoutManager = new LinearLayoutManager(MainAdapter.this);
            mAdapter = new ExampleAdapter(exampleList);
            mRecyclerView.setLayoutManager(mLayoutManager);
            mRecyclerView.setAdapter(mAdapter);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    swipeRefreshLayout.setRefreshing(false);
                }
            },4000);
        }
    });

    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            myReff =database.getReference("Date").child(dates.getText().toString()).child(blocks.getText().toString());

            // I WILL INSERT HERE THE CODE FOR GETTING THE TEXTVIEWS INSIDE THE RECYCLER
            Toast.makeText(MainAdapter.this,"SUCCESS!",Toast.LENGTH_LONG).show();
        }
    });

这是我的适配器类。我在这里使用一个Radio组在
RecyclerView中设置文本,但是我不知道获取每个
TextView

private ArrayList<Students> mExampleList;

public static class ExampleViewHolder extends RecyclerView.ViewHolder{
    public RadioGroup radioGroup;
    public TextView mTextView1;
    public TextView result;
    public ExampleViewHolder(@NonNull View itemView) {
        super(itemView);
        radioGroup = itemView.findViewById(R.id.allRadioButton);
        mTextView1 = itemView.findViewById(R.id.textView);
        result = itemView.findViewById(R.id.res);
    }
}

public ExampleAdapter(ArrayList<Students>exampleList) {
    mExampleList = exampleList;
 }

@NonNull
@Override
public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.items, parent, false);
    ExampleViewHolder evh = new ExampleViewHolder(v);
    return evh;
}

@Override
public void onBindViewHolder(@NonNull final ExampleViewHolder holder, int position) {
    Students currentItem = mExampleList.get(position);

    holder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            switch (i){
                case R.id.present:
                    holder.result.setText("Present");
                              break;
                case R.id.absent:
                    holder.result.setText("Absent");
                    break;
                case R.id.excuse:
                    holder.result.setText("Excuse");
                    break;
            }
        }
    });
    holder.mTextView1.setText(currentItem.getName());
}

@Override
public int getItemCount() {

    return mExampleList.size();
}

解决方案如下:

因此,您可以考虑使用一个数组来跟踪RecyclerView中的数据。在您的适配器中,考虑如下创建一个数组。

private int[] attendance = new int[list.size()]; // The idea is to initialize the array having the same size as your list 
private final int PRESENT = 0;
private final int ABSENT = 1;
private final int EXCUSE = 2;

现在,在检查侦听器中,将适当的值放入数组以指示出勤状态。

holder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i){
            case R.id.present:
                holder.result.setText("Present");
                attendence[position] = PRESENT;
                break;
            case R.id.absent:
                holder.result.setText("Absent");
                attendence[position] = ABSENT;
                break;
            case R.id.excuse:
                holder.result.setText("Excuse");
                attendence[position] = EXCUSE;
                break;
            case default:
                holder.result.setText("Present");
                attendence[position] = PRESENT;
                break;
        }
    }
});

现在,在需要时将数组传递给下一个活动或类。希望有帮助!