-1

I am new to Android development. I have an app in which the user creates multiple names (as if they were players of a game). These "players" appear as a matrix is used in the same activity. (Being possible here to exclude any player).

I want to display all of these players (MainActivity) in another activity (Main2Activity), showing only the first player added and, clicking a button, switch to the second player.

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

RecyclerView recyclerView;
TextView textAdd;
EditText etAdd;
ArrayList<Model> models = new ArrayList<Model>();
MyAdapter myAdapter;

int position;

Button prox;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    prox = findViewById(R.id.prox);
    prox.setOnClickListener(new View.OnClickListener() {
        //next screen
        public void onClick(View v) {

             Intent intent = new Intent(MainActivity.this, Main2Activity.class);
            String nome = etAdd.getText().toString();
            intent.putExtra("value", nome);
            startActivity(intent);
        }
    });

    recyclerView = findViewById(R.id.recyclerView);
    textAdd = findViewById(R.id.text_adicionar);
    etAdd = findViewById(R.id.et_Adicionar);

    recyclerView.setHasFixedSize(true);

    recyclerView.setLayoutManager(new GridLayoutManager(this, 3));

    myAdapter = new MyAdapter(getApplicationContext(),
            models, new MyAdapter.Onclick() {
        @Override
        public void onEvent(Model model, int pos) {
            position = pos;
            etAdd.setText(model.getId());
        }
    });
    recyclerView.setAdapter(myAdapter);
    textAdd.setOnClickListener(this);
}
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.text_adicionar: {
            insertItem(String.valueOf(etAdd.getText()));


        }
        break;
    }
}

private void insertItem(String name) {

    Gson gson = new Gson();
    try {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", name);
        Model model = gson.fromJson(String.valueOf(jsonObject), Model.class);
        models.add(model);
        myAdapter.notifyDataSetChanged();

    } catch (JSONException e) {
        e.printStackTrace();
    }

}

}

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {


Button play;
TextView text_player_name;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    play = findViewById(R.id.play);
    text_player_name = findViewById(R.id.text);


    play.setOnClickListener(this);
}

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.play: {
                String name = getIntent().getExtras().getString("value");
                text_player_name.setText(String.valueOf(name));

            }
            break;
        }
    }

}
  • StackOverflow is in English. Please [edit] your question and translate it into English. – Johannes Kuhn Nov 22 '19 at 19:35
  • Possible duplicate of [How to transfer data from one activity to another in android](https://stackoverflow.com/questions/20169993/how-to-transfer-data-from-one-activity-to-another-in-android) – Bö macht Blau Nov 22 '19 at 20:01
  • Perhaps there is some confusion in your question. Is the edit textfield and the RecyclerView in the same activity? Your question suggested passing between activities but your code appears to all be in the same activity. – C. Skjerdal Nov 22 '19 at 20:04
  • I updated my question, I want to transfer data between activity, but with editText being dynamically created on the first screen I have no idea how to reference them – – Sérgio Moreira Nov 22 '19 at 21:54

1 Answers1

0

I'm not sure if I fully understand your question, but are you just looking for a way to pass data from one Activity to another?

If so, use .putExtra() with your Intent for starting the next Activity.
In the onCreate method for your second Activity, use .getExtras() to retrieve the data.

In 1st Activity

Intent intent = new Intent(this, Activity2.class);
intent.putExtra("DataName", data);
startActivity(intent);

In the 2nd Activity

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);    
    String[] dataArray = getIntent().getExtras().get("DataName"));
 }

EDIT:

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {


Button play;
TextView text_player_name;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    play = findViewById(R.id.play);
    text_player_name = findViewById(R.id.text);

    play.setOnClickListener(this);
}


 @Override
 public void onClick(View v) {

     String name = getIntent().getExtras().getString("value");
     text_player_name.setText(String.valueOf(name));

 }
  • I updated my question, I want to transfer data between activity, but with editText being dynamically created on the first screen I have no idea how to reference them – Sérgio Moreira Nov 22 '19 at 21:34
  • In Activity2 implement View.OnClickListener, the same as MainActivity. Then for the button setOnClickListener(this); Then you can keep the code in your onClick method the same. The problem is, you are using an in-line class for the onClick in Activity2, so it is trying to get the getExtras() of the Button not Activity2. – GavinSkippy Nov 22 '19 at 22:27
  • The value that passes to another activity is what is in editText, I want to pass the values already entered in editText and that were added in my Array. – Sérgio Moreira Nov 22 '19 at 23:08
  • I don't understand what the problem is. Are the values not being passed to Activity2, or, are you not sure how to receive them in Activity2? – GavinSkippy Nov 24 '19 at 16:38