Last active
November 27, 2015 19:33
-
-
Save Felipe00/c976c85cce24407cbb17 to your computer and use it in GitHub Desktop.
Como implementar uma ListView e um BaseAdapter.
This file contains hidden or 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
public class MainActivity extends Activity { | |
List<String> listaBox; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
listaBox = new ArrayList<String>(); | |
/** | |
* Povoa a listaBox com dados aqui. | |
*/ | |
// Pega a ListView do Layout | |
ListView lv = (ListView) findViewById(R.id.list1); | |
// Instancia seu Adapter passado o Context (this) e a lista povoada (listaBox) | |
SeuAdapter adapter = new SeuAdapter(this, listaBox); | |
// Seta o adapter na listview | |
lv.setAdapter(adapter); | |
// Setando o clique do item da lista | |
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
Intent intent = new Intent(MainActivity.this, Main2Activity.class); | |
startActivity(intent); | |
} | |
}); | |
} | |
} |
This file contains hidden or 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
public class SeuAdapter extends BaseAdapter { | |
LayoutInflater layoutInflater; | |
List<String> list; | |
Boolean promo; | |
// Construtor recebendo o contexto e a lista | |
public SeuAdapter(Context c, List<String> boxs) { | |
layoutInflater = layoutInflater.from(c); | |
list = boxs; | |
} | |
// Conta os itens da lista | |
@Override | |
public int getCount() { | |
return list.size(); | |
} | |
// Retorna o item da lista | |
@Override | |
public Object getItem(int position) { | |
return list.get(position); | |
} | |
// Pega o id do item da lista. No meu caso a lista não tem Id então eu passei 1l | |
// Passem algo assim no de vocês: list.get(position).getId(); | |
@Override | |
public long getItemId(int position) { | |
return 1l; | |
} | |
// Cria o layout de cada item da lista | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
// Inflando o Layout (da linha) | |
View rview = layoutInflater.inflate(R.layout.item, parent, false); | |
// Pegando as Views do layout (da lina) | |
TextView tv1 = (TextView) rview.findViewById(R.id.tv1); | |
TextView tv2 = (TextView) rview.findViewById(R.id.tv2); | |
TextView tv3 = (TextView) rview.findViewById(R.id.tv3); | |
// Passando dados pra eles | |
tv1.setText(list.get(position)); | |
tv2.setText("2º andar"); | |
tv3.setText("Temos promoção!"); | |
// Ao final, retorne o Layout inflado. | |
return rview; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment