Comments (2)
Change your class as:
class MainActivity : AppCompatActivity() {
private val wordViewModel: WordViewModel by viewModels {
WordViewModelFactory((application as WordsApplication).repository)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)
val adapter = WordListAdapter()
recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(this)
wordViewModel.allWords.observe(this) { words ->
// Update the cached copy of the words in the adapter.
words?.let { adapter.submitList(it) }
}
val fab = findViewById<FloatingActionButton>(R.id.fab)
fab.setOnClickListener {
val intent = Intent(this@MainActivity, NewWordActivity::class.java)
onActivityResultLauncher.launch(intent)
}
}
// Declare the activity result launcher
private val onActivityResultLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == Activity.RESULT_OK) {
// Handle the result
val data: Intent? = result.data
data?.getStringExtra(NewWordActivity.EXTRA_REPLY)?.let {
val word = Word(it)
wordViewModel.insert(word)
}
} else {
Toast.makeText(
applicationContext,
R.string.empty_not_saved,
Toast.LENGTH_LONG
).show()
}
}
}
from codelab-android-room-with-a-view.
The replacement for startActivityForResult is registerForActivityResult which uses a different interface. As a new app developer (from old school programming) it is not clear how to implement the newer interface.
Change your class (in Java) as
class MainActivity : AppCompatActivity() {}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recyclerview);
final WordListAdapter adapter = new WordListAdapter(new WordListAdapter.WordDiff());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mWordViewModel = new ViewModelProvider(this).get(WordViewModel.class);
mWordViewModel.getAllWords().observe(this, words -> {
//Update the cached copy of the words in the adapter.
adapter.submitList(words);
});
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(view -> {
Intent intent = new Intent(MainActivity.this, NewWordActivity.class);
newWordActivityResultLauncher.launch(intent);
});
}
// Declare the activity result launcher
ActivityResultLauncher<Intent> newWordActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if(result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
Word word = new Word(data.getStringExtra(NewWordActivity.EXTRA_REPLY));
mWordViewModel.insert(word);
} else{
Toast.makeText(
getApplicationContext(),
R.string.empty_not_saved,
Toast.LENGTH_LONG).show();
}
}
}
);
from codelab-android-room-with-a-view.
Related Issues (20)
- Typo in What is the DAO?
- Code error in Add XML Layout ?
- When i execute the program, it's giving Caused by: org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$ArtifactResolveException
- error occured in add dependencies in android studio HOT 1
- error on adding update version
- Execution failed for task ':app:kaptDebugKotlin'.
- When I use Mac with M2 chips, room with the version in Tutorial cannot work well.
- Simple typo toLiveData
- code error in 16. Connect with the data
- Typo in 10. Create the ViewModel
- DELETE query methods must either return void or int (the number of deleted rows) HOT 1
- Step 3. Screenshot and description need to be updated HOT 1
- Room Database
- NewWordActivity using TextUtils
- errors after the changes from the example from android with java
- Using stateflow instead of livedata
- Android : IllegalStateException while doing DB operations
- Unresolved reference: kotlin_version HOT 1
- java.lang.ClassNotFoundException
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from codelab-android-room-with-a-view.