使用正确答案绘制ListView?

时间:2014-12-03 13:58:49

标签: android android-listview

我在此列表中有一个列表问题,有4个问题,3个错误和1个真实。我创建了一个Bean来控制这些问题,bean有一个属性值,这个属性接收1或0,1为真,0为0。

当用户点击ListView上的问题时我想检查问题是否有值= 1或值= 0,如果值= 0我想要将所有值= 0绘制为红色,如果值= 1则绘制颜色为绿色,表明这个问题是真的!

我该怎么做?

我在这里尝试。

//bean
public class Bean{
    private int value;
    //get and sets
}


//activity
List<Bean> answers = new ArrayList<Bean>();
ListView listView = (ListView)findViewById(R.id.answers_listview);
listView.setAdapter(new BeanListAdapter(context, answers));


//ListAdapter
public class BeanListAdapter extends BaseAdapter {
    private List<Bean> lista;
    private Context context;    

    public BeanListAdapter(Context context, List<Bean> lista){
        this.context = context;
        this.lista = lista;        
    }   

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

    @Override
    public Object getItem(int position) {
        return lista.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        Bean item = lista.get(position);
        View layout;
        if(view == null){
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = inflater.inflate(R.layout.alternativa_list_adapter, parent, false);            
        }else{
            layout = view;
        }
        LinearLayout llAlternativa = (LinearLayout) layout.findViewById(R.id.llAlternativa);
        TextView tvAlternativa = (TextView) layout.findViewById(R.id.tvAlternativa);

        if(item.getValue() >= 1){
            llAlternativa.setBackgroundColor(Color.parseColor("#529c03"));
            tvAlternativa.setTextColor(Color.parseColor("#FFFFFF"));
        }else{        
            llAlternativa.setBackgroundColor(Color.parseColor("#d74b3f"));
            tvAlternativa.setTextColor(Color.parseColor("#FFFFFF"));
        }   


        tvAlternativa.setText(item.getTexto());        

        return layout;
    }

}


@Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Bean answer = listAnswers.get(position);
            LinearLayout llAlternativa = (LinearLayout) getView().findViewById(R.id.llAlternativa);
            TextView tvAlternativa = (TextView) getView().findViewById(R.id.tvAlternativa);
            if(answer.getValor() == 1){
                //green
                llAlternativa.setBackgroundColor(Color.parseColor("#529c03"));
                tvAlternativa.setTextColor(Color.parseColor("#FFFFFF"));
            }else{
                //red
                for(Bean x : listAnswers){
                    if(x.getValor() == 0){                      
                        llAlternativa.setBackgroundColor(Color.parseColor("#d74b3f"));
                        tvAlternativa.setTextColor(Color.parseColor("#FFFFFF"));
                    }
                }               
            }


    }

1 个答案:

答案 0 :(得分:1)

我会用另一个if语句(if (userHasChosen))包围你的getView方法中的if else语句(改变颜色)。然后在你的onItemClick方法中将布尔userHasChosen更改为true并通知你的适配器(adapter.notifyDataSetChanged();

 if(userHasChosen){     // GETVIEW     
    if(item.getValue() >= 1){
        llAlternativa.setBackgroundColor(Color.parseColor("#529c03"));
        tvAlternativa.setTextColor(Color.parseColor("#FFFFFF"));
    }else{        
        llAlternativa.setBackgroundColor(Color.parseColor("#d74b3f"));
        tvAlternativa.setTextColor(Color.parseColor("#FFFFFF"));
    }   
 }

在你的onItemClick:

  userHasChosen = true;
  adapter.notifyDataSetChanged();