Search code examples
androidandroid-recyclerviewandroid-edittext

How to get EditText value from Recyclerview to ArrayList?


I have Activity and want to add edittext value to ArrayList useranswer. How I can do it? I reading some information from json file and add it in recyclerview.I also add Edittext in RecyclerView. It working, but i dont known how i can get infomation from edittext.Sorry about my english^^

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>

Solution

  • I find solution.I create setonclicklistener for this button and get data from 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);
                       }
                    }
                }
            });```