How to modify and save the new modified item in a listView?

0

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;
    }

}
    
asked by anonymous 16.07.2014 / 06:26

2 answers

1

One option would be to retrieve items using the get method of lisView and write the data to a file Shared Preference from Android

 String favorito = listView.getSelectedItem();

 SharedPreferences settings = getActivity().getSharedPreferences(PREFS_NAME, 0);
 SharedPreferences.Editor editor = settings.edit();
 editor.putString(KEY, favorito);
 editor.commit();

After saving, you would only need to retrieve the information in the Shared Preference file and load it in the list.

    
16.07.2014 / 15:36
0

What I do in this case is to have two lists:

  • a list of all items that appear in listView ;
  • other list with selected items ( lista_selec ).

Then whenever I click on CheckBox ( true ) I add the item to lista_selec .

That is, there in your onItemClick is only to check for each object of the selected position whether it is active or not. If it is active, as you clicked it will go to deactivate (remove from lista_selec ), if it is deactivated when clicks is active (add% to lista_selec ).

The lista_selec only serves to keep track of the selected fields for later recording in the DB. Is that what you wanted right?

    
16.07.2014 / 13:37