【问题标题】:How to get EditText value from Recyclerview to ArrayList?如何从 Recyclerview 获取 EditText 值到 ArrayList?
【发布时间】:2021-12-07 06:42:04
【问题描述】:

我有 Activity 并想将 edittext 值添加到 ArrayList useranswer。我该怎么做?我从 json 文件中读取了一些信息并将其添加到 recyclerview 中。我还在 RecyclerView 中添加了 Edittext。它工作正常,但我不知道如何从edittext获取信息。对不起我的英语^^

TestText.java


import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Objects;

public class TestText extends AppCompatActivity {
    RecyclerView recyclerView;
    ArrayList< String > id = new ArrayList<>();
    ArrayList< String > question = new ArrayList<>();
    ArrayList< String > possible_answer1 = new ArrayList<>();
    ArrayList< String > possible_answer2 = new ArrayList<>();
    ArrayList< String > possible_answer3 = new ArrayList<>();
    ArrayList< String > answer = new ArrayList<>();
    ArrayList<Integer> useranswer = new ArrayList<Integer>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_text);
        recyclerView = findViewById(R.id.recyclerviewtest);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(linearLayoutManager);

        Intent intent = getIntent();
        Context context=this;
        String fName = intent.getStringExtra("name");

        setTitle(fName);
        try {
            JSONObject jsonObject = new JSONObject(JsonDataFromAsset("tests.json"));
            JSONArray jsonArray = jsonObject.getJSONArray(fName);
            for (int i=0;i<= jsonArray.length();i++){
                JSONObject userData=jsonArray.getJSONObject(i);
                id.add(userData.getString("id"));
                question.add(userData.getString("question"));
                possible_answer1.add(userData.getString("possible_answer1"));
                possible_answer2.add(userData.getString("possible_answer2"));
                possible_answer3.add(userData.getString("possible_answer3"));
                answer.add(userData.getString("answer"));

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        HelperAdapterForTest helperAdapter = new HelperAdapterForTest(id,question,possible_answer1,possible_answer2,possible_answer3,answer,TestText.this);
        recyclerView.setAdapter(helperAdapter);
        Button but=findViewById(R.id.verify_button);
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Get edittext value in this place

            }
        });
    }

    public void GetThissheet(View view){

    }
    private String JsonDataFromAsset(String fileName) {
        String json = null;
        try {
            InputStream inputStream = getAssets().open(fileName);
            int sizeOfFile = inputStream.available();
            byte[] bufferData = new byte[sizeOfFile];
            inputStream.read(bufferData);
            inputStream.close();
            json = new String(bufferData, "UTF-8");
        }catch (IOException e){
            e.printStackTrace();
            return null;
        }
        return json;
    }
}

HelperAdapterForTest.java


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class HelperAdapterForTest extends RecyclerView.Adapter<HelperAdapterForTest.MyViewClass>
{

    ArrayList< String > id;
    ArrayList< String > question;
    ArrayList< String > possible_answer1;
    ArrayList< String > possible_answer2;
    ArrayList< String > possible_answer3;
    ArrayList< String > answer;
    Context context;

    public HelperAdapterForTest(ArrayList< String > id,ArrayList< String >  question, ArrayList< String > possible_answer1,ArrayList< String > possible_answer2,ArrayList< String > possible_answer3,ArrayList< String > answer,Context context) {


        this.id = id;
        this.question = question;
        this.possible_answer1 = possible_answer1;
        this.possible_answer2 = possible_answer2;
        this.possible_answer3 = possible_answer3;
        this.answer=answer;
        this.context = context;

    }


    @NonNull
    @Override
    public HelperAdapterForTest.MyViewClass onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.test,parent,false);
        HelperAdapterForTest.MyViewClass myViewClass=new HelperAdapterForTest.MyViewClass(view);
        return myViewClass;
    }

    @Override
    public void onBindViewHolder(@NonNull HelperAdapterForTest.MyViewClass holder, @SuppressLint("RecyclerView") int position) {
        holder.id.setText(id.get(position));
        holder.question.setText(question.get(position));
        holder.possible_answer1.setText(possible_answer1.get(position));
        holder.possible_answer2.setText(possible_answer2.get(position));
        holder.possible_answer3.setText(possible_answer3.get(position));

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Intent intent = new Intent(context, TestText.class);
                //intent.putExtra("name",question.get(position).toString());
                //v.getContext().startActivity(intent);
                //Toast.makeText(context,"Клик",Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return id.size();
    }


    public class MyViewClass extends RecyclerView.ViewHolder{
        TextView id;
        TextView question;
        TextView possible_answer1;
        TextView possible_answer2;
        TextView possible_answer3;
        EditText answer;
        public MyViewClass(@NonNull View itemView) {
            super(itemView);
            id = itemView.findViewById(R.id.questionid);
            question=itemView.findViewById(R.id.question);
            answer=itemView.findViewById(R.id.answer);
            possible_answer1=itemView.findViewById(R.id.posibleanswer1);
            possible_answer2=itemView.findViewById(R.id.posibleanswer2);
            possible_answer3=itemView.findViewById(R.id.posibleanswer3);



        }
    }
}

