How to modify and save the new modified listview item?

Asked

Viewed 675 times

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.

  • @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.

  • 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.

  • @Lucassantos I would prefer it to be with a long click even

  • Okay. I get it. It takes a long click and highlights the background color to know that it’s favorite.

  • @Lucassantos That’s right

  • 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.

  • 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?

  • 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?

  • @Lucassantos yes, the background is quiet, the problem is to make the code remember the positions

Show 5 more comments

2 answers

1

An option would be to retrieve the items via the method get of lisView and write the data to a file Shared Preference 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 recover the information in the Shared Preference file and click on the list.

  • That way I didn’t know. That works?

  • But then, if I save the string from the list and then retrieve it, wouldn’t there be duplication? There is no way to save just the position and refer to it in the list?

  • Da yes, instead of saving the favorite string, saves the position int position = listView.getIdItemSelected();

  • ta, but my list has more than one item.. how would I save in Shared preferences 3 items, and then retrieve them in one onResume() for example? there would have to be an Array of Integers... understand where I’m going?

  • The ideal would be a table in the database called "favorite" there vc recovered the id of the listview position. But if you want to do it in the mayor’s office. editor.putInt(KEY, favorito); editor.putInt(KEY, favorito2); editor.putInt(KEY, favorito3);

0

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

  • a list of all items that appear on listView;
  • another list of selected items (lista_selec).

Then every time I click CheckBox (true) add the item to lista_selec.

In other words, in your onItemClick just check for each object of the selected position whether it is active or not.
If it is active, as clicked will pass the deactivative (removes from lista_selec), if it is deactivated when clicking is active (you add the lista_selec).

To lista_selec just to keep track of the selected fields and then write to the BD. That’s what you wanted right?

  • Then I would have to have two lists in one layout, and the list of selected will be the part?

  • No, have your list lista right? then you would have a NORMAL list, with the selected items, to record in the BD.

  • I’m sorry my dear, but I couldn’t understand your reply.

  • Your list: lista.setAdapter(meuCustomAdapter); is a list of what? Show me the statement of that list.

  • Why don’t you use a Set with the indexes of the selected items. It would be efficient to use a HashSet to validate whether a particular item in Adapter is selected. Another suggestion is to look at the source code of ListView, he treats selection (choiceMode), and for that he uses a SparseArray with the indexes.

  • @Wakim I thought about doing this, but then how would I relate the position to the boolean?

  • The keys to the SparseArray (use the method keyAt to know the position) would be the positions of the Adapter that are selected. I did it already, but at the moment I can’t mount a response.

  • 1

    I think it’s easier to have a list of selected items in your code. Whenever someone clicks on the checkbox, either adds to the selected de_itens_list, or removes, as true or false to checkbox.

  • I think I understand.. and in this case, I compare the two lists, and the same items I would identify as selected. That’s right?

  • See my answer now, I’ve edited.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.