- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以将 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 中的一行:
<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 ,将以下代码放入其中:
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 :
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
如果需要的话,这是一个简单的测试:)
关于安卓 ListView : Different colours in Items and SubItems?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23086943/
这里我试图在 FlatList 中显示一个名为“posts”的数组。 render() { console.log(this.props.posts); return (
这是我的代码: {{day(list)}} {{list.weather[0].description}}
我是 Mahout 的新手,并且仍在使用它。 我的问题是,将 Item-Item 和 User-Item 结合起来是否合适? 我的用例是,一个社交网络应用会尝试根据用户历史数据(优先级较高)为当前用户
下午好, 我按数据库搜索以测试特定类测试,当我放置一个新项目时,如果列表包含该项目。 @Test public void insertAndDeleteTask() throws Interrupte
我有一个关于 ionic 框架的问题,我希望有人能帮助我...我有一个带有“ion-item-right”的 ionic 列表。这一切都可以,按钮在右边。现在我需要其他三个居中的图标,这样我就有了:文
我经常遇到类似下面的代码: if ( items != null) { foreach(T item in items) { //... } } 基本上,if 条件确
我最近问了a question about LocalStorage .使用 JSON.parse(localStorage.item) 和 JSON.parse(localStorage['item
我最近问了a question about LocalStorage .使用 JSON.parse(localStorage.item) 和 JSON.parse(localStorage['item
这个问题已经有答案了: Type mismatch: cannot convert from Item to Item (1 个回答) 已关闭 7 年前。 我很困惑。我无法将外部类的实例变量 Node
我目前正在使用 MUI Grid(但我对替代解决方案持开放态度)并且我想要并排放置两个组件:最右边的组件占 400px宽度和左侧组件占据其余部分。 || || || 当页面宽度缩小时: | | ||
我最近问过a question about LocalStorage 。使用 JSON.parse(localStorage.item) 和 JSON.parse(localStorage['item
public class Document extends Model { ... @ManyToMany public Set accessors; ... } 我想选择访问者包含某个用户的所有文档
我正在使用 selenium webdriver 为单页 Web 应用程序开发一个 Java 框架,使用以下模式:PageObject、SlowLoadableComponent(责任链)、PageF
最近在学习C,在网上发现了一个问题。问题是: What is the problem with this function in terms of memory allocation? What is
我有这个代码 ( -1 ? true : false} /> {genre.item.name}
在ASP.Net中使用DataGrid时真的没有快捷方法吗 (e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.A
我正在使用工作流程根据数据和一组要求将大量 pdf 从一个位置复制到其他大坝位置。我正在使用以下代码 Assets damAsset = manager.createAsset(path, is, m
我是 PowerShell 的新手。 我正在尝试自动将 dll 组件从源服务器上的文件夹部署到目标服务器上的多个文件夹。这看起来应该很简单:将组件从源服务器上的源(部署)文件夹复制到目标服务器上的文件
我的代码: for column_name, column_data in summary_words.iteritems(): if column_name != "summary" and
我的代码: for column_name, column_data in summary_words.iteritems(): if column_name != "summary" and
我是一名优秀的程序员,十分优秀!