Question

In following code I have added sent and received messages in a list.Now i want to show them in listview in such a way that sent messages must be with different background color and received messages with different background color anybody help.

            public List<String> getSMS() {

    List<String> sms1 = new ArrayList<String>();
    List<String> sms = new ArrayList<String>();
    sms2 = new ArrayList<String>();
    Uri uriSMSURI = Uri.parse("content://sms/");
   // Uri uriSMSURI1 = Uri.parse("content://sms/inbox");
    Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);
    //Cursor cur1 = getContentResolver().query(uriSMSURI1, null, null, null,null);
    while (cur.moveToNext()) {
        int type = cur.getColumnIndex("type");
        String address = cur.getString(cur.getColumnIndex("address"));
        long  millis = cur.getLong(cur.getColumnIndexOrThrow("date"));
        String dat = (String) DateFormat.format("EEEE, MMMM dd, yyyy h:mm:ss aa", new Date(millis));
        String body = cur.getString(cur.getColumnIndexOrThrow("body"));
        int z=address.length();

        if (z==13)
        {
            address=address.substring(3, 13);
            address="0" + address;
            address = address.toString();

        }

        if (address.equals(str))
        {
            if(cur.getString(type).equalsIgnoreCase("1")){
                // sms received


                sms.add("Received: " + body + "\n" + dat);
             }
             else if(cur.getString(type).equalsIgnoreCase("2")){
                //sms sent
                 sms.add(" Sent: " + body + "\n" + dat);
             }
         }
}
    return sms;
}
Was it helpful?

Solution

Which Adapter are you using? If you are using CursorAdapter, then you can do it in your bindView(). Here is a rough example code:

TextView tv = null; 

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
      tv = (TextView) v.findViewById(R.id.itemname);
}

@Override
public void bindView(View view, final Context context, final Cursor cursor) {

  if(condition1){
     tv.setBackgroundResource(R.color.white);
  }else{
     tv.setBackgroundResource(R.color.black);
  }

}

If you are using any other Adapter, you can use the corresponding methods to apply the same logic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top