Object list for another activity

0

I'm trying to save a list of objects with sharedpreference and gson. And the idea is that I can load this list into a ListView, I did the tests and when it goes to listVIew it loads only the packages, photos: Choose Products - FINAL SCREEN

Button that saves 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 the Firebase information, using FirebaseUI - E puts 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);
}

Load information and place it in the 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 display the product information, but it's just loading the package that should be the variable, I'm in doubt if I'm on the right track, I'm trying various ways to do this.

I tried using intent, I saved the Product object to ArrayList<Produto> and I started using myIntent.putExtra("LIST", (Serializable) list); and then I loaded list = (List<Produto>) i.getSerializableExtra("LIST"); and put it in ListView:

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

This is the same thing as before, just load the package in the listView.

    
asked by anonymous 22.09.2017 / 18:23

1 answer

1

In the way you are doing, the ArrayAdapter by default writes the representation of the object returned by the toString () method.

Since the toString () method has not been overridden, it will write the package name. Implement toString () in the Product class to return a view of the object, hence it will work.

Example of it:

@Override
public String toString() {
    return getNome();
}
    
22.09.2017 / 19:24