0
I would like to know how to save multiple positions from a listview. As an example, let the user bookmark certain list items.
I don’t know what would be useful to get an efficient response, so I will post some important parts that exemplify how my code works:
Listview on the 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;
}
}
List Class
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;
}
}
How do you want to save these items from the list? Do you want to save these items in such a way that you exit the application and when you re-enter, the saved items are there? I saw that it is a list of Info objects containing only correct name? Please try to explain better and answer these questions I asked so I can try to help you, otherwise I won’t be able to because I don’t quite understand your problem.
– Lucas Santos
@Lucassantos I want to save that way anyway, so that I exit the application and the chosen item as favorite is still there marked. For now Info has only names, because it is as study, I will still add email and phone.
– Guilherme Ramos
I think I get it, you have a list, on that list each item has a name and a Checkbox to mark it as favorite or not. If you mark as favorite, when exiting the app and back again, when loading the list, the checkboxes must be checked. Is that right or have I made a mistake? If that’s what I’m going to try to come up with an answer for you later, not just now, as soon as possible.
– Lucas Santos
@Lucassantos I would prefer it to be with a long click even
– Guilherme Ramos
Okay. I get it. It takes a long click and highlights the background color to know that it’s favorite.
– Lucas Santos
@Lucassantos That’s right
– Guilherme Ramos
you can try to follow what this link says: http://tuohuang.info/android-listview-remember-selection-and-set-default-selection/#. U8hwjvldxdq To highlight the selected item. To save the status I recommend to use Sharedpreferences as the friend Jorge B. said.
– Lucas Santos
So Lucas, my difficulty is being in the logic of sharedPreferences. Because in theory, I would have to make an array of integers... Or not?
– Guilherme Ramos
I think Sharedpreferences does not save array. You have to view the methods of the Sharedpreferences class and see what you can save. The highlight of the fund you have already managed to make?
– Lucas Santos
@Lucassantos yes, the background is quiet, the problem is to make the code remember the positions
– Guilherme Ramos