gpt4 book ai didi

安卓 ListView : Different colours in Items and SubItems?

转载 作者:行者123 更新时间:2023-11-30 02:57:10 27 4
gpt4 key购买 nike

是否可以将 ListView 中的项目显示为一种颜色,而将其子项目显示为另一种颜色?

我正在创建一个游戏(乘法表问答游戏),其中用户给出的问题和答案以及正确答案都被转移到 ListView 中。例如:

项目-“7 x 5=(用户答案)”子项-“(正确答案)”

我希望问题即“7x5”为黑色,如果正确则用户回答为绿色,如果不正确则为红色。还有永远是绿色的正确答案

我希望更改颜色的 Activity :

public class TestResults extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.testresults);

ListView itemList = (ListView) findViewById(R.id.lvRandomTestresults);

//gets all necessary data from the previous activity
int[] results = getIntent().getIntArrayExtra("results");
String[] questions= getIntent().getStringArrayExtra("Questions");
int[] correctAnswer= getIntent().getIntArrayExtra("CorrectAnswer");

ArrayList < HashMap <String, String> > list = new ArrayList < HashMap <String, String> > ();


// loop to give list view
for (int i = 1; i <= 10; ++i) {

int userAnswer = results[i - 1];

int expectedAnswer = correctAnswer[i-1];

String userString = questions[i-1] + userAnswer;

String expectedString = " " + expectedAnswer;

HashMap<String, String> map = new HashMap<String, String>();

map.put("user", userString);
map.put("expected", expectedString);

list.add(map);
}

String[] keys = {"user", "expected"};

int[] ids = {android.R.id.text1, android.R.id.text2};

SimpleAdapter adapter = new SimpleAdapter(this, list, android.R.layout.simple_list_item_2, keys, ids);

itemList.setAdapter(adapter);

}//onCreate end


}//class end

此 Activity 对应的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" >

<ListView
android:id="@+id/lvRandomTestresults"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

</ListView>


</LinearLayout>

最佳答案

在您提供的代码中,您使用了一个默认适配器,它实际上根本不可定制,您必须创建自己的适配器,所以让我们开始吧:

首先你必须添加一个 xml 文件到你的布局文件夹代表 listView 中的一行:

行.xml

<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/questionTV"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/answerTV"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

然后你必须创建你的 costum Adapter,所以创建新类并将其命名为 MyAdapter ,将以下代码放入其中:

我的适配器.java

public class MyAdapter extends ArrayAdapter<String> {

private final Activity context;
private final String[] questions;
private final int[] answers;
private final int[] rightAnswers;


static class ViewHolder {
public TextView text1;
public TextView text2;
}

public MyAdapter(Activity context, String[] questions, int[] answers, int[] rightAnswers){
super(context, R.layout.row, questions);

this.context = context;
this.questions= questions;
this.answers= answers;
this.rightAnswers= rightAnswers;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;

if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.row, null);

// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.text1 = (TextView) rowView.findViewById(R.id.questionTV);
viewHolder.text2 = (TextView) rowView.findViewById(R.id.answerTV);

rowView.setTag(viewHolder);
}

// fill data
ViewHolder holder = (ViewHolder) rowView.getTag();

String question = questions[position];
int answer = answers[position];
int rightAnswer = rightAnswers[position];

//set Question in the text View with a BLACK backgroud color
holder.text1.setText(question);
//holder.text1.setBackgroundColor(Color.BLACK);

//set User Answer with GREEN backgroun if it's correct or RED if it s not
holder.text2.setText(String.valueOf(answer));
if(answer == rightAnswer){
holder.text2.setBackgroundColor(Color.GREEN);
}else {
holder.text2.setBackgroundColor(Color.RED);
}

return rowView;
}
}

最后你需要用它来定制你的 Activity :

测试结果.java

public class TestResults extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.testresults);

ListView itemList = (ListView) findViewById(R.id.lvRandomTestresults);

//gets all necessary data from the previous activity
int[] results = getIntent().getIntArrayExtra("results");
String[] questions= getIntent().getStringArrayExtra("Questions");
int[] correctAnswer= getIntent().getIntArrayExtra("CorrectAnswer");

itemList.setAdapter(new MyAdapter(this, questions, results, correctAnswer));

}//onCreate end


}//class end

如果需要的话,这是一个简单的测试:) enter image description here

关于安卓 ListView : Different colours in Items and SubItems?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23086943/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com