Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ListView (Android SDK)

rodrigorgs
October 10, 2012

ListView (Android SDK)

rodrigorgs

October 10, 2012
Tweet

More Decks by rodrigorgs

Other Decks in Programming

Transcript

  1. MATC89: Aplicações para Dispositivos Móveis Universidade Federal da Bahia (UFBA)

    Rodrigo Rocha e Christina von Flach 2012.2 ListView Android SDK
  2. Tópicos 1. ListView e ArrayAdapter 2. Adapter Personalizado 3. Reuso

    de Células 4. Padrão Holder 5. Interação e ListActivity
  3. Activity (.java) MainActivity.java public class MainActivity extends Activity { @Override

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
  4. Activity (.java) MainActivity.java public class MainActivity extends Activity { @Override

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
  5. Activity (.java) MainActivity.java private ListView listView; public class MainActivity extends

    Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
  6. Activity (.java) MainActivity.java private ListView listView; public class MainActivity extends

    Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } listView = (ListView)findViewById(R.id.listView);
  7. ArrayAdapter MainActivity.java private ListView listView; public class MainActivity extends Activity

    { private String[] contatos = {"João", "Zé", "Ana"}; // ... private ArrayAdapter<String> listAdapter;
  8. ArrayAdapter MainActivity.java private ListView listView; public class MainActivity extends Activity

    { private String[] contatos = {"João", "Zé", "Ana"}; // ... private ArrayAdapter<String> listAdapter; modelo
  9. ArrayAdapter MainActivity.java protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }

    listView = (ListView)findViewById(R.id.listView); listAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, android.R.id.text1, contatos);
  10. ArrayAdapter MainActivity.java protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }

    listView = (ListView)findViewById(R.id.listView); listAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, android.R.id.text1, contatos); layout da célula
  11. ArrayAdapter MainActivity.java protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }

    listView = (ListView)findViewById(R.id.listView); listAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, android.R.id.text1, contatos); layout da célula id do TextView
  12. ArrayAdapter MainActivity.java protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }

    listView = (ListView)findViewById(R.id.listView); listAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, android.R.id.text1, contatos); layout da célula id do TextView modelo
  13. ArrayAdapter MainActivity.java protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }

    listView = (ListView)findViewById(R.id.listView); listAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, android.R.id.text1, contatos); layout da célula id do TextView modelo listView.setAdapter(listAdapter);
  14. Tópicos 1. ListView e ArrayAdapter 2. Adapter Personalizado 3. Reuso

    de Células 4. Padrão Holder 5. Interação e ListActivity
  15. Modelo Contato.java public class Contato { private long id; private

    String nome; private String telefone; public Contato(long id, String nome, String telefone) { this.id = id; this.nome = nome; this.telefone = telefone; } // getters e setters }
  16. Modelo MainActivity.java private ListView listView; public class MainActivity extends Activity

    { private String[] contatos = {"João", "Zé", "Ana"}; // ... private ArrayAdapter<String> listAdapter;
  17. Modelo MainActivity.java private ListView listView; public class MainActivity extends Activity

    { private List<Contato> contatos = new ArrayList<Contato>(); // ... private ArrayAdapter<String> listAdapter;
  18. Adapter Personalizado ContatoAdapter.java public class ContatoAdapter extends ArrayAdapter<Contato> { public

    ContatoAdapter(Context context, List<Contato> contatos) { super(context, 0, contatos); } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = null; return view; } // ... }
  19. Adapter Personalizado ContatoAdapter.java public class ContatoAdapter extends ArrayAdapter<Contato> { public

    ContatoAdapter(Context context, List<Contato> contatos) { super(context, 0, contatos); } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = null; return view; } // ... }
  20. Adapter Personalizado ContatoAdapter.java public class ContatoAdapter extends ArrayAdapter<Contato> { public

    ContatoAdapter(Context context, List<Contato> contatos) { super(context, 0, contatos); } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = null; return view; } // ... } salva o contexto e a lista
  21. Adapter Personalizado ContatoAdapter.java public class ContatoAdapter extends ArrayAdapter<Contato> { public

    ContatoAdapter(Context context, List<Contato> contatos) { super(context, 0, contatos); } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = null; return view; } // ... } cria uma view para cada célula salva o contexto e a lista
  22. Adapter Personalizado ContatoAdapter.java @Override public View getView(int position, View convertView,

    ViewGroup parent) { View view = null; return view; } Contato contato = getItem(position); if (contato != null) { } definido em ArrayAdapter
  23. Adapter Personalizado ContatoAdapter.java @Override public View getView(int position, View convertView,

    ViewGroup parent) { View view = null; return view; } Contato contato = getItem(position); if (contato != null) { } LayoutInflater inflater = (LayoutInflater)getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(android.R.layout.simple_list_item_2, null); definido em ArrayAdapter
  24. Adapter Personalizado ContatoAdapter.java @Override public View getView(int position, View convertView,

    ViewGroup parent) { View view = null; return view; } Contato contato = getItem(position); if (contato != null) { } LayoutInflater inflater = (LayoutInflater)getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(android.R.layout.simple_list_item_2, null); TextView text1 = (TextView)view.findViewById(android.R.id.text1); TextView text2 = (TextView)view.findViewById(android.R.id.text2); definido em ArrayAdapter
  25. Adapter Personalizado ContatoAdapter.java @Override public View getView(int position, View convertView,

    ViewGroup parent) { View view = null; return view; } Contato contato = getItem(position); if (contato != null) { } LayoutInflater inflater = (LayoutInflater)getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(android.R.layout.simple_list_item_2, null); text1.setText(contato.getNome()); text2.setText(contato.getTelefone()); TextView text1 = (TextView)view.findViewById(android.R.id.text1); TextView text2 = (TextView)view.findViewById(android.R.id.text2); definido em ArrayAdapter
  26. Tópicos 1. ListView e ArrayAdapter 2. Adapter Personalizado 3. Reuso

    de Células 4. Padrão Holder 5. Interação e ListActivity
  27. Reuso de Células ContatoAdapter.java @Override public View getView(int position, View

    convertView, ViewGroup parent) { View view = return view; } Contato contato = getItem(position); if (contato != null) { LayoutInflater inflater = (LayoutInflater)getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(android.R.layout.simple_list_item_2, null); text1.setText(contato.getNome()); text2.setText(contato.getTelefone()); TextView text1 = (TextView)view.findViewById(android.R.id.text1); TextView text2 = (TextView)view.findViewById(android.R.id.text2); null; }
  28. Reuso de Células ContatoAdapter.java @Override public View getView(int position, View

    convertView, ViewGroup parent) { View view = return view; } Contato contato = getItem(position); if (contato != null) { LayoutInflater inflater = (LayoutInflater)getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(android.R.layout.simple_list_item_2, null); text1.setText(contato.getNome()); text2.setText(contato.getTelefone()); TextView text1 = (TextView)view.findViewById(android.R.id.text1); TextView text2 = (TextView)view.findViewById(android.R.id.text2); ; }
  29. Reuso de Células ContatoAdapter.java @Override public View getView(int position, View

    convertView, ViewGroup parent) { View view = return view; } Contato contato = getItem(position); if (contato != null) { LayoutInflater inflater = (LayoutInflater)getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(android.R.layout.simple_list_item_2, null); text1.setText(contato.getNome()); text2.setText(contato.getTelefone()); TextView text1 = (TextView)view.findViewById(android.R.id.text1); TextView text2 = (TextView)view.findViewById(android.R.id.text2); ; } convertView
  30. Reuso de Células ContatoAdapter.java @Override public View getView(int position, View

    convertView, ViewGroup parent) { View view = return view; } Contato contato = getItem(position); if (contato != null) { LayoutInflater inflater = (LayoutInflater)getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(android.R.layout.simple_list_item_2, null); text1.setText(contato.getNome()); text2.setText(contato.getTelefone()); TextView text1 = (TextView)view.findViewById(android.R.id.text1); TextView text2 = (TextView)view.findViewById(android.R.id.text2); ; } if (view == null) { } convertView
  31. Reuso de Células ContatoAdapter.java @Override public View getView(int position, View

    convertView, ViewGroup parent) { View view = return view; } Contato contato = getItem(position); if (contato != null) { LayoutInflater inflater = (LayoutInflater)getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(android.R.layout.simple_list_item_2, null); text1.setText(contato.getNome()); text2.setText(contato.getTelefone()); TextView text1 = (TextView)view.findViewById(android.R.id.text1); TextView text2 = (TextView)view.findViewById(android.R.id.text2); ; } if (view == null) { } convertView lento!
  32. Tópicos 1. ListView e ArrayAdapter 2. Adapter Personalizado 3. Reuso

    de Células 4. Padrão Holder 5. Interação e ListActivity
  33. Padrão Holder ContatoAdapter.java public class ContatoAdapter extends ArrayAdapter<Contato> { public

    ContatoAdapter(...) { ... } public View getView(...) { ... } static class ViewHolder { public TextView textNome; public TextView textTelefone; } }
  34. Padrão Holder ContatoAdapter.java @Override public View getView(int position, View convertView,

    ViewGroup parent) { View view = return view; } Contato contato = getItem(position); if (contato != null) { LayoutInflater inflater = (LayoutInflater)getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(android.R.layout.simple_list_item_2, null); ; } if (view == null) { convertView } TextView text1 = TextView text2 = (TextView)view.findViewById(android.R.id.text1); (TextView)view.findViewById(android.R.id.text2); text1 text2 .setText(contato.getNome()); .setText(contato.getTelefone());
  35. Padrão Holder ContatoAdapter.java LayoutInflater inflater = (LayoutInflater)getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view =

    inflater.inflate(android.R.layout.simple_list_item_2, null); text1 text2 TextView text1 = TextView text2 = if (view == null) { (TextView)view.findViewById(android.R.id.text1); (TextView)view.findViewById(android.R.id.text2); } .setText(contato.getNome()); .setText(contato.getTelefone());
  36. Padrão Holder ContatoAdapter.java LayoutInflater inflater = (LayoutInflater)getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view =

    inflater.inflate(android.R.layout.simple_list_item_2, null); text1 text2 TextView text1 = TextView text2 = if (view == null) { if só é executado durante a criação inicial de células (TextView)view.findViewById(android.R.id.text1); (TextView)view.findViewById(android.R.id.text2); } .setText(contato.getNome()); .setText(contato.getTelefone());
  37. Padrão Holder ContatoAdapter.java LayoutInflater inflater = (LayoutInflater)getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view =

    inflater.inflate(android.R.layout.simple_list_item_2, null); text1 text2 TextView text1 = TextView text2 = if (view == null) { if só é executado durante a criação inicial de células sempre é executado (TextView)view.findViewById(android.R.id.text1); (TextView)view.findViewById(android.R.id.text2); } .setText(contato.getNome()); .setText(contato.getTelefone());
  38. Padrão Holder ContatoAdapter.java LayoutInflater inflater = (LayoutInflater)getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view =

    inflater.inflate(android.R.layout.simple_list_item_2, null); if (view == null) { (TextView)view.findViewById(android.R.id.text1); (TextView)view.findViewById(android.R.id.text2); } text1 text2 .setText(contato.getNome()); .setText(contato.getTelefone());
  39. Padrão Holder ContatoAdapter.java LayoutInflater inflater = (LayoutInflater)getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view =

    inflater.inflate(android.R.layout.simple_list_item_2, null); if (view == null) { (TextView)view.findViewById(android.R.id.text1); (TextView)view.findViewById(android.R.id.text2); } ViewHolder holder = new ViewHolder(); text1 text2 .setText(contato.getNome()); .setText(contato.getTelefone());
  40. Padrão Holder ContatoAdapter.java LayoutInflater inflater = (LayoutInflater)getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view =

    inflater.inflate(android.R.layout.simple_list_item_2, null); if (view == null) { (TextView)view.findViewById(android.R.id.text1); (TextView)view.findViewById(android.R.id.text2); } ViewHolder holder = new ViewHolder(); holder.textNome = holder.textTelefone = text1 text2 .setText(contato.getNome()); .setText(contato.getTelefone());
  41. Padrão Holder ContatoAdapter.java LayoutInflater inflater = (LayoutInflater)getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view =

    inflater.inflate(android.R.layout.simple_list_item_2, null); if (view == null) { (TextView)view.findViewById(android.R.id.text1); (TextView)view.findViewById(android.R.id.text2); } ViewHolder holder = new ViewHolder(); holder.textNome = holder.textTelefone = view.setTag(holder); text1 text2 .setText(contato.getNome()); .setText(contato.getTelefone());
  42. Padrão Holder ContatoAdapter.java LayoutInflater inflater = (LayoutInflater)getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view =

    inflater.inflate(android.R.layout.simple_list_item_2, null); if (view == null) { (TextView)view.findViewById(android.R.id.text1); (TextView)view.findViewById(android.R.id.text2); } ViewHolder holder = new ViewHolder(); holder.textNome = holder.textTelefone = view.setTag(holder); .setText(contato.getNome()); .setText(contato.getTelefone());
  43. Padrão Holder ContatoAdapter.java LayoutInflater inflater = (LayoutInflater)getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view =

    inflater.inflate(android.R.layout.simple_list_item_2, null); if (view == null) { (TextView)view.findViewById(android.R.id.text1); (TextView)view.findViewById(android.R.id.text2); } ViewHolder holder = new ViewHolder(); holder.textNome = holder.textTelefone = view.setTag(holder); .setText(contato.getNome()); .setText(contato.getTelefone()); ViewHolder holder = (ViewHolder)view.getTag();
  44. Padrão Holder ContatoAdapter.java LayoutInflater inflater = (LayoutInflater)getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE); view =

    inflater.inflate(android.R.layout.simple_list_item_2, null); if (view == null) { (TextView)view.findViewById(android.R.id.text1); (TextView)view.findViewById(android.R.id.text2); } ViewHolder holder = new ViewHolder(); holder.textNome = holder.textTelefone = view.setTag(holder); .setText(contato.getNome()); .setText(contato.getTelefone()); holder.textNome holder.textTelefone ViewHolder holder = (ViewHolder)view.getTag();
  45. Tópicos 1. ListView e ArrayAdapter 2. Adapter Personalizado 3. Reuso

    de Células 4. Padrão Holder 5. Interação e ListActivity
  46. Interação MainActivity.java listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent,

    View view, int position, long id) { Contato contato = (Contato)listView.getItemAtPosition(position); String mensagem = "Oi, " + contato.getNome(); Toast.makeText(MainActivity.this, mensagem, Toast.LENGTH_SHORT) .show(); } });