I'm making an app and a part of it is adding a note to a listview (it's a simple meal planner app). I've been following a course on Lynda to implement it into my project ( Building a Note-Taking App for Android (2013) ).
I have got 3 ListViews and 3 Buttons on my activity and in my Java class. The ListViews in the Java class are named mLBMon, mLLMon, mLDMon
(the B,L,D meaning breakfast, etc and the L meaning ListView, Mon just being Monday). And the Buttons use an OnClick in the XML, android:onClick="onClick".
The onClick method is:
public void onClick(View v){
MealItem meal = MealItem.getNew();
Intent i = new Intent(this, MealEditor.class); // Meal Editor is the activity where the user enters the note which is displayed in the text view
i.putExtra("key", meal.getKey());
i.putExtra("text", meal.getText());
startActivityForResult(i, 1001);
}
The ListView gets updated when the user returns back to the main page:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1001 && resultCode == RESULT_OK){
MealItem meal = new MealItem();
meal.setKey(data.getStringExtra("key"));
meal.setText(data.getStringExtra("text"));
datasource.update(meal);
refreshDisplay();
}
}
The refreshDisplay()
:
private void refreshDisplay() {
mealsList = datasource.findAll();
ArrayAdapter<MealItem> adapter =
new ArrayAdapter<MealItem>(this, R.layout.list_item_layout, mealsList);
mLBMon.setAdapter(adapter);
}
It seems that the mLBMon.setAdapater(adapter)
determines which ListView it is presented on as when I change it to mLLMon or mLDMon it displays on the ListView.
So what I am trying to do is when a different button is clicked, it displays on the ListView "linked" to that button:
MondayLunchButton clicked -> User makes a note in the editor & presses back -> MondayLunchListView updated
etc.
It would be of great help if someone could guide me in the right direction and I would gladly provide more code if needed. This is my first time working with Android and I am trying to learn it so I decided to do a project so I am sorry if I have not provided enough info. Thanks.
For each button pressed send a different request code
to MealEditor
launching intent. When it will return back you will get exactly same id. Use this id to put a condition and update your ListView
accordingly.
Do this
public void onClick(View v){
MealItem meal = MealItem.getNew();
Intent i = new Intent(this, MealEditor.class); // Meal Editor is the activity where the user enters the note which is displayed in the text view
i.putExtra("key", meal.getKey());
i.putExtra("text", meal.getText());
if(v.getId() == R.id.breakfast) // use your breakfast button id here
startActivityForResult(i, 1001);
else if(v.getId() == R.id.lunch) // use your lunch button id here
startActivityForResult(i, 1002);
else if(v.getId() == R.id.dinner) // use your dinner button id here
startActivityForResult(i, 1003);
}
then
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if ((requestCode == 1001 || requestCode == 1002 || requestCode == 1003) && resultCode == RESULT_OK){
MealItem meal = new MealItem();
meal.setKey(data.getStringExtra("key"));
meal.setText(data.getStringExtra("text"));
datasource.update(meal);
refreshDisplay(requestCode);
}
}
finally
private void refreshDisplay(int code) {
mealsList = datasource.findAll();
ArrayAdapter<MealItem> adapter =
new ArrayAdapter<MealItem>(this, R.layout.list_item_layout, mealsList);
if(code == 1001)
mLBMon.setAdapter(adapter);
else if(code == 1002)
mLLMon.setAdapter(adapter);
else if(code == 1003)
mLDMon.setAdapter(adapter);
}
Note: Better to keep values like 1001
, 1002
and 1003
in a int constant such as CODE_BREAKFAST
, CODE_LUNCH
, CODE_DINNER
; so that you don't mess up the if-else
condition by mistake.
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.