I am making an OTP Activity and need to implement delete feature, I have the below function that should be fired onKey up of the backspace button. My thought is that I could use getCurrentFocus to know which fields are currently being filled and then use requestFocus to select the one before, but it does not seem to work, I have another function for goToNext that works just fine, so I am confused why.
public void goToPrevious() {
EditText currentFocus = (EditText) getCurrentFocus();
if (currentFocus == otpOne) {
return;
}
if (currentFocus == otpTwo) {
otpOne.requestFocus();
return;
}
if (currentFocus == otpThree) {
otpTwo.requestFocus();
return;
}
if (currentFocus == otpFour) {
otpThree.requestFocus();
return;
}
}
Below is the code for goToNext that works fine.
public void goToNext() {
EditText currentFocus = (EditText) getCurrentFocus();
if (currentFocus == otpOne) {
otpTwo.requestFocus();
return;
}
if (currentFocus == otpTwo) {
otpThree.requestFocus();
return;
}
if (currentFocus == otpThree) {
otpFour.requestFocus();
return;
}
if (currentFocus == otpFour) {
return;
}
}
I call the goToPrevious by implementing an onKeyListener, below is the code
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
EditText currentFocus = (EditText) getCurrentFocus();
Log.i("ENOUGH", "onKey: " + keyCode + " " + event.getAction() + " " + KeyEvent.KEYCODE_DEL);
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_DEL: {
currentFocus.setText(null);
goToPrevious();
return false;
}
}
return false;
}
I got to fix it by passing the value of getCurrentFocus directly into goToPrevious for some very weird reason the value of getCurrentFocus inside the onKey handler is different from goToPrevious, below is the code I ended up with.
public void goToNext(EditText currentFocus) {
if (currentFocus == otpOne) {
otpTwo.requestFocus();
return;
}
if (currentFocus == otpTwo) {
otpThree.requestFocus();
return;
}
if (currentFocus == otpThree) {
otpFour.requestFocus();
return;
}
if (currentFocus == otpFour) {
return;
}
}
public void goToPrevious(EditText currentFocus) {
if (currentFocus == otpOne) {
return;
}
if (currentFocus == otpTwo) {
otpOne.requestFocus();
return;
}
if (currentFocus == otpThree) {
otpTwo.requestFocus();
return;
}
if (currentFocus == otpFour) {
otpThree.requestFocus();
return;
}
}
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.i("ENOUGH", "onKey: " + keyCode + " " + event.getAction() + " " + KeyEvent.KEYCODE_DEL);
if (event.getAction() == KeyEvent.ACTION_DOWN) {
EditText currentFocus = (EditText) getCurrentFocus();
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_DEL: {
if (currentFocus.getText().length() > 0) {
currentFocus.setText(null);
} else {
goToPrevious(currentFocus);
EditText focused = (EditText) getCurrentFocus();
focused.setText(null);
}
return false;
}
case KeyEvent.KEYCODE_FORWARD:
return true;
case KeyEvent.KEYCODE_BACK:
return true;
case KeyEvent.KEYCODE_ENTER:
return true;
default: {
currentFocus.setText("");
goToNext(currentFocus);
}
}
}
return false;
}
You can use like this following:
et1.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if(s.length()==1)
{
et2.requestFocus();
}
else if(s.length()==0)
{
et1.clearFocus();
}
}
});
et2.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if(s.length()==1)
{
et3.requestFocus();
}
else if(s.length()==0)
{
et1.requestFocus();
}
}
});
et3.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if(s.length()==1)
{
et4.requestFocus();
}
else if(s.length()==0)
{
et2.requestFocus();
}
}
});
et4.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if(s.length()==1)
{
et5.requestFocus();
}
else if(s.length()==0)
{
et3.requestFocus();
}
}
});
et5.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if(s.length()==1)
{
et6.requestFocus();
}
else if(s.length()==0)
{
et4.requestFocus();
}
}
});
et6.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if(s.length()==1)
{
et6.clearFocus();
}
else if(s.length()==0)
{
et5.requestFocus();
}
}
});
Related
for example: "2020/55" I have an edittext shape, put one after 4 numbers, write a number after it and it works well. My problem starts at the time of deleting. When deleting the part after /, it shows"/////" the figures of the part before / as and instead.I add your picture, how can I delete it.
enter image description here
And mask class
public class CaseInputMask implements TextWatcher {
int uzunluk = 0;
EditText girilenMetin;
public CaseInputMask(EditText girilenMetin) {
this.girilenMetin = girilenMetin;
this.girilenMetin.addTextChangedListener(this);
}
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String metin = girilenMetin.getText().toString();
uzunluk = metin.length();
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
try {
String metin = charSequence.toString();
String girilenDeger = girilenMetin.getText().toString();
if (girilenDeger.length() == 4) {
metin += '/';
girilenMetin.setText(metin);
girilenMetin.setSelection(metin.length());
}
} catch (Exception e) {
}
}
#Override
public void afterTextChanged(Editable editable) {
}
}
You should check deletion like this:
public class CaseInputMask implements TextWatcher {
private boolean running = false;
private boolean deleting = false;
private final String inputMask = "####/##";
public CaseInputMask() {
}
#Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
deleting = count > after;
}
#Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable editable) {
if (running || deleting) {
return;
}
running = true;
int length = editable.length();
if (length < inputMask.length()) {
if (inputMask.charAt(length) != '#') {
editable.append(inputMask.charAt(length));
} else if (inputMask.charAt(length-1) != '#') {
editable.insert(length-1, inputMask, length-1, length);
}
}
running = false;
}
}
And
girilenMetin.addTextChangedListener(new CaseInputMask())
I just want to do that If I enter a grade then It will change to the points field according to entered grade and If I enter a point then It will change the grade filed according to entered point. I added textChangeListener for both editText but when I enter something my app just doesn't response.
http://i.stack.imgur.com/50LYo.png
grade.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (grade.getText().toString().equals("A+"))
{
points.setText("4.00");
}
else if (grade.getText().toString().equals("A"))
{
points.setText("3.75");
}
else if (grade.getText().toString().equals("A-"))
{
points.setText("3.50");
}
else if (grade.getText().toString().equals("B+"))
{
points.setText("3.25");
}
else if (grade.getText().toString().equals("B"))
{
points.setText("3.00");
}
else if (grade.getText().toString().equals("B-"))
{
points.setText("2.75");
}
else if (grade.getText().toString().equals("C+"))
{
points.setText("2.50");
}
else if (grade.getText().toString().equals("C"))
{
points.setText("2.25");
}
else if (grade.getText().toString().equals("D"))
{
points.setText("2.00");
}
else if (grade.getText().toString().equals("F"))
{
points.setText("0.0");
}
else
points.setText(null);
}
#Override
public void afterTextChanged(Editable s) {
}
});
points.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (points.getText().toString().matches("[4]|[4]\\.[0]{0,2}"))
{
grade.setText("A+");
}
else if (points.getText().toString().matches("[3]\\.[7][5]{1}"))
{
grade.setText("A");
}
else if (points.getText().toString().matches("[3]\\.[5][0]{0,1}"))
{
grade.setText("A-");
}
else if (points.getText().toString().matches("[3]\\.[2][5]{1}"))
{
grade.setText("B+");
}
else if (points.getText().toString().matches("[3]|[3]\\.[0]{0,2}"))
{
grade.setText("B");
}
else if (points.getText().toString().matches("[2]\\.[7][5]{1}"))
{
grade.setText("B-");
}
else if (points.getText().toString().matches("[2]\\.[5][0]{0,1}"))
{
grade.setText("C+");
}
else if (points.getText().toString().matches("[2]\\.[2][5]{1}"))
{
grade.setText("C");
}
else if (points.getText().toString().matches("[2]|[2]\\.[0]{0,2}"))
{
grade.setText("D");
}
else if (points.getText().toString().matches("[0]|[0]\\.[0]{0,2}"))
{
grade.setText("F");
}
else
grade.setText(null);
}
#Override
public void afterTextChanged(Editable s) {
}
});
Try following code:
Call setFocus in onViewCreated() method of fragment or onCreate() in activity
setFocus(grade);
setFocus(points);
TextWatcher gradeWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (grade.getText().toString().equals("A+"))
{
points.setText("4.00");
}
else if (grade.getText().toString().equals("A"))
{
points.setText("3.75");
}
else if (grade.getText().toString().equals("A-"))
{
points.setText("3.50");
}
else if (grade.getText().toString().equals("B+"))
{
points.setText("3.25");
}
else if (grade.getText().toString().equals("B"))
{
points.setText("3.00");
}
else if (grade.getText().toString().equals("B-"))
{
points.setText("2.75");
}
else if (grade.getText().toString().equals("C+"))
{
points.setText("2.50");
}
else if (grade.getText().toString().equals("C"))
{
points.setText("2.25");
}
else if (grade.getText().toString().equals("D"))
{
points.setText("2.00");
}
else if (grade.getText().toString().equals("F"))
{
points.setText("0.0");
}
else
points.setText(null);
}
};
TextWatcher pointWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if (points.getText().toString().matches("[4]|[4]\\.[0]{0,2}"))
{
grade.setText("A+");
}
else if (points.getText().toString().matches("[3]\\.[7][5]{1}"))
{
grade.setText("A");
}
else if (points.getText().toString().matches("[3]\\.[5][0]{0,1}"))
{
grade.setText("A-");
}
else if (points.getText().toString().matches("[3]\\.[2][5]{1}"))
{
grade.setText("B+");
}
else if (points.getText().toString().matches("[3]|[3]\\.[0]{0,2}"))
{
grade.setText("B");
}
else if (points.getText().toString().matches("[2]\\.[7][5]{1}"))
{
grade.setText("B-");
}
else if (points.getText().toString().matches("[2]\\.[5][0]{0,1}"))
{
grade.setText("C+");
}
else if (points.getText().toString().matches("[2]\\.[2][5]{1}"))
{
grade.setText("C");
}
else if (points.getText().toString().matches("[2]|[2]\\.[0]{0,2}"))
{
grade.setText("D");
}
else if (points.getText().toString().matches("[0]|[0]\\.[0]{0,2}"))
{
grade.setText("F");
}
else
grade.setText(null);
}
};
/*---------- SET FOCUS LISTENERS ON EDIT TEXT--------------------------*/
public void setFocus(EditText editText){
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
if (v.getId() == grade.getId()) {
grade.addTextChangedListener(gradeWatcher);
points.removeTextChangedListener(pointWatcher);
} else if (v.getId() == points.getId()) {
points.addTextChangedListener(pointWatcher);
grade.removeTextChangedListener(gradeWatcher);
}
}
}
});
}
After declaring and creating the textWatcher object, I would like to disable the send button and set it to gray if the chatText (edit text) is empty
I think it's a problem of ranking. Please help.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
list = (ListView)findViewById(R.id.listView);
list.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
chatText = (EditText)findViewById(R.id.editText);
//chatText.setOnKeyListener(this);
me = true;
send = (Button)findViewById(R.id.button);
change = (Button)findViewById(R.id.button2);
list.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
adp = new TheAdapter(getApplicationContext(),R.layout.chat);
list.setAdapter(adp);
chatText.addTextChangedListener(textWatcher);
checkFieldsForEmptyValues();
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
envoyer();
}
});
change.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
me = !me;
if (!me) {
change.setText(R.string.sender2);
} else {
change.setText(R.string.sender);
}
}
});
}
public void envoyer(){
adp.add(new messages(me, chatText.getText().toString()));
chatText.setText("");
}
private void checkFieldsForEmptyValues(){
String s1 = chatText.getText().toString();
if (s1.length() < 0 ) {
send.setEnabled(false);
} else {
send.setEnabled(true);
send.setBackgroundColor(Color.BLUE);
//send.setBackgroundColor((getResources().getColor(R.color.blue)));
}
}
In the onTextChanged is where you would check to see if the text field is empty:
chatText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 0) { //enable}
else if (s.length() == 0 { //disable }
In your code, you have if (s1.length() < 0 ) which I don't think will ever be true because the text size will never be less than 0.
// Disable on init
send.setEnabled(false);
// add text changer
chatText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Enable when input is != null and not empty. You can check string lenght too
send.setEnabled(s != null && !s.toString().isEmpty());
}
#Override
public void afterTextChanged(Editable s) {
}
});
disable button before and add textwatch. You can also check if edittext value is not empty or verify string length.
I hope it will help you
I have four edittext (atm pin kind of thing) and I want to validate all these four edittext on click of keypads done button(validations like - empty or wrong pin). so far, I could get the validations but it happens only if I click the done buttn twice. not able to figure out the issue, please help. I am a beginner. thanks!
Below is my code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.pin_new);
setTextFocus(edtpin1, edtpin2);
setTextFocus(edtpin2, edtpin3);
setTextFocus(edtpin3, edtpin4);
setTextFocus(edtpin5, edtpin6);
setTextFocus(edtpin6, edtpin7);
setTextFocus(edtpin7, edtpin8);
edtpin1.setOnEditorActionListener(new OnEditorActionListener(){
#Override
public boolean onEditorAction(TextView v, int arg1, KeyEvent arg2) {
switch (v.getId()) {
case R.id.btnBackPin:
Intent i = new Intent(getApplicationContext(),RegistrationProfileEmail.class);
startActivity(i);
break;
case R.id.btnCancelPin:
Intent in = new Intent(getApplicationContext(),Exit.class);
finish();
startActivity(in);
break;
case R.id.btnConfirmPin:
String s = new String();
s = edtpin1.getText().toString() + edtpin2.getText().toString()
+edtpin3.getText().toString() + edtpin4.getText().toString();
if ((edtpin5.getText().toString().trim().isEmpty())
|| (edtpin1.getText().toString().trim().isEmpty())
|| (edtpin2.getText().toString().trim().isEmpty())
|| (edtpin4.getText().toString().trim().isEmpty())
|| (edtpin3.getText().toString().trim().isEmpty())
|| (edtpin6.getText().toString().trim().isEmpty())
|| (edtpin7.getText().toString().trim().isEmpty())
|| (edtpin8.getText().toString().trim().isEmpty())) {
Toast.makeText(getApplicationContext(), "Enter the Pin",
Toast.LENGTH_SHORT).show();
edtpin1.setText(null);
edtpin2.setText(null);
edtpin3.setText(null);
edtpin4.setText(null);
edtpin5.setText(null);
edtpin6.setText(null);
edtpin7.setText(null);
edtpin8.setText(null);
edtpin1.requestFocus();
}
else if (edtpin1.getText().toString().trim().equalsIgnoreCase(edtpin5.getText().toString().trim())&& edtpin2.getText().toString().trim().equalsIgnoreCase(
edtpin6.getText().toString().trim()) && edtpin3.getText().toString().trim().equalsIgnoreCase( edtpin7.getText().toString().trim()) && edtpin4.getText().toString().trim().equalsIgnoreCase( edtpin8.getText().toString().trim()))
{
s = edtpin1.getText().toString() + edtpin2.getText().toString()
+ edtpin3.getText().toString()
+ edtpin4.getText().toString();
RegistrationPin.this.setPin(s);
}
// TODO move to payment option
else {
Toast.makeText(getApplicationContext(), "Pin is not matching",Toast.LENGTH_SHORT).show();
edtpin1.setText(null);
edtpin2.setText(null);
edtpin3.setText(null);
edtpin4.setText(null);
edtpin5.setText(null);
edtpin6.setText(null);
edtpin7.setText(null);
edtpin8.setText(null);
edtpin1.requestFocus();
}
break;
default:
break;
}
return false;
}
});
}
Create a class and implement the textwatcher like this:
private class CheckTextifEmpty implements TextWatcher {
#Override
public void afterTextChanged(Editable s) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (isEmptyCharacters(serverNumber)
|| isEmptyCharacters(textview1)
|| isEmptyCharacters(textview2)
|| isEmptyCharacters(textview3)) {
//do something here
} else {
//do something here
}
}
}
and add this method here:
private boolean isEmptyCharacters(TextView v) {
return v.getText().toString().trim().isEmpty();
}
and use it in your textview like this:
textview1.addTextChangedListener(new CheckTextIfEmpty());
You can use your validation like this without using a button
Refactor your code like this:
private boolean checkifValidate(TextView view1,View TextView2){
return view1.getText().toString().trim().contentEquals(view2.getText().toString());
}
Hope it helps,
edtpin1.setOnKeyListener(onSoftKeyboardDonePress);
private View.OnKeyListener onSoftKeyboardDonePress=new View.OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
{
edtpin1.getText().toString();
edtpin2.getText().toString();
edtpin3.getText().toString();
edtpin4.getText().toString();
}
return false;
}
};
I need to carry my int value from one method to another, but I didnt find any solution. One method counts number of objects and the number needs to be "delivered" to another and check, if the count equals to something.
Do you have any idea, how to do it?
Check my codes below:
#Override
protected void onPostExecute(Void result) {
ParseQuery<Animal> query = ParseQuery.getQuery(Animal.class);
query.countInBackground(callcount = new CountCallback(){
public void done(int count, ParseException e) {
if (e == null) {
Log.v("count is" + count, "count is");
//I need to export int count from "done" method to another
} else {
}
}
});
}
And the secont method, which has to recieve that value and use it:
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//i need to set int count from method above here
int threshold = 1;
int counter = mListView.getCount();
if (scrollState == SCROLL_STATE_IDLE) {
if (mListView.getLastVisiblePosition() >= counter - threshold) {
updateData();
}
}
Whole code:
public class Fragment1 extends Fragment {
static ListView mListView;
static AnimalAdapter mAdapter;
static ProgressBar mProgressBar;
static EditText mEditText;
static LayoutInflater inflater;
CountCallback callcount;
int g_count = 0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.animalsfrag, container, false);
mListView = (ListView)rootView.findViewById(R.id.animal_list);
View header = getActivity().getLayoutInflater().inflate(R.layout.header, null);
header.setPadding(2, 8, 4, 2);
mListView.setVisibility(View.INVISIBLE);
mListView.requestFocus();
mListView.addHeaderView(header);
View footie = getActivity().getLayoutInflater().inflate(R.layout.footer, null);
mListView.addFooterView(footie);
footie.setVisibility(View.INVISIBLE);
mProgressBar = (ProgressBar) rootView.findViewById (R.id.loading_animals);
mProgressBar.setVisibility(View.VISIBLE);
RemoteDataTask task = new RemoteDataTask();
task.execute();
return rootView;
}
public void updateData() {
mListView = (ListView)getView().findViewById(R.id.animal_list);
final ParseQuery<Animal> query = ParseQuery.getQuery(Animal.class);
query.setCachePolicy(CachePolicy.NETWORK_ONLY);
query.orderByAscending("animal");
query.setLimit(mListView.getCount() + 5);
Log.v("updateData", "uploading data from Parse.com");
query.findInBackground(new FindCallback<Animal>() {
#Override
public void done(List<Animal> animals, ParseException error) {
if(animals != null){
mAdapter.clear();
mProgressBar = (ProgressBar) getView().findViewById (R.id.loading_animals);
mProgressBar.setVisibility(View.INVISIBLE);
RelativeLayout footie = (RelativeLayout) getView().findViewById(R.id.footerview);
footie.setVisibility(View.VISIBLE);
mAdapter.clear();
for (int i = 0; i < animals.size(); i++) {
mAdapter.add(animals.get(i));
}
}
}
}); }
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute(); }
#Override
protected Void doInBackground(Void... params) {
return null;
}
#Override
protected void onPostExecute(Void result) {
ParseQuery<Animal> query = ParseQuery.getQuery(Animal.class);
query.countInBackground(callcount = new CountCallback(){
public void done(int count, ParseException e) {
if (e == null) {
Log.v("count is" + count, "count is");
if (mListView.getCount() == count) {
Log.v("all loaded", "executed");
g_count=count;
}
} else {
// The request failed
}
}
});
Log.v("onpostexecute", "executing");
mListView = (ListView) getView().findViewById(R.id.animal_list);
if(mListView.getCount() == 0){
updateData();
}
mEditText = (EditText) getView().findViewById(R.id.search_animal);
mAdapter = new AnimalAdapter(getActivity(), new ArrayList<Animal>());
mListView.setVisibility(View.VISIBLE);
mListView.setTextFilterEnabled(true);
mListView.setAdapter(mAdapter);
mListView.setOnScrollListener(new OnScrollListener() {
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
}
#Override
public void onScrollStateChanged(AbsListView view,
int scrollState) {
int threshold = 1;
int counter = mListView.getCount();
if (scrollState == SCROLL_STATE_IDLE) {
if (mListView.getLastVisiblePosition() >= counter
- threshold){
updateData();
Log.v("count", "count is" + g_count);
}
}
if (SCROLL_STATE_TOUCH_SCROLL == scrollState) {
View currentFocus = getActivity().getCurrentFocus();
if(currentFocus != null) {
currentFocus.clearFocus();
}
}
}
});
mEditText.addTextChangedListener(new TextWatcher(){
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s,
int start, int count, int after) {}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
System.out.println("Text ["+s+"]");
mAdapter.getFilter().filter(s.toString());
}
});
}
}
}
I'd be glad for any advice.
Thanks in advance.
If you are not calling function from another then you need to create a global variable.
Declare a variable just after your class declaration i.e.
int g_count = 0;
then in
public void done(int count, ParseException e) {
if (e == null) {
Log.v("count is" + count, "count is");
g_count=count;
}
Then in second fucntion
public void onScrollStateChanged(AbsListView view, int scrollState) {
int count=g_count; \\you dont really need to declare this, just use g_count
int threshold = 1;
int counter = mListView.getCount();
if (scrollState == SCROLL_STATE_IDLE) {
if (mListView.getLastVisiblePosition() >= counter - threshold) {
updateData();
}
}