简体   繁体   中英

I want to send some items to second activity's recycler view. How can I do it with Room

I create an easy app (i think so) with RecyclerView, LiveData, and Room. You can see the whole of my project in GitHubMyProject

I have the Main Activity and there is a word counter there with TextView, two buttons for checking words, and send it to an another activity after each of click. When I open the second activity (YesList) i see there nothing. There is empty there. How can i fix it?

SCREEN

    public class MainActivity extends AppCompatActivity {
    public static final String EXTRA_YES = "com.saturnpro.wcounter.YES";
    public static final String EXTRA_NO = "com.saturnpro.wcounter.NO";
    public static final int NEW_WORD_ACTIVITY_REQUEST_CODE = 1;
    private TextView text;
    private TextView counterTxt;
    List<String> words;
    int count = 0;
    int i = 0;
    Intent intentYes;
    Intent intentNo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = findViewById(R.id.txt);
        counterTxt = findViewById(R.id.countertext);
        words = new ArrayList<>();
        words.add("hello");
        words.add("hi");
        words.add("cat");
        words.add("dog");
        words.add("drink");
        words.add("planet");
        words.add("space");
        words.add("kind");
        text.setText(words.get(0));
        counterTxt.setText(count+"");
        intentYes = new Intent(MainActivity.this, YesList.class);
        intentNo = new Intent(MainActivity.this, NoList.class);
    }

    public void addToYesList(View view) {
        i++;
        if(i < words.size()) {
            String word = words.get(i);
            text.setText(word);
            count++;
            counterTxt.setText(count+"");
            Intent replyIntent = new Intent();
            if (TextUtils.isEmpty(word)) {
                setResult(RESULT_CANCELED, replyIntent);
            } else {
                replyIntent.putExtra(EXTRA_YES, word);
                setResult(RESULT_OK, replyIntent);
            }
        }
        else {
            text.setText("Слов больше нет");
        }
    }

    public void addToNoList(View view) {
        i++;
        if (i < words.size()) {
            String word = words.get(i);
            text.setText(word);
        }
        else {
            text.setText("Слов больше нет");
        }
    }

    public void iknow(View view) {
        startActivityForResult(intentYes, NEW_WORD_ACTIVITY_REQUEST_CODE);
    }

    public void idontknow(View view) {
        startActivity(intentNo);
    }
}

And second activity YesList.

public class YesList extends AppCompatActivity {
    private WordViewModel mWordViewModel;
    public static final int NEW_WORD_ACTIVITY_REQUEST_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yes_list);
        RecyclerView recyclerViewYes = findViewById(R.id.listYes);
        final WordListAdapter adapter = new WordListAdapter(new WordListAdapter.WordDiff());
        recyclerViewYes.setAdapter(adapter);
        recyclerViewYes.setLayoutManager(new LinearLayoutManager(this));
       // mWordViewModel = new ViewModelProvider(this).get(WordViewModel.class);

        mWordViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(WordViewModel.class);

        mWordViewModel.getAllWords().observe(this, words -> {
            // Update the cached copy of the words in the adapter.
            adapter.submitList(words);
        });

    }


    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == NEW_WORD_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
            Words word = new Words(data.getStringExtra(MainActivity.EXTRA_YES));
            mWordViewModel.insert(word);
            Log.d("proverka", mWordViewModel.toString());
        } else {
            Toast.makeText(
                    getApplicationContext(),
                    R.string.empty_not_saved,
                    Toast.LENGTH_LONG).show();
        }
    }
}

I think you can use the interface so onclick of recyclerview Item you can send the data from one activity recycler to another activity recycler and catch the data into onBindViewHolder.

Im adding code snippet so you can get some Idea,

public void onBindViewHolder(ContactsAdapter.ViewHolder holder, int position)     {
    // Get the data model based on position
    Contact contact = mContacts.get(position);

    // Set item views based on your views and data model
        TextView textView = holder.nameTextView;

        textView.setText(contact.getName());
        Button button = holder.messageButton;
        button.setText(contact.isOnline() ?     "Message" : "Offline");
        button.setEnabled(contact.isOnline());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM