我正在制作聊天应用程序,我创建了这个ArrayAdapter 包帮助者;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import als.chaterix.R;
/**
* Created by arl on 27-Jan-15.
*/
public class ChatAdapter extends ArrayAdapter<String> {
private final Context context;
private final List values;
public ChatAdapter(Context context, List values) {
super(context, R.layout.messagelayout, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.messagelayout, parent, false);
TextView sender = (TextView) rowView.findViewById(R.id.user);
TextView date = (TextView) rowView.findViewById(R.id.date);
TextView messa = (TextView)rowView.findViewById(R.id.message);
sender.setText(((HashMap<String, String>)values.get(position)).get("user_id"));
ShortcutFunc sf = new ShortcutFunc(this.context);
try{
date.setText(sf.FormatDate(((HashMap<String, String>)values.get(position)).get("time")));
}catch(Exception e){
Log.d("AdapterTimeError", e.toString());
}
messa.setText(((HashMap<String, String>) values.get(position)).get("message"));
return rowView;
}
}
但这一行:
try{
date.setText(sf.FormatDate(((HashMap<String, String>)values.get(position)).get("time")));
}catch(Exception e){
Log.d("AdapterTimeError", e.toString());
}
如果我在我的sf(ShortcutFunc)类中使用名为FormatDate的函数,那么date将同样应用于TextView,但是如果我不使用该函数那么它很好但格式错误。
我很确定它与位置变量有关,但我不知道是什么。
我该如何解决?我的意思是,就像日期位置被卡在第一次进入。
这是功能:
public String FormatDate(String date) throws ParseException {
String turnOut = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd hh:mm:ss");
java.util.Date date2 = sdf.parse(date);
Calendar c = Calendar.getInstance();
c.setTime(date2);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE,0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND,0);
Calendar yesterday = Calendar.getInstance();
yesterday.add(Calendar.DAY_OF_MONTH, -1);
yesterday.set(Calendar.HOUR_OF_DAY,0);
yesterday.set(Calendar.MINUTE,0);
yesterday.set(Calendar.SECOND,0);
yesterday.set(Calendar.MILLISECOND,0);
if (c.getTime().equals(new java.util.Date())){
turnOut = "Today";
} else if (c.equals(yesterday)) {
turnOut = "Yesterday";
} else {
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy hh:mm");
turnOut = sdf1.format(date);
}
return turnOut;
}
图片:
Log.d("StackOverflow", ((HashMap<String, String>)values.get(position)).get("time"));
还有一些有趣的东西,它是异常的,它是什么意思?
堆栈跟踪:
> 01-27 14:15:43.664 25244-25244/als.chaterix W/System.err﹕
> java.lang.IllegalArgumentException: Bad class: class java.lang.String
> 01-27 14:15:43.664 25244-25244/als.chaterix W/System.err﹕ at
> java.text.DateFormat.format(DateFormat.java:296) 01-27 14:15:43.664
> 25244-25244/als.chaterix W/System.err﹕ at
> java.text.Format.format(Format.java:93) 01-27 14:15:43.664
> 25244-25244/als.chaterix W/System.err﹕ at
> helpers.ShortcutFunc.FormatDate(ShortcutFunc.java:120) 01-27
> 14:15:43.664 25244-25244/als.chaterix W/System.err﹕ at
> helpers.ChatAdapter.getView(ChatAdapter.java:44) 01-27 14:15:43.664
> 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.AbsListView.obtainView(AbsListView.java:2255) 01-27
> 14:15:43.664 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.ListView.makeAndAddView(ListView.java:1790) 01-27
> 14:15:43.664 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.ListView.fillUp(ListView.java:725) 01-27 14:15:43.664
> 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.ListView.correctTooHigh(ListView.java:1414) 01-27
> 14:15:43.664 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.ListView.fillSpecific(ListView.java:1352) 01-27
> 14:15:43.664 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.ListView.layoutChildren(ListView.java:1620) 01-27
> 14:15:43.664 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.AbsListView.onLayout(AbsListView.java:2087) 01-27
> 14:15:43.664 25244-25244/als.chaterix W/System.err﹕ at
> android.view.View.layout(View.java:14841) 01-27 14:15:43.664
> 25244-25244/als.chaterix W/System.err﹕ at
> android.view.ViewGroup.layout(ViewGroup.java:4631) 01-27 14:15:43.664
> 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 01-27
> 14:15:43.664 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.FrameLayout.onLayout(FrameLayout.java:388) 01-27
> 14:15:43.664 25244-25244/als.chaterix W/System.err﹕ at
> android.view.View.layout(View.java:14841) 01-27 14:15:43.664
> 25244-25244/als.chaterix W/System.err﹕ at
> android.view.ViewGroup.layout(ViewGroup.java:4631) 01-27 14:15:43.664
> 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 01-27
> 14:15:43.664 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.FrameLayout.onLayout(FrameLayout.java:388) 01-27
> 14:15:43.664 25244-25244/als.chaterix W/System.err﹕ at
> android.view.View.layout(View.java:14841) 01-27 14:15:43.664
> 25244-25244/als.chaterix W/System.err﹕ at
> android.view.ViewGroup.layout(ViewGroup.java:4631) 01-27 14:15:43.664
> 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 01-27
> 14:15:43.664 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.FrameLayout.onLayout(FrameLayout.java:388) 01-27
> 14:15:43.664 25244-25244/als.chaterix W/System.err﹕ at
> android.view.View.layout(View.java:14841) 01-27 14:15:43.664
> 25244-25244/als.chaterix W/System.err﹕ at
> android.view.ViewGroup.layout(ViewGroup.java:4631) 01-27 14:15:43.664
> 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
> 01-27 14:15:43.664 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
> 01-27 14:15:43.664 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 01-27
> 14:15:43.664 25244-25244/als.chaterix W/System.err﹕ at
> android.view.View.layout(View.java:14841) 01-27 14:15:43.664
> 25244-25244/als.chaterix W/System.err﹕ at
> android.view.ViewGroup.layout(ViewGroup.java:4631) 01-27 14:15:43.664
> 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 01-27
> 14:15:43.664 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.FrameLayout.onLayout(FrameLayout.java:388) 01-27
> 14:15:43.664 25244-25244/als.chaterix W/System.err﹕ at
> android.view.View.layout(View.java:14841) 01-27 14:15:43.664
> 25244-25244/als.chaterix W/System.err﹕ at
> android.view.ViewGroup.layout(ViewGroup.java:4631) 01-27 14:15:43.664
> 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 01-27
> 14:15:43.674 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.FrameLayout.onLayout(FrameLayout.java:388) 01-27
> 14:15:43.674 25244-25244/als.chaterix W/System.err﹕ at
> android.view.View.layout(View.java:14841) 01-27 14:15:43.674
> 25244-25244/als.chaterix W/System.err﹕ at
> android.view.ViewGroup.layout(ViewGroup.java:4631) 01-27 14:15:43.674
> 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
> 01-27 14:15:43.674 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
> 01-27 14:15:43.674 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 01-27
> 14:15:43.674 25244-25244/als.chaterix W/System.err﹕ at
> android.view.View.layout(View.java:14841) 01-27 14:15:43.674
> 25244-25244/als.chaterix W/System.err﹕ at
> android.view.ViewGroup.layout(ViewGroup.java:4631) 01-27 14:15:43.674
> 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 01-27
> 14:15:43.674 25244-25244/als.chaterix W/System.err﹕ at
> android.widget.FrameLayout.onLayout(FrameLayout.java:388) 01-27
> 14:15:43.674 25244-25244/als.chaterix W/System.err﹕ at
> android.view.View.layout(View.java:14841) 01-27 14:15:43.674
> 25244-25244/als.chaterix W/System.err﹕ at
> android.view.ViewGroup.layout(ViewGroup.java:4631) 01-27 14:15:43.674
> 25244-25244/als.chaterix W/System.err﹕ at
> android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1986) 01-27
> 14:15:43.674 25244-25244/als.chaterix W/System.err﹕ at
> android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1743)
> 01-27 14:15:43.674 25244-25244/als.chaterix W/System.err﹕ at
> android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996) 01-27
> 14:15:43.674 25244-25244/als.chaterix W/System.err﹕ at
> android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5603)
> 01-27 14:15:43.674 25244-25244/als.chaterix W/System.err﹕ at
> android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
> 01-27 14:15:43.674 25244-25244/als.chaterix W/System.err﹕ at
> android.view.Choreographer.doCallbacks(Choreographer.java:574) 01-27
> 14:15:43.674 25244-25244/als.chaterix W/System.err﹕ at
> android.view.Choreographer.doFrame(Choreographer.java:544) 01-27
> 14:15:43.674 25244-25244/als.chaterix W/System.err﹕ at
> android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
> 01-27 14:15:43.674 25244-25244/als.chaterix W/System.err﹕ at
> android.os.Handler.handleCallback(Handler.java:733) 01-27 14:15:43.674
> 25244-25244/als.chaterix W/System.err﹕ at
> android.os.Handler.dispatchMessage(Handler.java:95) 01-27 14:15:43.674
> 25244-25244/als.chaterix W/System.err﹕ at
> android.os.Looper.loop(Looper.java:136) 01-27 14:15:43.674
> 25244-25244/als.chaterix W/System.err﹕ at
> android.app.ActivityThread.main(ActivityThread.java:5146) 01-27
> 14:15:43.674 25244-25244/als.chaterix W/System.err﹕ at
> java.lang.reflect.Method.invokeNative(Native Method) 01-27
> 14:15:43.674 25244-25244/als.chaterix W/System.err﹕ at
> java.lang.reflect.Method.invoke(Method.java:515) 01-27 14:15:43.674
> 25244-25244/als.chaterix W/System.err﹕ at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
> 01-27 14:15:43.674 25244-25244/als.chaterix W/System.err﹕ at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612) 01-27
> 14:15:43.674 25244-25244/als.chaterix W/System.err﹕ at
> dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd hh:mm:ss");
缺少“y”(yyyy)。turnOut = sdf1.format(date);
中:参数应为date2
(日期对象)convertView != null
并使用ViewHolder
Recycle Views in adapter RecyclerView
代替ListView
Today
是否有效?:我的意思是这句话:if (c.getTime().equals(new java.util.Date()))
因为新的Date()会在调用发生时创建一个具有当前当前毫秒数的新Date对象。如果您今天在12:51:39调用它,则日期对象将不等于五秒钟后创建的日期对象。yesterday.add(Calendar.DAY_OF_MONTH, -1);
:这不是昨天,这是一个月前。((HashMap<String, String>)values.get(position)).get("time"))
,只是为了确保该值与前一个值不同。