如何获得不同行的时间

时间:2012-05-09 04:56:03

标签: android

我创建了一个应用程序,其中的消息是从Web服务发送的。我将响应存储在列表视图中。当响应保存在列表视图中时,它为每一行提供相同的时间,我想要不同的时间最新时间,但旧时间不会改变。请给我一些想法。代码是

    public class WebMessageAdapter extends ArrayAdapter<WebMessageCore>{

        Context _Context;
        ArrayList<WebMessageCore> _WebMsgList;
        WebMessageCore _WebMsgCore;
        TextView _MsgContent;
        private LayoutInflater mInflater;
        ImageButton Arrow;
        GabrielApplication application;
        public WebMessageAdapter(Context context, int resource,
         ArrayList<WebMessageCore> contactList) {
            super(context, resource,  contactList);
            _Context = context;
            _WebMsgList = contactList;
            mInflater = LayoutInflater.from(context);
        }

        public int getCount() {
            return _WebMsgList.size();
        }

        public WebMessageCore getItem(int position) {
            return _WebMsgList.get(position);
        }

        public long getItemId(int position) {
            return position;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.custom_row_view, null);
                holder = new ViewHolder();
                holder.txtName = (TextView) convertView.findViewById(R.id.name);
                holder.txtmessage = (TextView) convertView.findViewById(R.id.meassage);
                holder.txtPhone = (TextView) convertView.findViewById(R.id.phone);
                holder.txtdate=  (TextView ) convertView.findViewById(R.id.datetime);
               //holder.Arrow =  (ImageButton)convertView.findViewById(R.id.settingArrow);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            _WebMsgCore=_WebMsgList.get(position);
            holder.txtName.setText(_WebMsgCore.getName());
            holder.txtmessage.setText(_WebMsgCore.getMessage());
           holder.txtPhone.setText(_WebMsgCore.getMobileNo());
           Arrow.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    Intent newActivity =  new Intent(_Context, ContactInfoActivity.class);
                    _Context.startActivity(newActivity);
            /*      
            Intent intent = new Intent(_Context, ContactInfoActivity.class);
                    GabrielUtil.replaceContentView(application.getGroupContext(),
                            GBConstants.contact_info, intent);*/
                }
            });

           String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
           holder.txtdate.setText(currentDateTimeString);
            return convertView;



        }


        static class ViewHolder {
            TextView txtName;
            TextView txtmessage;
            TextView txtPhone;
            TextView txtdate;
            ImageButton Arrow;
        }
    }'

1 个答案:

答案 0 :(得分:0)

该行的实际问题

String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());

以上行提供相同的日期格式,例如2012年5月9日下午4:36:12。因为我的电脑上650毫秒内执行的getView()方法给出了相同的结果。时间可能因pc的配置而异。因此,您可以尝试使用

替换上述行
        SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss.SSS a");
        Date resultdate = new Date((new Date()).getTime());
        String currentDateTimeString = sdf.format(resultdate);