提问人:Marvin Liban 提问时间:11/14/2023 更新时间:11/14/2023 访问量:18
我的回收是 Bugging It 当项目数据未显示在屏幕上时无法更改 仅当按钮单击时在屏幕上显示的项目会更改 [关闭]
My Recycleis Bugging It Cannot change the item data when it didnt show in the screen only item showing in the screen changing when button click [closed]
问:
This is my Adapter
软件包 com.csucat.reviewer.Adapter;
导入 android.content.Context;
导入 android.view.LayoutInflater;
导入android.view.View;
导入 android.view.ViewGroup;
导入 android.widget.Button;
导入 android.widget.EditText;
导入 android.widget.ImageView;
导入 android.widget.TextView;
导入 android.widget.Toast;
导入 androidx.annotation.NonNull;
导入androidx.recyclerview.widget.RecyclerView;
导入 com.csucat.reviewer.R;
导入 com.csucat.reviewer.datalist;
导入 com.google.android.material.textfield.TextInputEditText;
导入 java.util.ArrayList;
公共类 rvAdapter 扩展了 RecyclerView.Adapter<rvAdapter.MyViewHolder> {
public rvAdapter(ArrayList datalists, Context context) {
this.datalists = datalists;
this.context = context;
}
ArrayList 数据列表;
上下文上下文;
@NonNull
@Override
公共 rvAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup 父级,int viewType) {
View itemview =
LayoutInflater.from(parent.getContext()).inflate(R.layout.item_questionaire, parent, false);
return new MyViewHolder(itemview);
}
@Override
public void onBindViewHolder(@NonNull rvAdapter.MyViewHolder holder, int position) {
datalist i = datalists.get(position);
holder.Question.setText(i.getQuestion());
holder.A.setText("A: " + i.getChoiceA());
holder.B.setText("B: " + i.getChoiceB());
holder.C.setText("C: " + i.getChoiceC());
holder.D.setText("D: " + i.getChoiceD());
holder.etAnswer.setText(i.getUserInput());
}
@Override
公共 int getItemCount() {
return datalists.size();
}
公共类 MyViewHolder 扩展 RecyclerView.ViewHolder {
private TextView Question;
private TextView A;
private TextView B;
private TextView C;
private TextView D;
private TextView tvIsCorrect;
private TextInputEditText etAnswer;
public MyViewHolder(@NonNull View view) {
super(view);
Question = view.findViewById(R.id.tvQuestion);
A = view.findViewById(R.id.tvA);
B = view.findViewById(R.id.tvB);
C = view.findViewById(R.id.tvC);
D = view.findViewById(R.id.tvD);
etAnswer = view.findViewById(R.id.etAnswer);
tvIsCorrect = view.findViewById(R.id.trueans);
}
public TextInputEditText getEtAnswer() {
return this.etAnswer;
}
public TextView gettvIsCorrect() {
return this.tvIsCorrect;
}
public void setEtAnswer(TextInputEditText etAnswer) {
this.etAnswer = etAnswer;
}
}
}
This is My ReviewerActivity
软件包 com.csucat.reviewer.Activities;
导入android.graphics.Bitmap;
导入 android.text.TextUtils;
导入 com.csucat.reviewer.Adapter.rvAdapter;
导入 android.os.Bundle;
导入 android.util.Log;
导入 android.widget.Button;
导入 android.widget.EditText;
导入 android.widget.TextView;
导入 android.widget.Toast;
导入android.view.View;
导入 android.graphics.Color;
导入androidx.appcompat.app;
导入androidx.recyclerview.widget.DefaultItemAnimator;
导入androidx.recyclerview.widget.LinearLayoutManager;
导入androidx.recyclerview.widget.RecyclerView;
导入 com.csucat.reviewer.Adapter.rvAdapter;
导入 com.csucat.reviewer.R;
导入 com.csucat.reviewer.datalist;
导入 org.json.JSONArray;
导入org.json.JSONObject;
导入 java.io.BufferedReader;
导入java.io.File;
导入 java.io.FileInputStream;
导入 java.io.InputStream;
导入 java.io.InputStreamReader;
导入 java.io.Reader;
导入 java.lang.reflect.Array;
导入 java.util.ArrayList;
public class ReviewerActivity extends AppCompatActivity {
private Button submit;
private RecyclerView rv;
private rvAdapter rvAdapter;
private ArrayList<datalist> datalists;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_reviewer);
setRecyCler();
submit = findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Iterate through your RecyclerView items and perform the logic
checkAllAnswers();
}
});
}
private void importData() {
datalists = new ArrayList<>();
try {
for (int i = 0; i < getdataquiz().length(); i++) {
JSONObject obj = getdataquiz().getJSONObject(i);
String question = obj.getString("Question");
String ChoiceA = obj.getString("a");
String ChoiceB = obj.getString("b");
String ChoiceC = obj.getString("c");
String ChoiceD = obj.getString("d");
String Answer = obj.getString("answer");
datalists.add(new datalist(question, ChoiceA, ChoiceB, ChoiceC, ChoiceD, Answer));
}
} catch (Exception e) {
// Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
private void setRecyCler(){
importData();
rv = findViewById(R.id.rv);
rvAdapter = new rvAdapter(datalists, this);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
rv.setLayoutManager(layoutManager);
rv.setAdapter(rvAdapter);
rvAdapter.notifyDataSetChanged();
}
protected String readStream(InputStream in)
{
StringBuilder sb = new StringBuilder();
try {
Reader reader = new BufferedReader(new InputStreamReader(in));
char[] buff = new char[1024];
while (true) {
int read = reader.read(buff, 0, buff.length);
if (read <= 0) {
break;
}
sb.append(buff, 0, read);
}
} catch (Exception e) {
}
return sb.toString();
}
protected JSONObject getJSONObject()
{
try {
InputStream inputStream = this.getAssets().open("data.json");
String json = readStream(inputStream).trim();
return new JSONObject(json);
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
return null;
}
public JSONArray getdataquiz() {
try {
if (getJSONObject() != null) {
JSONArray array = getJSONObject().getJSONArray("Data");
return array;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
私有 void checkAllAnswers() {
for (int i = 0; i < rvAdapter.getItemCount(); i++) {
rvAdapter.MyViewHolder holder = (rvAdapter.MyViewHolder) rv.findViewHolderForAdapterPosition(i);
if (holder != null) {
datalist item = datalists.get(i);
String userAnswer = holder.getEtAnswer().getText().toString();
TextView truans = holder.gettvIsCorrect();
if (TextUtils.isEmpty(userAnswer)) {
showToast("Fill up all fields");
truans.setVisibility(View.GONE);
} else {
truans.setVisibility(View.VISIBLE);
if (userAnswer.equalsIgnoreCase(item.getAnswer())) {
// Correct answer
truans.setTextColor(Color.GREEN);
truans.setText("Correct");
} else {
// Wrong answer
truans.setTextColor(Color.RED);
truans.setText("Wrong");
}
}
}
}
}
private void showToast(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
}
答: 暂无答案
评论