我正在制作一个计划应用程序,每周都有相同的任务。我首先使用ArrayAdapter来显示所选日期的所有任务,但后来我决定制作自己的适配器。我从来没有制作过一个,所以我在互联网上看,然后通过大多数复制粘贴和更改变量来制作我的。当我在EditActivity中添加任务时,您可以看到它,但是当您切换所选日期并返回时,或者如果您转到DisplayActivity,它就会消失。没有错误。我也尝试过实现Filterable,但我也不知道它是什么,所以我主要是复制粘贴。
TaskAdapter
public class TaskAdapter extends BaseAdapter /*implements Filterable*/ {
private HashMap<Task, ViewHolder> holders = new HashMap<>();
private List<Task> mData = new ArrayList<>();
private LayoutInflater mInflater;
public TaskAdapter(Context context, List<Task> tasks) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mData = tasks;
holders = new HashMap<>();
for (Task task : tasks) {
if (holders.containsKey(task))
return;
ViewHolder holder = new ViewHolder();
View convertView = mInflater.inflate(R.layout.list_item, null);
holder.textView = (TextView) convertView.findViewById(R.id.list_item_text);
convertView.setTag(holder);
holders.put(task, holder);
}
}
public void addItem(final Task task) {
mData.add(task);
notifyDataSetChanged();
if (holders.containsKey(task))
return;
ViewHolder holder = new ViewHolder();
View convertView = mInflater.inflate(R.layout.list_item, null);
holder.textView = (TextView) convertView.findViewById(R.id.list_item_text);
convertView.setTag(holder);
holders.put(task, holder);
}
public void removeItem(final Task task) {
mData.remove(task);
holders.remove(task);
}
public void setItem(int position, Task task) {
mData.set(position, task);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Task getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.list_item, null);
holder.textView = (TextView) convertView.findViewById(R.id.list_item_text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mData.get(position).getTask());
return convertView;
}
public TextView getTextView(Task task) {
return holders.get(task).textView;
}
/*
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
mData = (List<Task>) results.values;
TaskAdapter.this.notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<Task> filteredResults = getFilteredResults(constraint);
FilterResults results = new FilterResults();
results.values = filteredResults;
return results;
}
};
} */
public static class ViewHolder {
public TextView textView;
}
}
EditActivity - onCreate()
if (getIntent().getExtras() != null) {
selectedDay = getIntent().getExtras().getString("DAY");
List<Task> tasks = getTasks(selectedDay);
tasks.add(new Task(this, getIntent().getExtras().getString("TASK_NAME"), false, true));
setTasks(selectedDay, tasks);
updateList(tasks);
}
EditActivity - setTasks(String day)
private void setTasks(String day, List<Task> tasks) {
try {
FileOutputStream fileOutputStream = openFileOutput(day + ".dat", MODE_PRIVATE);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(tasks);
} catch (IOException e) {
e.printStackTrace();
}
}
EditActivity - updateList()
private void updateList(List<Task> tasks) {
if (tasks.size() == 0) {
noPlanTextView.setVisibility(View.VISIBLE);
} else {
noPlanTextView.setVisibility(View.INVISIBLE);
}
TaskAdapter adapter = new TaskAdapter(this, tasks);
adapter.notifyDataSetChanged();
ListView listView = (ListView) findViewById(R.id.listview_edit);
listView.setAdapter(adapter);
}
我只粘贴了我认为重要/有用的代码。如果您还想要,请告诉我。谢谢!