0

I am asking question in context with my previous question

I have following piece of code:

CallSoap cs=new CallSoap();


            lst=cs.GetMessage(id);
            lstNew=cs.GetNewMessage(id);

            int numOfMessages=lst.length-lstNew.length;  

            ArrayAdapter<String> adpt = new ArrayAdapter<String>(Messages.this, android.R.layout.simple_list_item_1,lst);
            lm.setAdapter(adpt);

            for (int i=0;i<numOfMessages;i++)
            {
                lm.
            }

I get number of new messages in variable : numOfMessages

lm is my listview.

ListView lm=(ListView)findViewById(R.id.listView1);

Suppose numOfMessages=3 , then i just want to make the color of text of first 3 elements of my listview (lm) as red.

Please help me how can i do that in this loop, in such a case.

Community
  • 1
  • 1
C Sharper
  • 8,284
  • 26
  • 88
  • 151

4 Answers4

2

You have to do it in your adapter. Create your custom adapter extending the base adapter and then you can check the position of your list items and apply whatever color you want.

Check here: Custom Adapter for List View

Community
  • 1
  • 1
fasteque
  • 4,309
  • 8
  • 38
  • 50
  • but how can i get ith index of listview? – C Sharper Aug 26 '13 at 09:51
  • In the getView(int position, View convertView, ViewGroup parent) method, the first parameter is the position of the item within the list. You should check that value. – fasteque Aug 26 '13 at 09:56
  • what is syntax to change the color? – C Sharper Aug 26 '13 at 10:00
  • For example: text.setTextColor(Color.RED); where text is your TextView For a complete answer look here: http://stackoverflow.com/questions/4602902/how-to-set-text-color-of-textview-in-code – fasteque Aug 26 '13 at 10:02
  • with the custom adapter you can specify the layout for your rows. I suggest you to read a bit the official documentation and the link I passed you in my first reply. – fasteque Aug 26 '13 at 10:08
2

just make sure that the actual 3 items's indexes are the right ones due to the fact that the CustomAdapter will recycle the Listview views thus giving relative indexes for the items currently visible on screen or some nearby calculation made by the adapter.

An additional approach is to manipulate the actual data collection to include a flag, which states a special condition, and then in the getView() method in the adapter, ask the current item's flag and modify that particular view

Israel Tabadi
  • 574
  • 4
  • 11
1

You should using a customized List Adapter by extending the BaseAdapter class instead of using ArrayAdapter. And int the method getView() in BaseAdapter, you can specify the certain item's color based on the position of the item.

Good Luck!

Nick
  • 1,614
  • 1
  • 12
  • 10
1

Try to impliment additional column of unique ID or Time of last fetch and next time compare database with new fetch ids or time and according to that display color in row..

Swap-IOS-Android
  • 4,363
  • 6
  • 49
  • 77