繁体   English   中英

如何更改特定列表项的背景色

[英]how to change background color of specific list items

在下面的代码中,我在列表中添加了已发送和已接收的消息。现在,我想以这样的方式在列表视图中显示它们:已发送的消息必须具有不同的背景颜色,已接收的消息必须具有不同的背景颜色,任何人都可以使用。

            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;
}

您正在使用哪个Adapter 如果您使用的是CursorAdapter ,则可以在bindView() 这是一个大概的示例代码:

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);
  }

}

如果使用任何其他适配器,则可以使用相应的方法来应用相同的逻辑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM