From d4842eda35cb8a0f3007dc98e7b431f0b47892d4 Mon Sep 17 00:00:00 2001 From: tom79 Date: Wed, 18 Oct 2017 14:25:51 +0200 Subject: [PATCH] Fixes index of bound --- .../mastodon/activities/TootActivity.java | 62 ++++++++++--------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java index ecf318e25..cc5ac4846 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java @@ -1213,37 +1213,39 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc toot_content.setThreshold(1); toot_content.setAdapter(accountsListAdapter); final String oldContent = toot_content.getText().toString(); - String[] searchA = oldContent.substring(0,currentCursorPosition).split("@"); - if( searchA.length > 0 ) { - final String search = searchA[searchA.length - 1]; - toot_content.setOnItemClickListener(new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - Account account = accounts.get(position); - String deltaSearch = ""; - if (currentCursorPosition - searchLength > 0 && currentCursorPosition < oldContent.length()) - deltaSearch = oldContent.substring(currentCursorPosition - searchLength, currentCursorPosition); - else { - if (currentCursorPosition >= oldContent.length()) - deltaSearch = oldContent.substring(currentCursorPosition - searchLength, oldContent.length()); - } + if( oldContent.length() >= currentCursorPosition) { + String[] searchA = oldContent.substring(0, currentCursorPosition).split("@"); + if (searchA.length > 0) { + final String search = searchA[searchA.length - 1]; + toot_content.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + Account account = accounts.get(position); + String deltaSearch = ""; + if (currentCursorPosition - searchLength > 0 && currentCursorPosition < oldContent.length()) + deltaSearch = oldContent.substring(currentCursorPosition - searchLength, currentCursorPosition); + else { + if (currentCursorPosition >= oldContent.length()) + deltaSearch = oldContent.substring(currentCursorPosition - searchLength, oldContent.length()); + } - if (!search.equals("")) - deltaSearch = deltaSearch.replace("@" + search, ""); - String newContent = oldContent.substring(0, currentCursorPosition - searchLength); - newContent += deltaSearch; - newContent += "@" + account.getAcct() + " "; - int newPosition = newContent.length(); - if (currentCursorPosition < oldContent.length() - 1) - newContent += oldContent.substring(currentCursorPosition, oldContent.length() - 1); - toot_content.setText(newContent); - toot_space_left.setText(String.valueOf(toot_content.length())); - toot_content.setSelection(newPosition); - AccountsSearchAdapter accountsListAdapter = new AccountsSearchAdapter(TootActivity.this, new ArrayList()); - toot_content.setThreshold(1); - toot_content.setAdapter(accountsListAdapter); - } - }); + if (!search.equals("")) + deltaSearch = deltaSearch.replace("@" + search, ""); + String newContent = oldContent.substring(0, currentCursorPosition - searchLength); + newContent += deltaSearch; + newContent += "@" + account.getAcct() + " "; + int newPosition = newContent.length(); + if (currentCursorPosition < oldContent.length() - 1) + newContent += oldContent.substring(currentCursorPosition, oldContent.length() - 1); + toot_content.setText(newContent); + toot_space_left.setText(String.valueOf(toot_content.length())); + toot_content.setSelection(newPosition); + AccountsSearchAdapter accountsListAdapter = new AccountsSearchAdapter(TootActivity.this, new ArrayList()); + toot_content.setThreshold(1); + toot_content.setAdapter(accountsListAdapter); + } + }); + } } } }