Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.
Next Next commit
#100[30m]. Add UI and base logic.
  • Loading branch information
Evgenii Kanivets committed Aug 15, 2016
commit c56fb1f6d6f880cbce55f1915ba60e3aed04568c
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package com.blogspot.e_kanivets.moneytracker.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.blogspot.e_kanivets.moneytracker.R;
import com.blogspot.e_kanivets.moneytracker.util.CategoryAutoCompleter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
* Custom adapter to autocomplete categories.
* Created on 3/18/16.
Expand All @@ -25,6 +33,31 @@ public CategoryAutoCompleteAdapter(Context context, int resource, CategoryAutoCo
this.autoCompleter = autoCompleter;
}

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

if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.view_category_item, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else viewHolder = (ViewHolder) convertView.getTag();

final String category = resultList.get(position);

viewHolder.tvCategory.setText(category);
viewHolder.ivCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
autoCompleter.removeFromAutoComplete(category);
resultList.remove(category);
notifyDataSetChanged();
}
});

return convertView;
}

@Override
public int getCount() {
return resultList.size();
Expand Down Expand Up @@ -63,4 +96,15 @@ protected void publishResults(CharSequence constraint, FilterResults results) {
}
};
}

public static class ViewHolder {
@Bind(R.id.tv_category)
TextView tvCategory;
@Bind(R.id.iv_cancel)
View ivCancel;

public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.blogspot.e_kanivets.moneytracker.entity.data.Category;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Util class to encapsulate category autocomplete logic.
Expand All @@ -14,9 +16,11 @@
*/
public class CategoryAutoCompleter {
private List<String> categoryList;
private Set<String> filterSet;

public CategoryAutoCompleter(CategoryController categoryController) {
categoryList = new ArrayList<>();
filterSet = new HashSet<>();

for (Category category : categoryController.readAll()) {
categoryList.add(category.getName());
Expand All @@ -32,4 +36,9 @@ public List<String> completeByPart(String part) {

return resultList;
}

public void removeFromAutoComplete(String category) {
filterSet.add(category);
categoryList.remove(category);
}
}
13 changes: 13 additions & 0 deletions app/src/main/res/drawable/ic_cancel.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:viewportHeight="24.0"
android:viewportWidth="24.0"
tools:ignore="VectorRaster"
tools:targetApi="kitkat">
<path
android:fillColor="#FF757575"
android:pathData="M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z" />
</vector>
29 changes: 25 additions & 4 deletions app/src/main/res/layout/view_category_item.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/half_margin"
android:paddingTop="@dimen/half_margin"
android:theme="@style/Text_Body1" />
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingEnd="@dimen/half_margin"
android:paddingStart="@dimen/normal_margin"
tools:ignore="UseCompoundDrawables">

<TextView
android:id="@+id/tv_category"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="@dimen/half_margin"
android:paddingTop="@dimen/half_margin"
android:theme="@style/Text_Body1" />

<ImageView
android:id="@+id/iv_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@android:string/cancel"
android:src="@drawable/ic_cancel" />

</LinearLayout>