Skip to content

Commit

Permalink
Aula 222 - Criando listagem de contatos customizada
Browse files Browse the repository at this point in the history
  • Loading branch information
daniloamsilva committed Aug 20, 2018
1 parent 327f374 commit be40fe6
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package br.com.whatsappandroid.cursoandroid.whatsapp2.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;

import br.com.whatsappandroid.cursoandroid.whatsapp2.R;
import br.com.whatsappandroid.cursoandroid.whatsapp2.model.Contato;

public class ContatoAdapter extends ArrayAdapter<Contato> {

private ArrayList<Contato> contatos;
private Context context;

public ContatoAdapter(Context c, ArrayList<Contato> objects) {
super(c, 0, objects);
this.contatos = objects;
this.context = c;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View view = null;

// Verifica se a lista está vazia
if( contatos != null ){

// inicializar objeto para montagem da view
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

// Monta view a partir do xml
view = inflater.inflate(R.layout.lista_contato, parent, false);

// recupera elemento para exibição
TextView nomeContato = view.findViewById(R.id.tv_nome);
TextView emailContato = view.findViewById(R.id.tv_email);

Contato contato = contatos.get( position );
nomeContato.setText( contato.getNome());
emailContato.setText( contato.getEmail() );

}

return view;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.ArrayList;

import br.com.whatsappandroid.cursoandroid.whatsapp2.R;
import br.com.whatsappandroid.cursoandroid.whatsapp2.adapter.ContatoAdapter;
import br.com.whatsappandroid.cursoandroid.whatsapp2.config.ConfiguracaoFirebase;
import br.com.whatsappandroid.cursoandroid.whatsapp2.helper.Preferencias;
import br.com.whatsappandroid.cursoandroid.whatsapp2.model.Contato;
Expand All @@ -29,7 +30,7 @@ public class ContatosFragment extends Fragment {

private ListView listView;
private ArrayAdapter adapter;
private ArrayList<String> contatos;
private ArrayList<Contato> contatos;
private DatabaseReference firebase;
private ValueEventListener valueEventListenerContatos;

Expand Down Expand Up @@ -61,11 +62,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
View view = inflater.inflate(R.layout.fragment_contatos, container, false);

listView = view.findViewById(R.id.lv_contatos);
adapter = new ArrayAdapter(
/*adapter = new ArrayAdapter(
getActivity(),
R.layout.lista_contato,
contatos
);
);*/
adapter = new ContatoAdapter(getActivity(), contatos );
listView.setAdapter(adapter);

Preferencias preferencias = new Preferencias(getActivity());
Expand All @@ -85,7 +87,7 @@ public void onDataChange(DataSnapshot dataSnapshot) {
//Listar contatos
for (DataSnapshot dados: dataSnapshot.getChildren() ){
Contato contato = dados.getValue( Contato.class );
contatos.add(contato.getNome());
contatos.add(contato);
}
adapter.notifyDataSetChanged();
}
Expand Down
35 changes: 28 additions & 7 deletions app/src/main/res/layout/lista_contato.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:textColor="@android:color/black"
android:textSize="18sp"
android:padding="8dp"
android:text="Nome"
/>
android:orientation="vertical">

<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv_nome"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textColor="@android:color/black"
android:textSize="18sp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:text="Nome do Contato"
/>

<TextView
android:id="@+id/tv_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="14sp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="contato@email.com.br"/>

</LinearLayout>

0 comments on commit be40fe6

Please sign in to comment.