我写了一个简单的java程序来显示最低价值。我运行java应用程序时代码运行正常。但是,当我在扩展BaseAdapter的CustomAdapter中使用相同的代码(在Custom Listview上显示值)时。我有20个值。它返回前2个正确的值,然后显示错误的值。我在这做错了什么?
public class FeedsAdapter extends BaseAdapter {
Activity activity;
List<UsersFeeds> userFeeds;
public FeedsAdapter(Activity activity,List<UsersFeeds> userFeeds) {
// TODO Auto-generated constructor stub
this.activity = activity;
this.userFeeds = userFeeds;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return userFeeds.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return userFeeds.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return userFeeds.indexOf(getItem(position));
}
@Override
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if(view == null){
view = inflater.inflate(R.layout.recent_feeds_custom_listview, null);
holder = new ViewHolder();
holder.userName = (TextView)view.findViewById(R.id.userName);
holder.time = (TextView)view.findViewById(R.id.time);
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
UsersFeeds data = userFeeds.get(position);
String name = data.getFirstName()+" "+data.getLastName();
holder.userName.setText(name);
/**
* Time
*/
long getTime = Long.parseLong(data.getCheckInTime());
long time = getTime * (long) 1000;
Date past = new Date(time);
Date now = new Date();
long i1 = TimeUnit.MILLISECONDS.toDays(now.getTime() - past.getTime());
long i2 = TimeUnit.MILLISECONDS.toMinutes(now.getTime()
- past.getTime());
ong i3 = TimeUnit.MILLISECONDS.toHours(now.getTime() - past.getTime());
String day = i1 + " days ago";
String sec = i2 + " minutes ago";
String hours = i3 + " hours ago";
long[] values = new long[] { i1, i2, i3 };
Arrays.sort(values);
String postfix ="";
for (int i11 = 0; i11 < values.length; i11++) {
if (values[i11] != 0) {
postfix = values[i11] == i11 ? day : (values[i11] == i2 ? sec
: hours);
holder.time.setText(postfix);
}
}
return view;
}
private class ViewHolder{
TextView userName;
TextView time;
}
答案 0 :(得分:0)
我想我知道什么是错的。您应该在调用getView()
之前检查时间差异,将其放入数组中,对该数组进行排序并将该数组放入getView()
。或者您应该在调用getView()
之前对用户进行排序。现在只是将时间差异放在listView中。