我有活动,并希望添加edittext值ArrayList useranswer。我该怎么做呢?我从json文件中读取一些信息,并将其添加到recyclerview中。我还在RecyclerView中添加了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>
你需要在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
,那么你可以创建接口类并编写一个方法,你可以在Activity中实现,或者你可以使用eventBus
传递值。
我找到解决办法了。我为这个按钮创建了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);
}
}
}
});```