我正在研究一个在android上提出真假问题的项目。在询问时,它会将问题,给定答案和正确答案收集到Results[]
对象数组中。在用户完成问题后ResultsActivity
开始,我很难以下列格式显示结果:
第1行:第一个问题
第2行:正确答案,用户答案
第3行:第二个问题
...
我会附上ResultsActivity
和content_results.xml
。你能帮助我走上正确的道路吗?
public class ResultsActivity extends AppCompatActivity {
private TextView questionText;
private TextView resultText;
private TextView givenText;
private LinearLayout rContent;
private LinearLayout rSecondaryContent;
private void ResultQuestionFiller(int i){
rContent = (LinearLayout) findViewById(R.id.results_content);
questionText = new TextView(this);
ViewGroup.LayoutParams fullWidth = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
questionText.setText(Results.True_False_Results[i].getQuestionResourceId());
rContent.addView(questionText, fullWidth);
}
private void ResultAnswerFiller(int i){
rSecondaryContent = (LinearLayout) findViewById(R.id.results_content_secondary);
resultText = new TextView(this);
ViewGroup.LayoutParams splitWidth = new ViewGroup.LayoutParams(100, ViewGroup.LayoutParams.WRAP_CONTENT);
if (Results.True_False_Results[i].isTFgivenAnswer() == Results.True_False_Results[i].isTFcorrectAnswer()){
resultText.setText(R.string.right_answer);
resultText.setTextColor(Color.parseColor("#009933"));
}
else{
resultText.setText(R.string.false_answer);
resultText.setTextColor(Color.RED);
}
givenText = new TextView(this);
givenText.setText(String.valueOf(Results.True_False_Results[i].isTFcorrectAnswer()));
rSecondaryContent.addView(givenText,splitWidth);
rSecondaryContent.addView(resultText, splitWidth);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
for (int i = 0; i < Results.True_False_Results.length; i++ ){
ResultQuestionFiller(i);
ResultAnswerFiller(i);
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
xml
:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="hu.david.szablyteszt.ResultsActivity"
tools:showIn="@layout/activity_results">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/scrollView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/results_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/results_content_secondary"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
使用该代码,我会得到以下结果:
第1行:所有正确答案,用户排成一行。
第2行:问题1
第3行:问题2
...
答案 0 :(得分:0)
使用ListView或RecyclerView会更有效率。以下是使用RecyclerView实现您想要的代码:
创建一个对象来保存问题,用户回答并更正答案:
public class QuestionAnswerObject {
private String question;
private String userAnswer;
private String correctAnswer;
public QuestionAnswerObject(String question, String userAnswer, String correctAnswer) {
this.question = question;
this.userAnswer = userAnswer;
this.correctAnswer = correctAnswer;
}
public String getQuestion() {
return question;
}
public String getUserAnswer() {
return userAnswer;
}
public String getCorrectAnswer() {
return correctAnswer;
}
}
ActivityMain布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}">
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</RelativeLayout>
RecyclerView项目布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<TextView
android:id="@+id/question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="5dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/userAnswer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="5dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/correctAnswer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="5dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
RecyclerView适配器:
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ObjectHolder>
{
private ArrayList<QuestionAnswerObject> questionAnswerObjects;
public static class ObjectHolder extends RecyclerView.ViewHolder
{
TextView question;
TextView userAnswer;
TextView correctAnswer;
public ObjectHolder(View itemView)
{
super(itemView);
question = (TextView) itemView.findViewById(R.id.question);
userAnswer = (TextView) itemView.findViewById(R.id.userAnswer);
correctAnswer = (TextView) itemView.findViewById(R.id.correctAnswer);
}
}
public MyRecyclerViewAdapter(ArrayList<QuestionAnswerObject> data) {
questionAnswerObjects = data;
}
@Override
public ObjectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item, parent, false);
ObjectHolder dataObjectHolder = new ObjectHolder(view);
return dataObjectHolder;
}
@Override
public void onBindViewHolder(ObjectHolder holder, int position) {
holder.question.setText(questionAnswerObjects.get(position).getQuestion());
holder.userAnswer.setText(questionAnswerObjects.get(position).getUserAnswer());
holder.correctAnswer.setText(questionAnswerObjects.get(position).getCorrectAnswer());
}
@Override
public int getItemCount()
{
return questionAnswerObjects.size();
}
}
MainActivity
public class MainActivity extends Activity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
ArrayList<QuestionAnswerObject> data = new ArrayList<>();
data.add(new QuestionAnswerObject("question1", "userAnswer1", "correctAnswer1"));
data.add(new QuestionAnswerObject("question2", "userAnswer2", "correctAnswer2"));
data.add(new QuestionAnswerObject("question3", "userAnswer3", "correctAnswer3"));
mAdapter = new MyRecyclerViewAdapter(data);
mRecyclerView.setAdapter(mAdapter);
RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(this, LinearLayoutManager.VERTICAL);
mRecyclerView.addItemDecoration(itemDecoration);
}
}