Skip to content

Instantly share code, notes, and snippets.

@ericksprengel
Last active October 8, 2016 21:12
Show Gist options
  • Save ericksprengel/0b7cde81d74b4bb9a4b8d99dfe54b424 to your computer and use it in GitHub Desktop.
Save ericksprengel/0b7cde81d74b4bb9a4b8d99dfe54b424 to your computer and use it in GitHub Desktop.
AndroidA02T02 - ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="br.com.comschool.aula02b.MainActivity">
<Button
android:text="ArrayAdapter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/main_activity_array_adapter_button" />
<Button
android:text="BaseAdapter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/main_activity_base_adapter_button" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:padding="16dp"
android:text="@string/app_name"
android:background="@color/colorPrimaryDark"
android:textColor="@android:color/white"
android:id="@+id/nome"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
package br.com.comschool.aula02b;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class ArrayAdapterActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_array_adapter);
Contato[] contatos = Contato.getContatos();
ArrayAdapter adapter = new ArrayAdapter<Contato>(this, R.layout.array_adapter_activity_item, R.id.nome, contatos);
setListAdapter(adapter);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/foto"/>
<TextView
android:padding="16dp"
android:text="@string/app_name"
android:background="@color/colorPrimaryDark"
android:textColor="@android:color/white"
android:id="@+id/nome"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:padding="16dp"
android:text="@string/app_name"
android:background="@color/colorPrimary"
android:textColor="@android:color/white"
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
package br.com.comschool.aula02b;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class BaseAdapterActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base_adapter);
Contato[] contatos = Contato.getContatos();
ContatosBaseAdapter adapter = new ContatosBaseAdapter(this, contatos);
setListAdapter(adapter);
}
class ContatosBaseAdapter extends BaseAdapter {
private final Activity activity;
private final Contato[] contatos;
public ContatosBaseAdapter(Activity activity, Contato[] contatos) {
this.activity = activity;
this.contatos = contatos;
}
@Override
public int getCount() {
return contatos.length;
}
@Override
public Object getItem(int position) {
return contatos[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = activity.getLayoutInflater().inflate(
R.layout.base_adapter_activity_item,
parent, false);
}
Contato contato = contatos[position];
TextView nomeTextView = (TextView) convertView.findViewById(R.id.nome);
TextView emailTextView = (TextView) convertView.findViewById(R.id.email);
ImageView fotoImageView = (ImageView) convertView.findViewById(R.id.foto);
nomeTextView.setText(contato.nome);
emailTextView.setText(contato.email);
fotoImageView.setImageResource(contato.foto);
return convertView;
}
}
}
package br.com.comschool.aula02b;
/**
* Created by Professor2 on 08/10/2016.
*/
public class Contato {
String nome;
String email;
int idade;
int foto;
String celular;
public Contato(String nome, String email, int idade, int foto, String celular) {
this.nome = nome;
this.email = email;
this.idade = idade;
this.foto = foto;
this.celular = celular;
}
public static Contato[] getContatos() {
Contato[] contatos = new Contato[2];
contatos[0] = new Contato("Erick Massa Sprengel", "[email protected]", 27, R.mipmap.ic_launcher, "55 11 996 382 272");
contatos[1] = new Contato("Erick Massa Sprengel 2", "[email protected]", 27, R.mipmap.ic_launcher, "55 11 996 382 272");
return contatos;
}
}
package br.com.comschool.aula02b;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.main_activity_array_adapter_button).setOnClickListener(this);
findViewById(R.id.main_activity_base_adapter_button).setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent;
switch (v.getId()) {
case R.id.main_activity_array_adapter_button:
// abrir array adapter
intent = new Intent(this, ArrayAdapterActivity.class);
startActivity(intent);
break;
case R.id.main_activity_base_adapter_button:
// abrir base adapter
intent = new Intent(this, BaseAdapterActivity.class);
startActivity(intent);
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment