How to populate data in a AutoCompleteTextView from database or from anywhere

This post belongs to a series of publications that I will write with the intent of the learn about the development Android, I’m a backend developer but the system android called my attention.

Sometime we need populate a AutoCompleteTextView but with the data from database or other repository of data, this depending if the user write for example: apple, app, appstore or play, playground, playstore, etc.

Then, how fill these record depending of the what he write the user? The class

AutoCompleteTextView has a method called addTextChangedListener this method receive the interface TextWatcher.

The interface TextWatcher needs implements three methods like the following:

@Override
public void beforeTextChanged(CharSequence sequence, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence sequence, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {

}

In this post I will use the method onTextChanged, this method receive four values, today the value interest is sequence, this value has the text that the user wrote.

On the method onTextChanged you just need create a logic for get records from databa or request api for example and create something similar to this:

//Simulate search from database or other repository
//and here create an array with the records obtained
String [] items2 = new String[]{"Apple","App","AppStore"};

//now recreate the ArrayAdapter with the new Array String and set this to variable of the AutoCompleteTextView
adapter = new ArrayAdapter<>(self,android.R.layout.simple_expandable_list_item_1,items2);
autoCompleteTextView.setAdapter(adapter)

This works for me and is very easy, I don’t know well if this is the better option or if exists some better way and with the better performance.

If you want you can clone the sample project from github.

Javascript. How to count the number of specific elements in an array

Sometime we need to count, how many elements there are in an array of Javascript, but specific elements, for example if you have an array like this:

[1, 2, 3, 5, 2, 8, 9, 2]

Then you need to know how many elements there are of the number 2, the easy form maybe can be, if you create a cycle to count each one elements and writing a conditional with the element to compare if exists or not, look this:

var count = 0;
for(var i = 0; i < array.length; ++i){
    if(array[i] == 2)
        count++;
}

If you want beautify this, maybe you can create a method with parameters, something like this:

function countEelements(element,array)
{
var count = 0;
for(var i = 0; i < array.length; ++i){
    if(array[i] == element)
        count++;
}
return count;
}

that is better, but in Javascript exists a method called filter in the arrays.

The explanation is:

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

Then if you write something like this:

var count = array.filter(function(x){return x==2}).length

You will get the total of elements of an array, that is very easy.

 

How to clone an object in JavaScript

In JavaScript the assignation of the objects always is through references, for example:

var ob1 = {a:'hi'};

var ob2 = ob1;

If you print this, It show the following:

console.log(ob1);

console.log(ob2);

{ a: 'hi' }

{ a: 'hi' }

What happend if you modify ob2? For example:

ob2.a = 'hi ob2';

Now I’ll print the value of both of them

console.log(ob1);

console.log(ob2);

The result is:

{ a: 'hi ob2' }

{ a: 'hi ob2' }

The value of both is the same, because the value of ob2 is the reference to ob1,  then how we can have two objects diferents, or exactly how to clone an object?.

That is very simple, you just need convert this object to string and convert it to object again.

var ob1 = {a:'hi'};

var ob2 = JSON.parse(JSON.stringify(ob1));

With this, now you have differents objects, if you modify ob2, this change does not affect to object ob1.

Very simple.

Programing a app to send massive emails

I know the following is not an app (app mobile, app web,.. ) it is a little program to PC, but in this days I’m not sure how to say it, whatever.

In my work we needed a way to send emails from a list, but we didn’t not want to use an external service like mailchimp for example, then we need a simple way to send those emails.

The requeriments

The email’s list will be in csv format, looks like this:

email1@email.com,Jon Doe

emial2@email.com, Rick Doe

Why need two fields? because we need into the email a greeting, for example:

Hi Jon Doe

Loremp ipsum

So each email will have the name of the person.

Continue reading Programing a app to send massive emails

MongoDB. How to install mongodb on Ubuntu 16.04

Maybe you need a MongoDB server on your system linux, in this post I will be enumerate the steps for install MongoDB on ubuntu version 16.04 that at now is the LTS version.

The first step is add the public key, you need write the following command into terminal

Continue reading MongoDB. How to install mongodb on Ubuntu 16.04

Microsoft give us books about technology

I found a publication of Microsoft that make available many books about technology, some topics are: Windows 10, Office 365, Office 2016, Power BI, Azure, Windows 8.1, Office 2013, SharePoint 2016, SharePoint 2013, Dynamics CRM, PowerShell, Exchange Server, System Center, Cloud, SQL Server.

The books are in three differents format: PDF, MOBI, EPUB.

Really are many books and in my opinion are very worthwhile, maybe in a few days I can download all this books and make them available in a pack.

Publication in Spanish

Microsoft regala libros sobre tecnología

Me encontrado con esta publicación de Microsoft que pone a disposición muchísimos libros sobre tecnología, entre los temas estan: Windows 10, Office 365, Office 2016, Power BI, Azure, Windows 8.1, Office 2013, SharePoint 2016, SharePoint 2013, Dynamics CRM, PowerShell, Exchange Server, System Center, Cloud, SQL Server

Todos los libros estan en ingles y en tres formatos distintos, PDF, MOBI, EPUB.

Realmente son muchos libros y en mi opinion valen mucho la pena, tal vez en algunos dias me pueda bajar todos y ponerlos a disposicion en un pack.

Publicacion en Ingles