android显示即时时间

时间:2012-09-25 10:20:16

标签: android layout time chat

在我的聊天应用程序中,我需要在消息下方显示聊天发送/接收时间。我用holder来显示我的消息。如何将聊天发送/接收时间包括在内?我的代码如下:

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    if (convertView == null) {
        if(itemDetailsrrayList.get(position).getId().contains("1"))
        {
            convertView = l_Inflater.inflate(R.layout.item_details_view2, null);
            holder = new ViewHolder();
            holder.txt_itemName = (TextView) convertView.findViewById(R.id.name);
            holder.chat_id=(TextView) convertView.findViewById(R.id.id);

            convertView.setTag(holder);
        }
        else
        {
            convertView = l_Inflater.inflate(R.layout.item_details_view, null);
            holder = new ViewHolder();
            holder.txt_itemName = (TextView) convertView.findViewById(R.id.name);
            holder.chat_id=(TextView) convertView.findViewById(R.id.id);

            convertView.setTag(holder);


        } 
    } 
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txt_itemName.setText(itemDetailsrrayList.get(position).getName());
    holder.chat_id.setText(itemDetailsrrayList.get(position).getId());

    return convertView;

}
static class ViewHolder {
    public TextView timef;
    TextView txt_itemName;
    TextView chat_id;

}

2 个答案:

答案 0 :(得分:1)

像这样获取日历实例

final Calendar c = Calendar.getInstance();       
    hour = c.get(Calendar.HOUR_OF_DAY);
    minute = c.get(Calendar.MINUTE);
//  second = c.get(Calendar.SECOND);         

    if (c.get(Calendar.AM_PM) == Calendar.AM)
        am_pm = "AM";
    else if (c.get(Calendar.AM_PM) == Calendar.PM) 
        am_pm = "PM";                                   

    if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
        day = "Sunday";
    else if (c.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) 
        day = "Monday";

s和

  String send_msg = "send_msg +"\n"+"Send at"+" "+hour+":"+minute+" "+ am_pm+" "+"on"+" "+day;

接收

  String receve_msg = "receve_msg +"\n"+"receve at"+" "+hour+":"+minute+" "+ am_pm+" "+"on"+" "+day;

答案 1 :(得分:1)

您可以使用日历..

 Calender cc = cc.getInstance();
    Date date = cc.getTime();
    SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss a");
    String time = format.format(date);