Object list for another Activity

Asked

Viewed 430 times

0

I’m trying to save a list of objects with sharedpreference and gson. And the idea is that I can upload this list to a Listview, I ran the tests and when it goes to listview it only loads the packages, photos: Choose Products - FINAL CANVAS

Button Saving Information with Sharedpreference.

btnFinalizar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent myIntent = new Intent(getApplicationContext(),FinalPedidoActivity.class);

            SharedPreferences pref = getApplicationContext().getSharedPreferences(PREFS_PRODUTO_NAME, MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();

            Gson gson = new Gson();
            String json = gson.toJson(list);

            editor.putString("produtosJSON", json);
            editor.commit();

            startActivity(myIntent);
        }
    });

Load Recyclerview with Firebase information, using Firebaseui - And place the object in a List:

public void setupRecycler(){


    adapter = new FirebaseRecyclerAdapter<Produto, ProdutoHolder>(
            Produto.class,
            R.layout.recyclerview_items,
            ProdutoHolder.class,
            ref
    ) {
        @Override
        protected void populateViewHolder(final ProdutoHolder viewHolder, final Produto model, final int position) {
            viewHolder.setId(model.getId().toUpperCase());
            viewHolder.setNome(model.getNome());
            viewHolder.setDescricao(model.getDescricao());
            viewHolder.setTamanho(model.getTamanho());
            viewHolder.setQuantidade(model.getQuantidade());
            viewHolder.setValor(model.getValor());


            viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                @Override

                public void onClick(View v) {
                    if (SystemClock.elapsedRealtime() - lastClickTime < 1500){
                        viewHolder.itemView.setBackgroundColor(Color.RED);

                        return;
                    }
                    lastClickTime = SystemClock.elapsedRealtime();
                    viewHolder.itemView.setBackgroundColor(Color.GREEN);

                    Produto produto = new Produto();

                    produto.setNome(model.getNome());
                    produto.setId(model.getId());
                    produto.setDescricao( model.getDescricao());
                    produto.setTamanho(model.getTamanho());
                    produto.setQuantidade(model.getQuantidade());
                    produto.setValor(model.getValor());

                    list.add(produto);

                }
            });

        }
    };

    recyclerView.setAdapter(adapter);
}

Upload information and place in Listview:

SharedPreferences pref = getApplicationContext().getSharedPreferences(PREFS_PRODUTO_NAME, MODE_PRIVATE);
    SharedPreferences.Editor editor = pref.edit();
    Gson gson = new Gson();
    String json = pref.getString("produtosJSON", "");
    Type type = new TypeToken<List<Produto>>() {
    }.getType();
    list = gson.fromJson(json, type);

    listView = (ListView) findViewById(R.id.listViewProdutosFinal);

    //adapter
    ArrayAdapter<Produto> adapter = new ArrayAdapter<Produto>(this,android.R.layout.simple_list_item_1,list);
    listView.setAdapter(adapter);

What I need is to show the product information, but it is only loading the package that should be the variable, I am in doubt if I am on the right track, I am trying various ways to do this.

I tested using Intent, I saved the object Product in ArrayList<Produto> and spent using myIntent.putExtra("LIST", (Serializable) list); and then I charge list = (List<Produto>) i.getSerializableExtra("LIST"); and put on Listview :

ArrayAdapter<Produto> adapter = new ArrayAdapter<Produto>(this,android.R.layout.simple_list_item_1,list);
        listView.setAdapter(adapter);

That way the same thing as before, only loads the package in listview.

1 answer

1


As you are doing, Arrayadapter by default writes the representation of the object returned by the toString method().

Since the toString() method was not overrided, it will write the package name. Implement toString() in the Product class to return an object view, so it will work.

Example of the same:

@Override
public String toString() {
    return getNome();
}
  • Man, it really, really worked. Another question I have is that if I need to get other information, such as getValor(), getId(), to use in the other Activity..., how could I do this?

  • In this case, you have to create an Adapter extending a base, take a look at that link here of the OS that has an Adapter implemented, will already give you a notion.

  • But then you better use Recyclerview?

  • 1

    The right thing to do is to use recyclerview from the beginning, as it is more modular and more optimized. I recommend a read in the Google documentation even if there you can quickly pick up how it works.

Browser other questions tagged

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