更改自定义适配器中的列表项背景

时间:2014-06-09 14:09:44

标签: android android-listview

我有自定义适配器,我想要挑选新邮件。我的适配器看起来像。新邮件有new_message图标。

public class InputMessagesAdapter extends BaseAdapter {
private Context context;
private LayoutInflater layoutInflater;
private List<Message> messageList;
private List<SellerStatement> sellerStatementList;
private List<Integer> messagesIds;
private String arrayName = "messagesIds";
private SharedPreferences prefs;

    public InputMessagesAdapter(Context context, List<Message> messageList, List<SellerStatement> sellerStatementList) {
        this.context = context;
        layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.messageList = messageList;
        this.sellerStatementList = sellerStatementList;
        prefs = PreferenceManager.getDefaultSharedPreferences(context);

        int size = prefs.getInt(arrayName + "_size", 0);
        Integer[] tempArray = new Integer[size];

        for(int i = 0; i < size; i++)
            tempArray[i] = prefs.getInt(arrayName + "_" + i, -1);

        messagesIds = new ArrayList<Integer>(Arrays.asList(tempArray));
    }

    @Override
        public int getCount() {
        return messageList.size();
    }

    @Override
    public Object getItem(int position) {
        return messageList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        Message message = messageList.get(position);

        View view = convertView;
        if (view == null) {
            view = layoutInflater.inflate(R.layout.fragment_messages_new_row, null);
        }

        TextView subject = (TextView) view.findViewById(R.id.tvSubject);
        TextView date = (TextView) view.findViewById(R.id.tvDate);
        TextView sender = (TextView) view.findViewById(R.id.tvSender);
        ImageView icon = (ImageView) view.findViewById(R.id.imMessage);


        SellerStatement sellerStatement = sellerStatementList.get(position);

        subject.setText(message.getSubject());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        date.setText(String.valueOf(sdf.format(message.getTimestamp())));
        sender.setText(sellerStatement.getSender().getGoogleAccount());

        Drawable image = context.getResources().getDrawable(R.drawable.old_message);

        if(!messagesIds.contains(message.getMessageId()))
            image = context.getResources().getDrawable(R.drawable.new_message);


        icon.setImageDrawable(image);

        return view;
    }
} 

我试试,但所有项目都会改变颜色。

if(!messagesIds.contains(message.getMessageId())) {
    image = context.getResources().getDrawable(R.drawable.new_message);
    view.setBackgroundColor(COLOR);
}

这项工作也不正确(我改变布局的背景颜色)

View view = convertView;
if (view == null) {
    if(!messagesIds.contains(message.getMessageId())) {
        view = layoutInflater.inflate(R.layout.fragment_messages_new_row, null);
    } else {
            view = layoutInflater.inflate(R.layout.fragment_messages_row, null);
    }
}

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

很简单:

 View view = convertView; //find this line in your code
 view.setBackgroundColor(Color.CYAN); //add this line below

答案 1 :(得分:1)

更新后,适配器使用adapter.notifyDataSetChanged()强制列表重绘

答案 2 :(得分:0)

添加创建适配器的时间并创建视图, all 消息ID在列表中,因此,!messagesIds.contains(message.getMessageId())总是假的。

当您向邮件列表添加新邮件时,您必须在notifyDatasetChanged()上调用Adapter方法。这将再次创建所有视图,并使用新图标和背景颜色添加新消息