-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8aec8af
commit 89d0ab8
Showing
6 changed files
with
239 additions
and
7 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
app/src/main/java/de/domjos/unitrackermobile/adapter/SuggestionAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package de.domjos.unitrackermobile.adapter; | ||
|
||
import android.app.Activity; | ||
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.TextView; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
import de.domjos.unitrackerlibrary.model.objects.DescriptionObject; | ||
|
||
public class SuggestionAdapter extends ArrayAdapter<DescriptionObject> { | ||
private Context context; | ||
private List<DescriptionObject> values; | ||
|
||
public SuggestionAdapter(@NonNull Context context, List<DescriptionObject> values) { | ||
super(context, android.R.layout.simple_list_item_1); | ||
this.context = context; | ||
this.values = values; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public View getView(int position, View convertView, @NonNull ViewGroup parent) { | ||
View view = convertView; | ||
try { | ||
if(convertView == null) { | ||
LayoutInflater inflater = ((Activity) context).getLayoutInflater(); | ||
view = inflater.inflate(android.R.layout.simple_list_item_1, parent, false); | ||
} | ||
DescriptionObject descriptionObject = this.getItem(position); | ||
if(descriptionObject != null) { | ||
TextView name = view.findViewById(android.R.id.text1); | ||
name.setText(descriptionObject.getDescription()); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return view; | ||
} | ||
|
||
@Override | ||
public Filter getFilter() { | ||
return nameFilter; | ||
} | ||
|
||
|
||
private Filter nameFilter = new Filter() { | ||
@Override | ||
public CharSequence convertResultToString(Object resultValue) { | ||
return ((DescriptionObject) resultValue).getTitle(); | ||
} | ||
|
||
@Override | ||
protected FilterResults performFiltering(CharSequence constraint) { | ||
List<DescriptionObject> descriptionObjects = new LinkedList<>(); | ||
for(int i = 0; i<=SuggestionAdapter.this.values.size()-1; i++) { | ||
DescriptionObject descriptionObject = SuggestionAdapter.this.values.get(i); | ||
if(descriptionObject!=null) { | ||
if(constraint != null && descriptionObject.getTitle()!=null) { | ||
if(descriptionObject.getTitle().contains(constraint)) { | ||
descriptionObjects.add(descriptionObject); | ||
} | ||
} | ||
} | ||
} | ||
FilterResults filterResults = new FilterResults(); | ||
filterResults.values = descriptionObjects; | ||
filterResults.count = descriptionObjects.size(); | ||
return filterResults; | ||
} | ||
|
||
@Override | ||
protected void publishResults(CharSequence constraint, FilterResults results) { | ||
List<DescriptionObject> filterList = (LinkedList) results.values; | ||
if (results != null && results.count > 0) { | ||
clear(); | ||
for (DescriptionObject descriptionObject : filterList) { | ||
add(descriptionObject); | ||
notifyDataSetChanged(); | ||
} | ||
} | ||
} | ||
}; | ||
} |
51 changes: 51 additions & 0 deletions
51
app/src/main/java/de/domjos/unitrackermobile/custom/SpecialTokenizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package de.domjos.unitrackermobile.custom; | ||
|
||
import android.text.SpannableString; | ||
import android.text.Spanned; | ||
import android.text.TextUtils; | ||
import android.widget.MultiAutoCompleteTextView; | ||
|
||
public class SpecialTokenizer implements MultiAutoCompleteTextView.Tokenizer { | ||
@Override | ||
public int findTokenStart(CharSequence charSequence, int i) { | ||
while(i > 0) { | ||
if(charSequence.charAt(i - 1)=='#' || charSequence.charAt(i - 1)=='@') { | ||
return i-1; | ||
} | ||
i--; | ||
} | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int findTokenEnd(CharSequence charSequence, int i) { | ||
while(i > 0) { | ||
if(charSequence.charAt(i - 1)==' ') { | ||
return i; | ||
} | ||
i--; | ||
} | ||
return 0; | ||
} | ||
|
||
@Override | ||
public CharSequence terminateToken(CharSequence charSequence) { | ||
int i = charSequence.length(); | ||
|
||
while (i > 0 && charSequence.charAt(i - 1) == '#' || charSequence.charAt(i - 1) == '@') { | ||
i--; | ||
} | ||
|
||
if (i > 0 && charSequence.charAt(i - 1) == ' ') { | ||
return charSequence; | ||
} else { | ||
if (charSequence instanceof Spanned) { | ||
SpannableString sp = new SpannableString(charSequence + "\n"); | ||
TextUtils.copySpansFrom((Spanned) charSequence, 0, charSequence.length(), Object.class, sp, 0); | ||
return sp; | ||
} else { | ||
return charSequence + " "; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters