1

Displaying textview as list element, for this textview getting values by returning values from a method getValues()so setting text fot textview like this t1.setText(getValues());these values are like (123 kms,12 kms,13 kms,15 kms,198 kms,135 kms,456 kms,12 kms,135 kms,147 kms,....)i want to compare all elements with first one and if it is grater than first element that text color is RED ,if it it less than first element it is BLUE ,i tried something like this and got two lists.

What i tried

 List<Integer> myList = Arrays.asList(12,34,45,23,45,7,68,4,345,56,67,4,999,454,6,76,0);
        Iterator<Integer> iter = myList.iterator();
        List<Integer> lessList = new ArrayList<Integer>(), biggerList = new ArrayList<Integer>();
        Integer firstItem = iter.next();
        while (iter.hasNext()) {
            Integer currElement = iter.next();
            if (firstItem.compareTo(currElement) > -1) {
                lessList.add(currElement);
            } else {
                biggerList.add(currElement);
            }
        }

here i am not getting how to set text of textview and two colors for same textview. help me.

tried code something like this

 if(odo_chk_num>400)
                       {

                              if(t2!=null)
                              {
                                  t2.setTextColor(Color.RED);
                                t2.setText(getValues());
                              }

                       }  

 else
                       {
                           t2.setTextColor(Color.BLACK);
                       }

but here first if condition executes then all texts color changing to RED,but what i need text should be RED based on condition only. ADAPTER CLASS

public class FileArrayAdapter extends ArrayAdapter<Option>{

    private Context c;
    private int id;
    private List<Option>items;

    public FileArrayAdapter(Context context, int textViewResourceId,
            List<Option> objects) {
        super(context, textViewResourceId, objects);
        c = context;
        id = textViewResourceId;
        items = objects;
    }
    public Option getItem(int i)
     {
         return items.get(i);
     }
     @Override
       public View getView(int position, View convertView, ViewGroup parent) {
               View v = convertView;
               if (v == null) {
                   LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                   v = vi.inflate(id, null);
               }
               final Option o = items.get(position);
               if (o != null) {
                       TextView t1 = (TextView) v.findViewById(R.id.TextView01);
                       TextView t2 = (TextView) v.findViewById(R.id.TextView02);

                       if(o.getDDS().equals("Odometer reading:"))
                       {

                           int odo_num, odo_chk_num = 0;
                           System.out.println("");
                           String odo_str=o.getSpans();

                           for(int i = 0 ; i<o.getSpans().length();i++)
                           {
                               char xyz=odo_str.charAt(i);
                               System.out.println("");
                               if(xyz >= '0' && xyz <= '9')
                               {
                               odo_num=(int)xyz;

                               odo_chk_num=  odo_chk_num*10 + odo_num;

                               }

                           }

                         //  System.out.println("after digit" + odo_chk_num);


                           if(odo_chk_num>40000)
                           {
                               if(t1!=null)
                                    t1.setText(o.getDDS());
                                  if(t2!=null)
                                  {
                                     // t2.setTextColor(Color.RED);
                                      SpannableStringBuilder sb = new SpannableStringBuilder(o.getSpans());
                                      ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(158, 158, 158));
                                      sb.setSpan(fcs, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
                                    t2.setText(sb);
                                  }

                           } 

                       }


                       else
                       {
                           t2.setTextColor(Color.BLACK);

                       if(t1!=null)
                        t1.setText(o.getDDS());
                       if(t2!=null)
                        t2.setText(o.getSpans());
                       }

               }
               return v;
       }

}
4

1 回答 1

0

使一个包装器类获取该类中的所有变量和一个附加变量,该变量是 int 类型的颜色代码,并以一种方法计算此条件,然后在包装器中填充所有数据和颜色代码,然后用此包装器和颜色填充列表滚动查看时,代码也是不变的或不变的。

于 2013-10-15T09:34:14.097 回答