Listview in another Activity

Asked

Viewed 200 times

1

I have a Listview in my activity main when I click a button, it adds data to my Listview, so far everything is correct.

How do I show this listview in another activity? I show other data (EditText) no problem, but listview I couldn’t.

I tried to use it to send the data :

 public void EnviarParaResultadoActivity(View view){

    EditText resultadoID = (EditText) findViewById(R.id.resultadoID);
    EditText nomesorteioID = (EditText) findViewById(R.id.nomesorteioID);
    ListView listView1 = (ListView)findViewById(R.id.listView1);

    Bundle parametro = new Bundle();

    parametro.putString("resultadoID", resultadoID.getText().toString());
    parametro.putString("nomesorteioID", nomesorteioID.getText().toString());
    parametro.putString("listView1", listView1.getContext().toString());

    Intent intent = new Intent(this,   ResultadoActivity.class);
    intent.putExtras(parametro);

    startActivity(intent);      
}

and to receive the data:

public void ReceberParametros() {       

        Intent intent = getIntent();
        if(intent != null){         
            Bundle parametros = intent.getExtras();
            if(parametros != null){             
              String resultadoID = parametros.getString("resultadoID");
              String nomesorteioID = parametros.getString("nomesorteioID");
              String listview1 = parametros.getString("listview1");

              TextView ResultadoTV =   (TextView)findViewById(R.id.resultadoID); 
              TextView NomesorteioTV = (TextView) findViewById(R.id.nomesorteioID); 
              ListView listviewLV = (ListView) findViewById(R.id.listView1);    


              ResultadoTV.setText(" "+resultadoID);
              NomesorteioTV.setText(" "+nomesorteioID);
              listviewLV.setFilterText(" "+listview1);

            }
        }
    }

The data of TextView They are displayed normally, but Listview no. someone can help me?

1 answer

1

To send the data the correct is to set the values in a class (model) and send with the putExtra():

Intent it = new Intent(this, ResultadoActivity.class);
it.putExtra("objeto", classModel);
startActivity(it);

And to catch:

ClassModel cm = (ClassModel) getIntent().getSerializableExtra("objeto");

And the ClassModel must have implements Serializable:

public class ClassModel implements Serializable {
    //Código...
}

To set the data in ListView:

ArrayList<ClassModel> array = cm.getArray(); //cm.getArray deve conter o array de dados que vão ser setados no listView
ArrayAdapter<ClassModel> adapter = new ArrayAdapter<>(SegundaActivity.this, android.R.layout.simple_list_item_1, array);
listview1.setAdapter(adapter);
  • How do I do it? I’m sorry, I’m a beginner and I didn’t get it right

  • You know what it is MVC?

  • I know, model, view, controller

  • Well, when you want to pass multiple data between activitys, instead of sending multiple String, int, Boolean..., you can set all the data in a model class and send that class (its instance), basically you create a new instance Classe c - new Classe();, arrow the values you want to pass with set and send with Intent.puExtra(), and I believe the way you’re setting your listview is wrong

Browser other questions tagged

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