我从服务器获取了json数据,其中包含目标类型问题和答案到recyclerview中。 我需要在单击“提交”按钮时更改用户输入答案的颜色。
MainActivity在下面给出
public class MainActivity extends AppCompatActivity {
List<QuestionObjects> arrayList;
RecyclerView recyclerView;
public static List<String> answers = new ArrayList<>();
public static List<RadioButton> buttons = new ArrayList<android.widget.RadioButton>();
QuestionsAdapter myAdapter;
public static Button submitTest;
public static List answersList;
public boolean clicked = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayList = new ArrayList<>();
answersList = new ArrayList();
recyclerView = (RecyclerView) findViewById(R.id.questionsRecyclerview);
submitTest = (Button) findViewById(R.id.submitTest);
submitTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clicked = true;
Toast.makeText(MainActivity.this, myAdapter.getArrayList() + "", Toast.LENGTH_SHORT).show();
}
});
submitTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, myAdapter.getArrayList() + "", Toast.LENGTH_SHORT).show();
for (int i = 0; i < answers.size(); i++) {
arrayList.get(i).setStrAnswer(MainActivity.answers.get(i));
myAdapter = new QuestionsAdapter(getApplicationContext(), arrayList);
recyclerView.setAdapter(myAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), RecyclerView.VERTICAL, true));
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
}
}
});
new backgroundThread().execute("https://api.myjson.com/bins/v4k7l");
}
private class backgroundThread extends AsyncTask<String, String, Boolean> {
private String Content;
private String error = null;
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Loading..please wait");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... strings) {
BufferedReader reader = null;
try {
URL url = new URL(strings[0]);
URLConnection connection = url.openConnection();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
Content = stringBuilder.toString();
JSONObject jsonObject1;
jsonObject1 = new JSONObject(Content);
JSONArray jsonArray = jsonObject1.getJSONArray("test");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject2 = jsonArray.getJSONObject(i);
arrayList.add(new QuestionObjects(jsonObject2.getString("question") + "",
jsonObject2.getString("o1") + "",
jsonObject2.getString("o1") + "",
jsonObject2.getString("o3") + "",
jsonObject2.getString("o4") + "",
""));
answers.add(jsonObject2.getString("A") + "");
}
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
myAdapter = new QuestionsAdapter(getApplicationContext(), arrayList);
recyclerView.setNestedScrollingEnabled(true);
recyclerView.setAdapter(myAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), RecyclerView.VERTICAL, true));
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
dialog.dismiss();
myAdapter.notifyDataSetChanged();
if (result == false) {
Toast.makeText(getApplicationContext(), "unable to retrieve data", Toast.LENGTH_LONG).show();
}
}
}
}
适配器在下面给出
public class QuestionsAdapter extends RecyclerView.Adapter<QuestionsAdapter.ViewHolder> {
public RadioGroup radioGroup;
RadioButton checkedRadioButton;
public Context aContext;
public List<QuestionObjects> questionsList;
public LinkedHashMap<Integer, String> userAnswersList = new LinkedHashMap<>();
public List<String> serverAnswersList = new ArrayList<>();
public QuestionsAdapter(Context aContext, List<QuestionObjects> questionsList) {
this.aContext = aContext;
this.questionsList = questionsList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
return new ViewHolder(mView);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
holder.txtQuestion.setText(questionsList.get(position).getStrQuestion());
serverAnswersList.add(questionsList.get(position).getStrAnswer() + "");
holder.radioOption1.setText(questionsList.get(position).getStrOption1());
holder.radioOption2.setText(questionsList.get(position).getStrOption2());
holder.radioOption3.setText(questionsList.get(position).getStrOption3());
holder.radioOption4.setText(questionsList.get(position).getStrOption4());
holder.txtAnswer.setText(questionsList.get(position).getStrAnswer());
if (userAnswersList.containsValue(position)) {
checkedRadioButton.setTextColor(Color.parseColor("#c4c4c4"));
}
holder.radioOption1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
userAnswersList.put(position, holder.radioOption1.getText().toString() + "");
// resultFunction();
// holder.radioOption1.setTextColor(Color.parseColor("#c4c4c4"));
}
});
holder.radioOption2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
userAnswersList.put(position, holder.radioOption2.getText().toString() + "");
// resultFunction();
}
});
holder.radioOption3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
userAnswersList.put(position, holder.radioOption3.getText().toString() + "");
//Toast.makeText(aContext, serverAnswersList +"", Toast.LENGTH_SHORT).show();
// resultFunction();
}
});
holder.radioOption4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
userAnswersList.put(position, holder.radioOption4.getText().toString() + "");
//resultFunction();
}
});
}
private void resultFunction() {
for (Map.Entry m : userAnswersList.entrySet()) {
if (serverAnswersList.contains(m.getValue()))
// Toast.makeText(aContext, m.getKey()+"", Toast.LENGTH_SHORT).show();
Toast.makeText(aContext, "true", Toast.LENGTH_SHORT).show();
else
Toast.makeText(aContext, "false", Toast.LENGTH_SHORT).show();
}
}
public LinkedHashMap<Integer, String> getArrayList() {
return userAnswersList;
}
@Override
public int getItemCount() {
return questionsList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView txtQuestion, txtAnswer;
public RadioButton radioOption1, radioOption2, radioOption3, radioOption4;
public ViewHolder(@NonNull final View itemView) {
super(itemView);
txtQuestion = (TextView) itemView.findViewById(R.id.questionId);
txtAnswer = (TextView) itemView.findViewById(R.id.answerId);
radioOption1 = (RadioButton) itemView.findViewById(R.id.option1);
radioOption2 = (RadioButton) itemView.findViewById(R.id.option2);
radioOption3 = (RadioButton) itemView.findViewById(R.id.option3);
radioOption4 = (RadioButton) itemView.findViewById(R.id.option4);
radioGroup = (RadioGroup) itemView.findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
checkedRadioButton = (RadioButton) itemView.findViewById(checkedId);
String text = checkedRadioButton.getText().toString();
checkedRadioButton.setTextColor(Color.parseColor("#c4c4c4"));
Toast.makeText(aContext, text, Toast.LENGTH_SHORT).show();
}
});
}
}
}
任何人都可以,请给我一个建议,在单击“提交”按钮时使用更改单选按钮文本的代码进行更改。