[英]Android - Add to ListView depending on which button is pressed
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) ).
我一直在学习有关Lynda的课程,以将其实施到我的项目中( 构建适用于Android的笔记应用程序(2013年) )。
I have got 3 ListViews and 3 Buttons on my activity and in my Java class. 我的活动和Java类中都有3个ListViews和3个Button。 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). Java类中的ListView命名为
mLBMon, mLLMon, mLDMon
(B,L,D表示早餐等,L表示ListView,Mon表示星期一)。 And the Buttons use an OnClick in the XML, android:onClick="onClick".
并且Button使用XML中的OnClick
android:onClick="onClick".
The onClick method is: onClick方法是:
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: 当用户返回主页时,ListView将更新:
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()
: 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. 似乎
mLBMon.setAdapater(adapter)
确定将其显示在哪个ListView上,就像我将其更改为显示在ListView上的mlLMon或mlDMon一样。
So what I am trying to do is when a different button is clicked, it displays on the ListView "linked" to that button: 所以我想做的是当单击另一个按钮时,它显示在“链接到”该按钮的ListView上:
MondayLunchButton clicked -> User makes a note in the editor & presses back -> MondayLunchListView updated 单击 MondayLunchButton->用户在编辑器中做笔记并按回-> MondayLunchListView已更新
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.
这是我第一次使用Android,并且正在尝试学习它,因此我决定做一个项目,因此如果我没有提供足够的信息,我们感到抱歉。 Thanks.
谢谢。
For each button pressed send a different request code
to MealEditor
launching intent. 对于每个按下的按钮,向
MealEditor
启动意图发送不同的request code
。 When it will return back you will get exactly same id. 当它返回时,您将获得完全相同的ID。 Use this id to put a condition and update your
ListView
accordingly. 使用此ID设置条件并相应地更新
ListView
。
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
; 注意:更好的保持等的值
1001
, 1002
和1003
在一个int常量如CODE_BREAKFAST
, CODE_LUNCH
, CODE_DINNER
; so that you don't mess up the if-else
condition by mistake. 这样您就不会错误地弄乱
if-else
条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.