0

i am using Edittexts in my application. but not able to set their background color at runtime. If i am doing something wrong then please suggest.

i have taken three edittexts which should change their color according to if codition used in the do while loop. please check and do response ASAp. thanks in advance.

here is my code.

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



            List<Map<String,String>> list = new ArrayList<Map<String,String >>();
            EditText ed1 = (EditText) findViewById(R.id.name);
            EditText ed2 = (EditText) findViewById(R.id.age);
            EditText ed3 = (EditText) findViewById(R.id.date);
            Map<String,String> map = new HashMap<String, String>();

            int i = 2;
            EmpDatabase empClick = new EmpDatabase(getApplicationContext());
            Cursor cursor = empClick.getDetails();
            if(cursor.moveToFirst()){
                do{
                    i= i+1;
                    if(i % 2 == 0)
                    {
                        // here is something wrong
                        ed1.setBackgroundColor(Color.BLUE);
                        ed2.setBackgroundColor(Color.BLUE);
                        ed3.setBackgroundColor(Color.BLUE);

                    }
                    else 
                    {
                        // here is something wrong
                        ed1.setBackgroundColor(Color.CYAN);
                        ed2.setBackgroundColor(Color.CYAN);
                        ed3.setBackgroundColor(Color.CYAN);
                    }
                    map = new HashMap<String, String>();
                    String name = cursor.getString(cursor.getColumnIndex("name"));
                    String age = cursor.getString(cursor.getColumnIndex("age"));
                    String time = cursor.getString(cursor.getColumnIndex("time"));
                    map.put("name",name);
                    map.put("age",age);
                    map.put("time", time);
                    list.add(map);

                }while(cursor.moveToNext());
                cursor.close();
            }


            SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.text_view, new String [] {"name", "age", "time"}, new int[] {R.id.name,R.id.age, R.id.date});
            setListAdapter(adapter);

    }
Sangeeta
  • 961
  • 2
  • 13
  • 34
  • hii just use setbackground insted of color and also setcontent view – Digvesh Patel Oct 14 '14 at 07:17
  • Check out that cursor is moved to first. May be if condition is not satisfying. – Shvet Oct 14 '14 at 07:23
  • there is not cursor problem.. i have taken an integer i to change the color. – Sangeeta Oct 14 '14 at 07:27
  • i have tried tried setbackground also.. but didnt work. – Sangeeta Oct 14 '14 at 07:28
  • Try this `int j = i%2;` and use `j.equal("0");` – Shvet Oct 14 '14 at 07:31
  • tried...didnt work. :( – Sangeeta Oct 14 '14 at 07:46
  • equal is used for strings.. not for int values – Sangeeta Oct 14 '14 at 07:47
  • any other solution? :? – Sangeeta Oct 14 '14 at 08:01
  • Please try something, make a Log after if(cursor.moveToFirst()), for example Log.d("TEST","WORKS"), because if the cursor is empty because You get no data from database, cursor.moveToFirst() returns false and Your operation will never be executet. – Opiatefuchs Oct 14 '14 at 08:03
  • also, if You got only one entry for the cursor, it wont go to next. – Opiatefuchs Oct 14 '14 at 08:05
  • cursor is not empty.. if i remove if els condition.. it works properly.. but i want to put color on edittext background in alternate blocks – Sangeeta Oct 14 '14 at 08:07
  • What is meaning of "cursor is not empty..", We are telling you to check out `if(cursor.moveToFirst())` is satisfied or not. inner code will run only and only if above code will be satisfied.use `else` and `Toast` to check that `if(cursor.moveToFirst())` part is not running. – Shvet Oct 14 '14 at 08:12
  • which if else condition do You removing to work? The if(cursor.moveToFirst), or if(i % 2 == 0)? – Opiatefuchs Oct 14 '14 at 08:46
  • if You remove if(cursor.moveToFirst()) and then it works, the cursor must be empty. – Opiatefuchs Oct 14 '14 at 08:49
  • if(i%2==0) condition – Sangeeta Oct 14 '14 at 09:02
  • Check out Integer Compare [this](http://stackoverflow.com/questions/9429185/java-integers-return-strange-result-while-compare-two-integers) and [this link](http://stackoverflow.com/questions/1514910/when-comparing-two-integers-in-java-does-auto-unboxing-occur) . – Shvet Oct 14 '14 at 09:12
  • then, follow Dhaval´s hint. The best way is to use the compare method from integer, look at the API : http://developer.android.com/reference/java/lang/Integer.html#compare%28int,%20int%29 – Opiatefuchs Oct 14 '14 at 09:35

2 Answers2

1

Where is your call to setContentView(YOUR_LAYOUT), basically without this your layout in not generated even generated yet, so findViewById(R.id.name) will result into nothing.

Techfist
  • 4,314
  • 6
  • 22
  • 32
  • I think its just a copy paste mistake, if the OP has forgot setContentView, he would see nothing. But he described it like there is no color change instead. – Opiatefuchs Oct 14 '14 at 07:20
  • there is no layout problem.. other than this its working properly, if i remove if else condition.. but dont know why this background color is not changing. – Sangeeta Oct 14 '14 at 07:26
  • the only plausible reason i see otherwise is cursor being empty. – Techfist Oct 14 '14 at 09:55
0

i got answer of this problem. i made an adapter class which extends simple adapter.. and write following code..

public class EmpCustomAdapter extends SimpleAdapter{

    private int[] colors = new int[] { 0x30FF0000, 0x300000FF };

    public EmpCustomAdapter(Context context, List<Map<String, String>> list, int resource, String[] from, int[] to) {
        super(context, list, resource, from, to);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        int colorPos = position % colors.length;
        view.setBackgroundColor(colors[colorPos]);
        return view;
    }
}

and make its object and set adapter..

EmpCustomAdapter adapter = new EmpCustomAdapter(this, list, R.layout.text_view, new String [] {"name", "age", "time"}, new int[] {R.id.name,R.id.age, R.id.date});
            setListAdapter(adapter);

and it worked.. :) thanks to all who helped me to get rid of this issue..:)cant upload result image due to reputation issue.:(

Sangeeta
  • 961
  • 2
  • 13
  • 34