activity_test_text.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TestText"
    android:orientation="vertical"
    >


    <ScrollView
        android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
        android:fillViewport="true"
        android:layout_weight="1">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerviewtest"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

         />

    </ScrollView>

    <Button
        android:id="@+id/verify_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"

        android:gravity="center_horizontal">

    </Button>



</LinearLayout>

【问题讨论】:

    标签: android android-recyclerview android-edittext


    【解决方案1】:

    您需要添加一个按钮来确认视图中的操作

     public class MyViewClass extends RecyclerView.ViewHolder{
        TextView id;
        .
        .
        EditText answer;
        Button getAnswer;
        public MyViewClass(@NonNull View itemView) {
            super(itemView);
            id = itemView.findViewById(R.id.questionid);
            .
            .
            getAnswer=itemView.findViewById(R.id.getAnswer);
    
    
    
    
        }
    }
    

    并在 onBindViewHolder 中添加这个

    holder.getAnswer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            answer.add(answer.getText().toString());
        }
    });
    

    现在答案列表将包含从编辑文本中获得的答案列表

    【讨论】:

    • 感谢您的解决方案,但当我按下按钮时,我需要从 Activity 中的 Edittext 读取值
    【解决方案2】:

    addTextChangedListener 用于您的editText,以便您可以观察更改并添加到您的arrayList,

    如果你想将相同的值传递给Activity,那么你可以创建接口类并编写一个你可以在Activity中实现的方法,或者你可以使用eventBus来传递值。

    【讨论】:

      【解决方案3】:

      我找到了解决方案。我为此按钮创建了 setonclicklistener 并从 edittext 获取数据:)

              but.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      for (int i=0;i<recyclerView.getAdapter().getItemCount();i++){
                          v=recyclerView.getChildAt(i);
                          EditText tutu=(EditText) v.findViewById(R.id.answer);
                          useranswer.add(i,tutu.getText().toString());
      
                      }
                      for (int i=0;i<useranswer.size();i++){
                          //Toast.makeText(TestText.this, useranswer.get(i)+","+answer.get(i), Toast.LENGTH_SHORT).show();
                         if (useranswer.get(i).equals(answer.get(i))){
                              v=recyclerView.getChildAt(i);
                              CardView card=v.findViewById(R.id.cardtest);
                              card.setCardBackgroundColor(Color.GREEN);
      
                          }else{
                             v=recyclerView.getChildAt(i);
                             CardView card=v.findViewById(R.id.cardtest);
                             card.setCardBackgroundColor(Color.RED);
                         }
                      }
                  }
              });```
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-31
        • 1970-01-01
        • 2020-09-14
        • 2021-03-12
        • 2020-11-24
        • 2016-08-23
        • 2014-01-15
        相关资源
        最近更新 更多