-
-
Save realdm/399dc1becc257e2911ff to your computer and use it in GitHub Desktop.
Desenvolvendo para Android- Lista e Adapters - Part 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" | |
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
tools:context="maputo.gdg.listaeadapters.activity.Segunda"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/nome_escolhido" | |
/> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
//define o layoutInflater para desenhar o layout de cada linha da lista | |
LayoutInflater inflater = LayoutInflater.from(mContext); | |
TextView textView=null; | |
if(convertView==null) | |
{ | |
//desenha a view da linha da lista passando o ficheiro de layout | |
convertView = inflater.inflate(R.layout.linha_lista_layout,parent,false); | |
//encontra o textView pelo seu Id | |
textView = (TextView)convertView.findViewById(R.id.nome); | |
} | |
//faz o set do nome ao textview | |
textView.setText(listaNomes[position]); | |
//faz return da view para a linha da posicao | |
return convertView; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package maputo.gdg.listaeadapters.fragments; | |
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.Fragment; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ListView; | |
import maputo.gdg.listaeadapters.R; | |
import maputo.gdg.listaeadapters.adapters.ListaAdapter; | |
/** | |
* Created by Mac on 2/25/2015. | |
*/ | |
public class FragmentLista extends Fragment { | |
private ListView mLista; | |
private ListaAdapter adapter; | |
private String[] listaNomes= | |
{ | |
"Dario ICS", | |
"Paulo HoneyComb", | |
"Mariano Froyo", | |
"Martin Jelly Bean", | |
"Edson Kit", | |
"Marry Kat", | |
"Paula LoliPop", | |
"John Doe", | |
"Chuck Norris" | |
}; | |
@Override | |
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
//Desenhamos uma nova view utilizando o inflater e passando o ficheiro de layout | |
View view = inflater.inflate(R.layout.lista_fragment_layout,container,false); | |
//mapeamos a instancia do listview ao widget no xml pelo seu id | |
//lembre-se que se fizer (ListView) findViewById nao ira funcionar pois este metodo pertence a uma view | |
mLista = (ListView)view.findViewById(R.id.listView); | |
//definir o adapter para a lista | |
mLista.setAdapter(adapter); | |
//fazer o return da view | |
return view; | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
adapter = new ListaAdapter(getActivity().getApplicationContext()); | |
adapter.setData(listaNomes); | |
} | |
@Override | |
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package maputo.gdg.listaeadapters.fragments; | |
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.Fragment; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ListView; | |
import maputo.gdg.listaeadapters.R; | |
/** | |
* Created by Mac on 2/25/2015. | |
*/ | |
public class FragmentLista extends Fragment { | |
private ListView mLista; | |
@Override | |
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
//Desenhamos uma nova view utilizando o inflater e passando o ficheiro de layout | |
View view = inflater.inflate(R.layout.lista_fragment_layout,container,false); | |
//mapeamos a instancia do listview ao widget no xml pelo seu id | |
//lembre-se que se fizer (ListView) findViewById nao ira funcionar pois este metodo pertence a uma view | |
mLista = (ListView)view.findViewById(R.id.listView); | |
//fazer o return da view | |
return view; | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
} | |
@Override | |
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package maputo.gdg.listaeadapters.fragments; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.Fragment; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.AdapterView; | |
import android.widget.ListView; | |
import maputo.gdg.listaeadapters.R; | |
import maputo.gdg.listaeadapters.activity.Segunda; | |
import maputo.gdg.listaeadapters.adapters.ListaAdapter; | |
/** | |
* Created by Mac on 2/25/2015. | |
*/ | |
public class FragmentLista extends Fragment { | |
private ListView mLista; | |
private ListaAdapter adapter; | |
private String[] listaNomes= | |
{ | |
"Dario ICS", | |
"Paulo HoneyComb", | |
"Mariano Froyo", | |
"Martin Jelly Bean", | |
"Edson Kit", | |
"Marry Kat", | |
"Paula LoliPop", | |
"John Doe", | |
"Chuck Norris" | |
}; | |
private static final String CHAVE_NOME="nome"; | |
@Override | |
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
//Desenhamos uma nova view utilizando o inflater e passando o ficheiro de layout | |
View view = inflater.inflate(R.layout.lista_fragment_layout,container,false); | |
//mapeamos a instancia do listview ao widget no xml pelo seu id | |
//lembre-se que se fizer (ListView) findViewById nao ira funcionar pois este metodo pertence a uma view | |
mLista = (ListView)view.findViewById(R.id.listView); | |
//definir o adapter para a lista | |
mLista.setAdapter(adapter); | |
//definir o listener de clique a lista | |
mLista.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
//pega o item do adapter na posicao passada | |
String nome = (String) parent.getItemAtPosition(position); | |
//cria intent e passa argumentos | |
Intent i = new Intent(getActivity(), Segunda.class); | |
i.putExtra(CHAVE_NOME,nome); | |
startActivity(i); | |
} | |
}); | |
//fazer o return da view | |
return view; | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
adapter = new ListaAdapter(getActivity().getApplicationContext()); | |
adapter.setData(listaNomes); | |
} | |
@Override | |
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<ListView | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:id="@+id/listView" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package maputo.gdg.listaeadapters.adapters; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.TextView; | |
import maputo.gdg.listaeadapters.R; | |
/** | |
* Created by Mac on 2/25/2015. | |
*/ | |
public class ListaAdapter extends BaseAdapter { | |
private Context mContext; | |
private String[] listaNomes; | |
public ListaAdapter(Context context) | |
{ | |
mContext = context; | |
} | |
public void setData(String[] nomes) | |
{ | |
listaNomes = nomes; | |
} | |
@Override | |
public int getCount() { | |
//fazemos o return do tamanho do nosso array de nomes | |
return listaNomes.length; | |
} | |
@Override | |
public Object getItem(int position) { | |
//fazemos o return do nome em uma certa posicao da lista | |
return listaNomes[position]; | |
} | |
@Override | |
public long getItemId(int position) { | |
return 0; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
//define o layoutInflater para desenhar o layout de cada linha da lista | |
LayoutInflater inflater = LayoutInflater.from(parent.getContext()); | |
ViewHolder viewHolder = null; | |
if(convertView==null) | |
{ | |
//desenha a view da linha da lista passando o ficheiro de layout | |
convertView = inflater.inflate(R.layout.linha_lista_layout,parent,false); | |
viewHolder = new ViewHolder(convertView); | |
//guardamos a referencia ao viewholder e nao temos de re-fazer o findViewById sempre | |
convertView.setTag(viewHolder); | |
} | |
else | |
{ | |
//pegamos o objecto do view holder | |
viewHolder = (ViewHolder) convertView.getTag(); | |
} | |
//faz o set do nome ao textview directament do view holder | |
viewHolder.textView.setText(listaNomes[position]); | |
//faz return da view para a linha da posicao | |
return convertView; | |
} | |
//View Holder | |
public class ViewHolder | |
{ | |
public TextView textView; | |
public ViewHolder(View view) | |
{ | |
textView = (TextView) view.findViewById(R.id.nome); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package maputo.gdg.listaeadapters.activity; | |
import android.support.v7.app.ActionBarActivity; | |
import android.os.Bundle; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.widget.TextView; | |
import maputo.gdg.listaeadapters.R; | |
public class Segunda extends ActionBarActivity { | |
private TextView textView; | |
private static final String CHAVE_NOME="nome"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_segunda); | |
textView = (TextView) findViewById(R.id.nome_escolhido); | |
String nome = getIntent().getStringExtra(CHAVE_NOME); | |
textView.setText("O nome escolhido foi: "+nome); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_segunda, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment