FileListAdapter.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (C) 2012 Paul Burke
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.ipaulpro.afilechooser;
  17. import android.content.Context;
  18. import android.view.LayoutInflater;
  19. import android.view.View;
  20. import android.view.ViewGroup;
  21. import android.widget.BaseAdapter;
  22. import android.widget.TextView;
  23. import java.io.File;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. /**
  27. * List adapter for Files.
  28. *
  29. * @version 2013-12-11
  30. * @author paulburke (ipaulpro)
  31. */
  32. public class FileListAdapter extends BaseAdapter {
  33. private final static int ICON_FOLDER = R.drawable.ic_folder;
  34. private final static int ICON_FILE = R.drawable.ic_file;
  35. private final LayoutInflater mInflater;
  36. private List<File> mData = new ArrayList<File>();
  37. public FileListAdapter(Context context) {
  38. mInflater = LayoutInflater.from(context);
  39. }
  40. public void add(File file) {
  41. mData.add(file);
  42. notifyDataSetChanged();
  43. }
  44. public void remove(File file) {
  45. mData.remove(file);
  46. notifyDataSetChanged();
  47. }
  48. public void insert(File file, int index) {
  49. mData.add(index, file);
  50. notifyDataSetChanged();
  51. }
  52. public void clear() {
  53. mData.clear();
  54. notifyDataSetChanged();
  55. }
  56. @Override
  57. public File getItem(int position) {
  58. return mData.get(position);
  59. }
  60. @Override
  61. public long getItemId(int position) {
  62. return position;
  63. }
  64. @Override
  65. public int getCount() {
  66. return mData.size();
  67. }
  68. public List<File> getListItems() {
  69. return mData;
  70. }
  71. /**
  72. * Set the list items without notifying on the clear. This prevents loss of
  73. * scroll position.
  74. *
  75. * @param data
  76. */
  77. public void setListItems(List<File> data) {
  78. mData = data;
  79. notifyDataSetChanged();
  80. }
  81. @Override
  82. public View getView(int position, View convertView, ViewGroup parent) {
  83. View row = convertView;
  84. if (row == null)
  85. row = mInflater.inflate(R.layout.file, parent, false);
  86. TextView view = (TextView) row;
  87. // Get the file at the current position
  88. final File file = getItem(position);
  89. // Set the TextView as the file name
  90. view.setText(file.getName());
  91. // If the item is not a directory, use the file icon
  92. int icon = file.isDirectory() ? ICON_FOLDER : ICON_FILE;
  93. view.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0);
  94. return row;
  95. }
  96. }