Android 根据用户输入向ListView中的项目结尾添加字符
在我的应用程序乘法游戏中,我有一个活动,显示用户对问题的答案。 如果给出的答案正确,则显示为绿色。如果答案错误,则在子项中以红色显示,正确答案以绿色显示 如果答案是错误的,我想在答案后面加一个x符号,如果答案是正确的,我想在答案后面加一个勾号。在项目中 当前活动的图像: 活动的代码:Android 根据用户输入向ListView中的项目结尾添加字符,android,xml,listview,adapter,Android,Xml,Listview,Adapter,在我的应用程序乘法游戏中,我有一个活动,显示用户对问题的答案。 如果给出的答案正确,则显示为绿色。如果答案错误,则在子项中以红色显示,正确答案以绿色显示 如果答案是错误的,我想在答案后面加一个x符号,如果答案是正确的,我想在答案后面加一个勾号。在项目中 当前活动的图像: 活动的代码: public class RandomTestResults extends Activity { // Set up variables TextView scoreText; Stri
public class RandomTestResults extends Activity {
// Set up variables
TextView scoreText;
String phrase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testresults);
// List view to hold the test results
ListView itemList = (ListView) findViewById(R.id.lvRandomTestresults);
// getting data from the previous activity via intents
int[] results = getIntent().getIntArrayExtra("results");
String[] questions = getIntent().getStringArrayExtra("Questions");
int[] correctAnswer = getIntent().getIntArrayExtra("CorrectAnswer");
int score = getIntent().getIntExtra("score", 0);
// if else statements to determine what phrase to present to user
if (score <= 4) {
phrase = "Please try again! ";
} else if (score <= 6) {
phrase = "Good effort! ";
} else if (score <= 9) {
phrase = "Well done! ";
} else if (score == 10) {
phrase = "Perfect! ";
}
// Set text to tell user what they scored in test
scoreText = (TextView) findViewById(R.id.tvRandomTestresults);
scoreText.setText(phrase + "You got " + score + " correct out of 10!");
// ArrayList containing Hashmap
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
// loop to give list view (10 items and sub items)
for (int i = 1; i <= 10; i++) {
// set userAnswer equal to correct place in results array
int userAnswer = results[i - 1];
// Setting expected answer to correct place in correctAnswer array
int expectedAnswer = correctAnswer[i - 1];
// Set string to present to user
String userString = questions[i - 1] + " " + userAnswer; // mybe
// change
// correct answer
String expectedString = "" + expectedAnswer;
// HashMap containing 2 strings
HashMap<String, String> map = new HashMap<String, String>();
// add strings to HashMap
map.put("user", userString);
map.put("expected", expectedString);
// add HashMap to list
list.add(map);
}
// Instantiate custom array adapter
MyArrayAdapter adapter = new MyArrayAdapter(
this.getApplicationContext(), R.layout.list_row, list,
questions, results);
// Set custom adapter to your ListView.
itemList.setAdapter(adapter);
}
/**
* Method that ensures user is returned
* to main menu when they press back button
*/
@Override
public void onBackPressed() {
Intent t = new Intent(this, Menu.class);
startActivity(t);
}
}
来自适配器的代码:
public class MyArrayAdapter extends
ArrayAdapter<ArrayList<HashMap<String, String>>> {
Context mContext;
ArrayList<HashMap<String, String>> mQuestionArrayList;
int mLayoutResourceId;
String[] mQuestionsArray;
int[] mUsersAnswers;
/**
* Constructor with arguments
* @param context
* @param layoutResourceId
* @param questionsArrayList
* @param questionsArray
* @param usersAnswers
*/
public MyArrayAdapter(Context context, int layoutResourceId,
ArrayList<HashMap<String, String>> questionsArrayList,
String[] questionsArray, int[] usersAnswers) {
super(context, layoutResourceId);
mContext = context;
this.mQuestionArrayList = questionsArrayList;
this.mLayoutResourceId = layoutResourceId;
this.mQuestionsArray = questionsArray;
this.mUsersAnswers = usersAnswers;
}
/**
* Method that returns the size
* of the ArrayList
*/
@Override
public int getCount() {
return mQuestionArrayList.size();
}
/**
*
* Method that will get the view to display to user
*/
@Override
public View getView(int position, View row, ViewGroup parent) {
HashMap<String, String> question = mQuestionArrayList.get(position);
//set layout inflater equal to context
LayoutInflater inflater = LayoutInflater.from(mContext);
// Initialize the row layout by inflating the xml file list_row.
row = inflater.inflate(this.mLayoutResourceId, parent, false);
// Initialize TextViews defined in the list_row layout.
TextView questionTxtView = (TextView) row.findViewById(R.id.question);
TextView answerTxtView = (TextView) row.findViewById(R.id.answer);
TextView correctAnswerTxtView = (TextView) row
.findViewById(R.id.correct);
// Set text for each TextView
questionTxtView.setText(mQuestionsArray[position]);
answerTxtView.setText(String.valueOf(mUsersAnswers[position]));
correctAnswerTxtView.setText(question.get("expected").toString());
// Setting colour of the user answer dependent on if its correct
if (mUsersAnswers[position] != Integer.parseInt(question
.get("expected").toString())){
answerTxtView.setTextColor(Color.RED);
correctAnswerTxtView.setVisibility(View.VISIBLE);
}
else{
answerTxtView.setTextColor(Color.GREEN);
correctAnswerTxtView.setVisibility(View.GONE);
}
return row;
}
}
两个对应的XML:
测试结果:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvRandomTestresults"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<ListView
android:id="@+id/lvRandomTestresults"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
列表行:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/list_row.xml"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/questionLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:paddingLeft="10dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"/>
<TextView
android:id="@+id/answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:paddingLeft="10dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"/>
</LinearLayout>
<TextView
android:id="@+id/correct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:paddingLeft="10dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"/>
</LinearLayout>
添加图像视图:
适配器中的getView:
@Override
public View getView(int position, View row, ViewGroup parent) {
HashMap<String, String> question = mQuestionArrayList.get(position);
//set layout inflater equal to context
LayoutInflater inflater = LayoutInflater.from(mContext);
// Initialize the row layout by inflating the xml file list_row.
row = inflater.inflate(this.mLayoutResourceId, parent, false);
// Initialize TextViews defined in the list_row layout.
TextView questionTxtView = (TextView) row.findViewById(R.id.question);
TextView answerTxtView = (TextView) row.findViewById(R.id.answer);
ImageView imgResult = (ImageView) row.findViewById(R.id.imgResult);
TextView correctAnswerTxtView = (TextView) row
.findViewById(R.id.correct);
// Set text for each TextView
questionTxtView.setText(mQuestionsArray[position]);
answerTxtView.setText(String.valueOf(mUsersAnswers[position]));
correctAnswerTxtView.setText(question.get("expected").toString());
// Setting colour of the user answer dependent on if its correct
if (mUsersAnswers[position] != Integer.parseInt(question
.get("expected").toString())){
answerTxtView.setTextColor(Color.RED);
correctAnswerTxtView.setVisibility(View.VISIBLE);
imgResult.setImageResource(R.drawable.ic_incorrect);
}
else{
answerTxtView.setTextColor(Color.GREEN);
correctAnswerTxtView.setVisibility(View.GONE);
imgResult.setImageResource(R.drawable.ic_correct);
}
return row;
}
在自定义列表的输入字段后添加ImageView,然后显示复选标记或X。在用户输入任何内容之前,将其留空。我是否需要if语句来决定根据用户答案显示哪个图像?我是否需要if语句来决定根据用户答案显示哪个图像?是,在java代码中,您需要一个if语句。如果答案正确,则将imgResult.setImageResourceR.drawable.ic_改为正确;else imgResult.setImageResourceR.drawable.ic_不正确;谢谢,我能把这个if语句放在我的代码中吗?还有,有没有办法使图像足够小,以确保它们在列表视图的每个项目中都显示出来?谢谢,我怎样才能确保图像足够小,以显示在每个列表视图中?
@Override
public View getView(int position, View row, ViewGroup parent) {
HashMap<String, String> question = mQuestionArrayList.get(position);
//set layout inflater equal to context
LayoutInflater inflater = LayoutInflater.from(mContext);
// Initialize the row layout by inflating the xml file list_row.
row = inflater.inflate(this.mLayoutResourceId, parent, false);
// Initialize TextViews defined in the list_row layout.
TextView questionTxtView = (TextView) row.findViewById(R.id.question);
TextView answerTxtView = (TextView) row.findViewById(R.id.answer);
ImageView imgResult = (ImageView) row.findViewById(R.id.imgResult);
TextView correctAnswerTxtView = (TextView) row
.findViewById(R.id.correct);
// Set text for each TextView
questionTxtView.setText(mQuestionsArray[position]);
answerTxtView.setText(String.valueOf(mUsersAnswers[position]));
correctAnswerTxtView.setText(question.get("expected").toString());
// Setting colour of the user answer dependent on if its correct
if (mUsersAnswers[position] != Integer.parseInt(question
.get("expected").toString())){
answerTxtView.setTextColor(Color.RED);
correctAnswerTxtView.setVisibility(View.VISIBLE);
imgResult.setImageResource(R.drawable.ic_incorrect);
}
else{
answerTxtView.setTextColor(Color.GREEN);
correctAnswerTxtView.setVisibility(View.GONE);
imgResult.setImageResource(R.drawable.ic_correct);
}
return row;
}