0

I want to show a list of the last 'n' entries when the user taps on a text input. How can I do it?

Right now I am extending EditText, incorporating the 'x' button to clear its content. So, my idea is incorporate this functionality to this widget

Angel
  • 902
  • 8
  • 16
Manuel
  • 2,236
  • 2
  • 18
  • 28
  • just add recent data entered to the adapter of ACTV – pskink Feb 04 '14 at 11:55
  • @pskink, It works, but some extra work has to be done to show the last entries and get the drop down list without user input. At the moment, I have not found how to show just the last 'n' entries. – Manuel Feb 04 '14 at 15:13
  • use ArrayAdapter for example - it can use an array that you can change as you want – pskink Feb 04 '14 at 15:18

1 Answers1

0

The comment of @pskink put me on the correct way

First, the AutoCompleteTextView has to be reimplemented

public class QuickSayAutoComplete extends AutoCompleteTextView {

private Context ctx;

public QuickSayAutoComplete(Context context) {
    super(context);
    ctx = context;
    init();
}

public QuickSayAutoComplete(Context context, AttributeSet attrs) {
    super(context, attrs);
    ctx = context;
    init();
}

public QuickSayAutoComplete(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    ctx = context;
    init();
}

To add the last elements introduced, a ClickListener has to be added.

this.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            String text = getText().toString();

            // The text is not empty
            if ( (!text.matches("")) && (text.length() > 0)) {

                // The last element introduced will be the first
                ArrayAdapter<String> adapter = (ArrayAdapter<String>) getAdapter();
                adapter.remove(text);
                adapter.insert(text, 0);
            }
        }
    });

The ACTV's adapter has to be an ArrayList, if not, we cannot remove elements

    String[] array = {"Hello", "Good Morning", "Nice to see you"};
    ArrayList<String> greetings = new ArrayList<String>();
    greetings.addAll(Arrays.asList(array));
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(ctx, android.R.layout.simple_dropdown_item_1line, greetings);
    setAdapter(adapter);
    setThreshold(0);

Finally, the DropDown list is shown (with all the entries) when the user touch the ACTV and there is not text input yet. As the user introduces an input, the list gets shorter. It could be perfect if we can limit the number of entries shown in this point

// TouchListener to show the list without user input
setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        showDropDown();
        return false;
    }
});
Community
  • 1
  • 1
Manuel
  • 2,236
  • 2
  • 18
  • 28