【问题标题】:Making a sortable ListView in Android by time for my code在 Android 中按时间为我的代码制作可排序的 ListView
【发布时间】:2015-08-19 20:09:19
【问题描述】:
  • 我尝试了很多方法,但我不想按时间排序...
  • 我想显示最新的旧项目
  • 谢谢,我不知道如何或使用什么方法,我应该在哪里复制带有完整详细信息的适配器类

这里是源代码:

public class AdapterImage extends ArrayAdapter<StructureImage> {

    public AdapterImage(ArrayList<StructureImage> array) {
        super(G.context, R.layout.adapter_images, array);
    }

    private static class ViewHolder {

        public ViewGroup layoutRoot;
        public TextView  txtTitle;
        public TextView  txtTozih;
        public ImageView imgPreview;


        public ViewHolder(View view) {
            layoutRoot = (ViewGroup) view.findViewById(R.id.layoutRoot);
            txtTitle = (TextView) view.findViewById(R.id.txtTitle);
            txtTozih = (TextView) view.findViewById(R.id.txtTozih);
            imgPreview = (ImageView) view.findViewById(R.id.imgPreview);
        }


        public void fill(final ArrayAdapter<StructureImage> adapter, final StructureImage item, final int position) {
            txtTitle.setText(item.title);
            txtTozih.setText(item.tozihat);
            imgPreview.setClickable(true);
            imgPreview.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Toast.makeText(G.context, txtTitle.getText() + "", Toast.LENGTH_SHORT).show();
                }
            });
            String filename = item.thumbnail.replace("/thumbnail/", "");
            final File imageFile = new File(G.DIR_FINAL + "/" + filename);
            if ( !imageFile.exists()) {
                imgPreview.setImageBitmap(null);
                DownloadManager.addToDownloadList(item.thumbnail);
            }

            BitmapFactory.Options options = new BitmapFactory.Options();
            //options.inSampleSize = 8;
            Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
            imgPreview.setImageBitmap(bitmap);



        }
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        StructureImage item = getItem(position);
        if (convertView == null) {
            convertView = G.inflater.inflate(R.layout.adapter_images, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.fill(this, item, position);
        return convertView;
    }
}

【问题讨论】:

    标签: android sorting listview adapter


    【解决方案1】:

    让我尝试一下,因为它似乎有足够的兴趣。完成显示最旧的方法的一种方法是在构造函数中对列表进行排序:

    public class ListObject {
        String name;
        long timestamp;
    
        public ListObject(String name, long timestamp) {
            this.name = name;
            this.timestamp = timestamp;
        }
    }
    
    public class SortedObjectArrayAdapter extends ArrayAdapter<ListObject> {
        public SortedObjectArrayAdapter(Context context, int resource, List<ListObject> listItems) {
            super(context, resource, sortList(listItems));
        }
    
        static List<ListObject> sortList(List<ListObject> list) {
            Collections.sort(list, new Comparator<ListObject>() {
                @Override
                public int compare(ListObject left, ListObject right) {
                    return (left.timestamp < right.timestamp) ? -1 : ((left.timestamp > right.timestamp) ? 1 : 0);
                }
            });
            return list;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-11
      • 1970-01-01
      • 1970-01-01
      • 2014-03-30
      • 2021-11-01
      • 2017-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多