Fixes index of bound

This commit is contained in:
tom79 2017-10-18 14:25:51 +02:00
parent ab2fe48b8c
commit d4842eda35
1 changed files with 32 additions and 30 deletions

View File

@ -1213,37 +1213,39 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc
toot_content.setThreshold(1); toot_content.setThreshold(1);
toot_content.setAdapter(accountsListAdapter); toot_content.setAdapter(accountsListAdapter);
final String oldContent = toot_content.getText().toString(); final String oldContent = toot_content.getText().toString();
String[] searchA = oldContent.substring(0,currentCursorPosition).split("@"); if( oldContent.length() >= currentCursorPosition) {
if( searchA.length > 0 ) { String[] searchA = oldContent.substring(0, currentCursorPosition).split("@");
final String search = searchA[searchA.length - 1]; if (searchA.length > 0) {
toot_content.setOnItemClickListener(new AdapterView.OnItemClickListener() { final String search = searchA[searchA.length - 1];
@Override toot_content.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { @Override
Account account = accounts.get(position); public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String deltaSearch = ""; Account account = accounts.get(position);
if (currentCursorPosition - searchLength > 0 && currentCursorPosition < oldContent.length()) String deltaSearch = "";
deltaSearch = oldContent.substring(currentCursorPosition - searchLength, currentCursorPosition); if (currentCursorPosition - searchLength > 0 && currentCursorPosition < oldContent.length())
else { deltaSearch = oldContent.substring(currentCursorPosition - searchLength, currentCursorPosition);
if (currentCursorPosition >= oldContent.length()) else {
deltaSearch = oldContent.substring(currentCursorPosition - searchLength, oldContent.length()); if (currentCursorPosition >= oldContent.length())
} deltaSearch = oldContent.substring(currentCursorPosition - searchLength, oldContent.length());
}
if (!search.equals("")) if (!search.equals(""))
deltaSearch = deltaSearch.replace("@" + search, ""); deltaSearch = deltaSearch.replace("@" + search, "");
String newContent = oldContent.substring(0, currentCursorPosition - searchLength); String newContent = oldContent.substring(0, currentCursorPosition - searchLength);
newContent += deltaSearch; newContent += deltaSearch;
newContent += "@" + account.getAcct() + " "; newContent += "@" + account.getAcct() + " ";
int newPosition = newContent.length(); int newPosition = newContent.length();
if (currentCursorPosition < oldContent.length() - 1) if (currentCursorPosition < oldContent.length() - 1)
newContent += oldContent.substring(currentCursorPosition, oldContent.length() - 1); newContent += oldContent.substring(currentCursorPosition, oldContent.length() - 1);
toot_content.setText(newContent); toot_content.setText(newContent);
toot_space_left.setText(String.valueOf(toot_content.length())); toot_space_left.setText(String.valueOf(toot_content.length()));
toot_content.setSelection(newPosition); toot_content.setSelection(newPosition);
AccountsSearchAdapter accountsListAdapter = new AccountsSearchAdapter(TootActivity.this, new ArrayList<Account>()); AccountsSearchAdapter accountsListAdapter = new AccountsSearchAdapter(TootActivity.this, new ArrayList<Account>());
toot_content.setThreshold(1); toot_content.setThreshold(1);
toot_content.setAdapter(accountsListAdapter); toot_content.setAdapter(accountsListAdapter);
} }
}); });
}
} }
} }
} }