Android listview:项目和子项目中的不同颜色?

Android listview:项目和子项目中的不同颜色?,android,android-listview,Android,Android Listview,是否可以将列表视图中的项目作为一种颜色,将其子项目作为不同的颜色 我正在创建一个游戏(times tables问答游戏),在这个游戏中,用户给出的问题和答案以及正确的答案都被传递到一个列表视图中。e、 g: 项目-“7 x 5=(用户答案)” 分项-“(正确答案)” 我希望问题“7x5”为黑色,如果正确,用户答案为绿色,如果不正确,则为红色。而且正确的答案总是绿色的 我希望更改颜色的活动: public class TestResults extends Activity { @Ove

是否可以将列表视图中的项目作为一种颜色,将其子项目作为不同的颜色

我正在创建一个游戏(times tables问答游戏),在这个游戏中,用户给出的问题和答案以及正确的答案都被传递到一个列表视图中。e、 g:

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

我希望问题“7x5”为黑色,如果正确,用户答案为绿色,如果不正确,则为红色。而且正确的答案总是绿色的

我希望更改颜色的活动:

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 
公共类测试结果扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.testresults);
ListView itemList=(ListView)findViewById(R.id.lvRandomTestresults);
//从上一个活动中获取所有必要的数据
int[]results=getIntent().getIntArrayExtra(“结果”);
字符串[]问题=getIntent().getStringArrayExtra(“问题”);
int[]correctAnswer=getIntent().getIntArrayExtra(“correctAnswer”);
ArrayListlist=新的ArrayList();
//循环以提供列表视图

对于(int i=1;i创建一个名为
list_row.xml
的新布局文件,作为列表行的布局。每行都有此布局,每行有两个文本视图

list_row.xml
我用虚构的值测试了代码,下面的结果是从这个答案中编写的代码中检索出来的。


如果您有更多疑问,请告诉我。


希望这对您有所帮助!

在您给出的代码中,您使用了一个默认适配器。它实际上根本不可使用,您必须创建自己的适配器,所以让我们这样做:

首先,您必须向布局文件夹中添加一个xml文件,表示listView中的一行:

row.xml
如果需要,这是一个简单的测试:)

我知道你的意思,但是我的代码很不同,所以很难关联,我已经在最新编辑中发布了我的代码,谢谢!@RNI2013我已经更新了我的答案以适应你的问题。你仍然可以发送一个带有HashMap的ArrayList,并在MyArrayAdapter中检索它的值。希望这对你有所帮助。谢谢,不管我怎么努力实现的目的是得到问题,即“7x5=”全部为黑色,然后用户回答为绿色/红色取决于其是否正确,然后下面的正确答案始终为绿色。这仍然可能吗?@RNI2013确实是。这非常有效,非常感谢您的帮助和回答我的许多问题!Lol您不应该为此创建新答案。相反,您应该编辑您的前男友还有,你的xml布局有一些错误,没有
width
height
选项,它被称为
layout\u width
layout\u height
@rogcg还有别的吗?!!你的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 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>
public class MyArrayAdapter extends ArrayAdapter<ArrayList<HashMap<String, String>>>
{
    Context mContext;
    ArrayList<HashMap<String, String>> mQuestionArrayList;
    int mLayoutResourceId;
    String[] mQuestionsArray;
    int[] mUsersAnswers;

    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;
    }

    @Override
    public int getCount()
    {
        return mQuestionArrayList.size();
    }

    @Override
    public View getView(int position, View row, ViewGroup parent)
    {
        HashMap<String, String> question = mQuestionArrayList.get(position);

        LayoutInflater inflater = LayoutInflater.from(mContext);

        // Here you will initialize the row layout, by inflating the xml file list_row.
        row = inflater.inflate(this.mLayoutResourceId, parent, false);

        // Here we initialize those two 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);

        questionTxtView.setText(mQuestionsArray[position]);
        answerTxtView.setText(String.valueOf(mUsersAnswers[position]));
        correctAnswerTxtView.setText(question.get("expected").toString());

        // This is just a pseudo code to show you when and how to set the colors of
        // the TextView programatically.
        if(mUsersAnswers[position] != Integer.parseInt(question.get("expected").toString()))
            answerTxtView.setTextColor(Color.RED);
        else
            answerTxtView.setTextColor(Color.GREEN);

        return row;
    }
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    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 = 0; i < 10; i++) {

        int userAnswer = results[i];

        int expectedAnswer = correctAnswer[i];

        String userString = questions[i] + " " + userAnswer;

        String expectedString = "" + expectedAnswer;

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

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

        list.add(map);
    }

    // Instantiate your custom array adapter
    MyArrayAdapter adapter = new MyArrayAdapter(this.getApplicationContext(), R.layout.list_row, list, questions, results);

    // Here you set the custom adapter to your ListView.
    itemList.setAdapter(adapter);

}
<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>
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;
  }
}
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