SaturnPro
SaturnPro

Reputation: 39

How to send a value from TextView to RecyclerView in another Activity

I'm trying to send a value from TextView to RecyclerView (in an another Activity).

MainActivity.class There is i want to use a method onClickYes for send each of word to my recyclerView in an another activity.

public class MainActivity extends AppCompatActivity {
TextView display, progress;
private List<String> worldList;
private ArrayList<RecyclerItems> recyclerItems;
private WordDataBase wordDatabaseForYes;
int counter = 1;
int i = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    display = findViewById(R.id.dispalay);
    progress = findViewById(R.id.progress);
    recyclerItems = new ArrayList<>();
    worldList = new ArrayList<>();
    worldList.add("cat");//i want to display this word firstly
    worldList.add("dog");//it after click
    worldList.add("monkey");//after it
    worldList.add("bird");//after it
    worldList.add("fish");//etc
    worldList.add("home");//etc
    worldList.add("car");//etc
}

public void onClickYes(View view) {
    if (i >= worldList.size()) return;
    display.setText(worldList.get(i));
    progress.setText(counter + "");
    //заполняем наш recyclerView
    //Intent intent  = new Intent(MainActivity.this, YesActivity.class);
    //intent.putExtra("word", user + ", вам передали: " + gift);
    //startActivity(intent);
    i++;
    counter++;
}

public void onClickNo(View view) {
    Toast.makeText(this, "Size is " + worldList.size(), Toast.LENGTH_SHORT).show();
    //recyclerItems.add(new RecyclerItems(worldList.get(i)));
    i = 0;
    counter = 0;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    Intent intent;
    switch (item.getItemId()) {
        case R.id.yes:
            recyclerItems.add(new RecyclerItems(worldList.get(i)));
            intent = new Intent(MainActivity.this, YesActivity.class);
            startActivity(intent);
            break;
        case R.id.no:
            intent = new Intent(MainActivity.this, NoActivity.class);
            startActivity(intent);
            break;
        case R.id.about:
            Toast.makeText(this, "By SaturnPRO", Toast.LENGTH_SHORT).show();
            //openDialogAbout
            break;
    }
    return true;
}

}

My Second Activity

public class YesActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;
    ArrayList<RecyclerItems> yesWordList;

    private WordDataBase wordDatabaseForYes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        yesWordList = new ArrayList<>();
        recyclerView = findViewById(R.id.rvYes);
        recyclerView.setHasFixedSize(true);
        adapter = new RecyclerViewAdapter(yesWordList);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(layoutManager);


    }
}

Also i have an Adapter but i couldn't show it here.

How can i do it? enter code here Please help.

Upvotes: 0

Views: 66

Answers (2)

SoftwareGuy
SoftwareGuy

Reputation: 1479

Send the word in intent extras:

case R.id.yes:
    recyclerItems.add(new RecyclerItems(worldList.get(i)));
    intent = new Intent(MainActivity.this, YesActivity.class);
    intent.putExtra("key", worldList.get(i));
    startActivity(intent);
    break;

In your second activity:

String name = intent.getStringExtra("key");

Upvotes: 1

Simran Sharma
Simran Sharma

Reputation: 892

You can make the wordList a static member. Then you can access it in your adapter.

Upvotes: 1

Related Questions