如何从 Recyclerview 获取 EditText 值到 ArrayList?
How to get EditText value from Recyclerview to ArrayList?
我有 Activity 并且想将 edittext 值添加到 ArrayList useranswer。我该怎么做?我从 json 文件中读取了一些信息并将其添加到 recyclerview.I 中,还在 RecyclerView 中添加了 Edittext。它有效,但我不知道如何从 edittext.Sorry 获得关于我的英语的信息^^
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>
您需要在 viewholder 中添加一个按钮来确认操作
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());
}
});
现在答案列表将包含从编辑文本中获得的答案列表
将 addTextChangedListener
用于您的 editText
以便您可以观察变化并添加到您的 arrayList,
如果你想将相同的值传递给 Activity
那么你可以创建接口 class 并编写一个你可以在 Activity 中实现的方法或者你可以使用 eventBus
传值。
我发现 solution.I 为此按钮创建 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);
}
}
}
});```
我有 Activity 并且想将 edittext 值添加到 ArrayList useranswer。我该怎么做?我从 json 文件中读取了一些信息并将其添加到 recyclerview.I 中,还在 RecyclerView 中添加了 Edittext。它有效,但我不知道如何从 edittext.Sorry 获得关于我的英语的信息^^
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>
您需要在 viewholder 中添加一个按钮来确认操作
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());
}
});
现在答案列表将包含从编辑文本中获得的答案列表
将 addTextChangedListener
用于您的 editText
以便您可以观察变化并添加到您的 arrayList,
如果你想将相同的值传递给 Activity
那么你可以创建接口 class 并编写一个你可以在 Activity 中实现的方法或者你可以使用 eventBus
传值。
我发现 solution.I 为此按钮创建 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);
}
}
}
});```