I would like to know how to save multiple positions in a listView. As an example, let the user bookmark certain items in the list.
I do not know what would be useful to get an efficient answer, so I'll post some important parts that exemplify how my code works:
ListView in fragment
ItensAdapter meuCustomAdapter = new ItensAdapter(getActivity()
.getApplicationContext(),
Lista.Itens.setItens());
lista.setAdapter(meuCustomAdapter);
lista.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
//Aqui eu iria salvar mudando a cor de fundo do item
}
});
Adapter
public class ItensAdapter extends BaseAdapter {
private List<Info> informacoes;
private Context contexto;
private Typeface fonte;
public ItensAdapter(Context contexto, List<Info> informacoes) {
this.contexto = contexto;
this.informacoes = informacoes;
}
@Override
public int getCount() {
return informacoes.size();
}
@Override
public Object getItem(int position) {
return informacoes.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView titulos = new TextView(contexto);
titulos.setTypeface(fonte);
titulos.setTextSize(18);
titulos.setGravity(Gravity.CENTER);
titulos.setPadding(8, 48, 8, 48);
titulos.setText(informacoes.get(position).getNome());
return titulos;
}
}
Modified class Info
public class Info {
private String nome;
public Info(String nome) {
this.nome = nome;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
Class List
public class Lista {
static Context context;
// TODO
public static class Itens {
public final static Info ob1 = new Info(
"Item 1");
public final static Info ob2 = new Info(
"Item 2");
public final static Info ob3 = new Info(
"Item 3");
public final static Info ob4 = new Info(
"Item 4");
public final static Info ob5 = new Info(
"Item 5");
public static List<Info> setItens() {
final List<Info> itens = new ArrayList<Info>();
itens.add(ob1);
itens.add(ob2);
itens.add(ob3);
itens.add(ob4);
itens.add(ob5);
return itens;
}
}