更改选择/单击列表视图项目的背景 - Android

2023-12-11

我正在研究quiz application.为此我正在使用listview for the dispaly the answers options,我想在用户选择列表视图项时更改列表视图背景颜色,如果答案是correct然后设置green background and wrong然后设置red background

我已经尝试了很多,但我没有得到解决方案。

适配器类

public class ListviewAdapter extends BaseAdapter{

public List<String> Questions;  

public Activity context;  
public LayoutInflater inflater;
private int[] colors = new int[] { 0x30505050, 0x30808080 };

private String[] opt_no;
public static View change_color;

public ListviewAdapter(Activity context,List<String> answers, String[] que_opt_no) {  
    super();  

    this.context = context;  
    this.Questions = answers; 
    this.opt_no = que_opt_no;

    //this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
}  

@Override  
public int getCount() {  
    // TODO Auto-generated method stub  
    return Questions.size();  
}  

@Override  
public Object getItem(int position) {  
    // TODO Auto-generated method stub  
     return Questions.get(position);  
}  

@Override  
public long getItemId(int position) {  
    // TODO Auto-generated method stub  
    return 0;  
}  

public static class ViewHolder  
{  

    TextView txtquestion;  
    TextView txtquestion_no;  
}  

@Override  
public View getView(int position, View convertView, ViewGroup parent) {  
    // TODO Auto-generated method stub  

    ViewHolder holder; 

    LayoutInflater inflater = context.getLayoutInflater();
   // this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    String fontPath = "fonts/Face Your Fears.ttf";

    if(convertView==null)  
    {  
        holder = new ViewHolder();  
        convertView = inflater.inflate(R.layout.quiz_questions_listitem, null);  

        holder.txtquestion = (TextView) convertView.findViewById(R.id.textView_option);
        holder.txtquestion_no = (TextView) convertView.findViewById(R.id.textView_option_no);

       // holder.txtquestion .setTypeface(Typeface.createFromAsset(convertView.getContext().getAssets(),fontPath));

        convertView.setTag(holder);  

    }  
    else  
        holder=(ViewHolder)convertView.getTag(); 


   /* int colorPos = position % colors.length;
    convertView.setBackgroundColor(colors[colorPos]); */

    change_color = convertView;

   // convertView.setBackgroundResource(R.drawable.listview_background);


    holder.txtquestion.setText(Questions.get(position)); 
    holder.txtquestion_no.setText(opt_no[position]); 

    return convertView;  
}  

/*public static void setbackground(){

    String answer = SelectedAnswer.getAnswer();
       if (Display_questions.currentQ.getAnswer().trim().equals(answer.trim()))
    { 

          Toast.makeText(change_color.getContext(), "red",Toast.LENGTH_SHORT).show();
         change_color.setBackgroundResource(R.drawable.listview_background);
        //ListviewAdapter.change_color.setBackgroundResource(R.drawable.listview_background);
        //Display_questions.currentGame.incrementRightAnswers();
    }
    else{

        Toast.makeText(change_color.getContext(), "Blue",Toast.LENGTH_SHORT).show();
        change_color.setBackgroundResource(R.drawable.listview_false_background);
        //Display_questions.currentGame.incrementWrongAnswers();
    }
}*/

}

Java类

 public class Display_questions extends Activity{

 public static Question currentQ;
  public static GamePlay currentGame;
 ListView listview;

ListviewAdapter adapter;

String que_opt_no[] = {"a) ","b)","c) ","d) "};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz_questions);

             listview = (ListView) findViewById(R.id.questions_list);
        listview.setItemsCanFocus(false);

        GoToNextQuestion();
}

private void GoToNextQuestion() {
    // TODO Auto-generated method stub

    listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> myAdapter, View myView, int pos, long mylng) {


                String  selectedFromList = (String) listview.getItemAtPosition(pos);

                 SelectedAnswer.setAnswer(selectedFromList);

                 if (!checkAnswer(pos)) return;

                if (currentGame.isGameOver()){

                    Intent i = new Intent(Display_questions.this, Display_result.class);
                    i.putExtra("Timer_Value", TimerTime);
                    startActivity(i); 

                    finish();
                }
                else{

                    GoToNextQuestion();


                } 
            }
          });

     setQuestions();
  }

 private void setQuestions() {

    // set the question text from current question

    String question = currentQ.getQuestion().trim();
    TextView qText = (TextView) findViewById(R.id.txt_questions);
    qText.setText(question);


    // set the available options
    List<String> answers = currentQ.getQuestionOptions();

     adapter = new ListviewAdapter(this,answers,que_opt_no);

     listview.setAdapter(adapter);

}

 static boolean checkAnswer(int selectedPosition) {

    String answer = SelectedAnswer.getAnswer();

    if (answer==null){

        return false;
    }
    else {

         AnswerStates state = AnswerStates.NONE;

        if (currentQ.getAnswer().trim().equals(answer.trim()))
        { 

            //listview.setBackgroundResource(R.drawable.listview_background);

            currentGame.incrementRightAnswers();
            state = AnswerStates.RIGHT;

        }
        else{

            //ListviewAdapter.setbackground();
            currentGame.incrementWrongAnswers();
            state = AnswerStates.WRONG;

        }

        adapter.setSelectedAnswerState(selectedPosition, state);
        adapter.notifyDataSetChanged();

        return true;
    }
}

  }

Edit : check My images : 1.) enter image description here 2.)

enter image description here


选择正确答案后是否要更改列表视图或所选项目的背景。

        @Override
        public void onItemClick(AdapterView<?> myAdapter, View myView, int pos, long mylng) {


             String  selectedFromList = (String) listview.getItemAtPosition(pos);
             if(selectedFromList.equals("your_answer")) {
                   // to change the listview background
                   listview.setBackgroundColor(getResources().getColor(R.color.your_color_id));

                   // to change the selected item background color
                   myView.setBackgroundColor(getResources().getColor(R.color.your_color_id));
             }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

更改选择/单击列表视图项目的背景 - Android 的相关文章

随机推荐