gpt4 book ai didi

Android:如果 ArrayAdapter 通过函数,则将 TextView 日期设置为相同

转载 作者:行者123 更新时间:2023-11-29 20:54:30 24 4
gpt4 key购买 nike

我正在制作聊天应用程序并创建了这个 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 的函数,那么日期将完全应用于 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"));

enter image description here

还有个有意思的地方,就是giving exception,什么意思?

堆栈跟踪:

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

最佳答案

  1. SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd hh:mm:ss");缺少一个“y”(yyyy)。
  2. turnOut = sdf1.format(date); : 参数应该是date2 (日期对象)
  3. 您的 ListView 在滚动时会非常滞后,因为您没有回收 View 。检查是否convertView != null并使用 ViewHolder Recycle Views in adapter
  4. 你应该考虑使用RecyclerView而不是 ListView
  5. 是否Today工作?:我的意思是这个声明:if (c.getTime().equals(new java.util.Date()))因为 new Date() 会在调用发生时使用当前毫秒数创建一个新的 Date 对象。如果您在今天的 12:51:39 调用它,日期对象将不等于五秒后创建的日期对象。
  6. yesterday.add(Calendar.DAY_OF_MONTH, -1); : 这不是昨天,这是一个月前。
  7. 你确定/已经调试/打印到 logcat 的值 ((HashMap<String, String>)values.get(position)).get("time")) , 只是为了确保该值与之前的不同。

关于Android:如果 ArrayAdapter 通过函数,则将 TextView 日期设置为相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28170982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com