[英]Android - Add to ListView depending on which button is pressed
我正在制作一個應用程序,其中一部分是向列表視圖添加注釋(這是一個簡單的膳食計划器應用程序)。 我一直在學習有關Lynda的課程,以將其實施到我的項目中( 構建適用於Android的筆記應用程序(2013年) )。
我的活動和Java類中都有3個ListViews和3個Button。 Java類中的ListView命名為mLBMon, mLLMon, mLDMon
(B,L,D表示早餐等,L表示ListView,Mon表示星期一)。 並且Button使用XML中的OnClick android:onClick="onClick".
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);
}
當用戶返回主頁時,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();
}
}
refreshDisplay()
:
private void refreshDisplay() {
mealsList = datasource.findAll();
ArrayAdapter<MealItem> adapter =
new ArrayAdapter<MealItem>(this, R.layout.list_item_layout, mealsList);
mLBMon.setAdapter(adapter);
}
似乎mLBMon.setAdapater(adapter)
確定將其顯示在哪個ListView上,就像我將其更改為顯示在ListView上的mlLMon或mlDMon一樣。
所以我想做的是當單擊另一個按鈕時,它顯示在“鏈接到”該按鈕的ListView上:
單擊 MondayLunchButton->用戶在編輯器中做筆記並按回-> MondayLunchListView已更新
等等
如果有人可以引導我朝正確的方向發展,那將是非常有幫助的,如果需要,我很樂意提供更多代碼。 這是我第一次使用Android,並且正在嘗試學習它,因此我決定做一個項目,因此如果我沒有提供足夠的信息,我們感到抱歉。 謝謝。
對於每個按下的按鈕,向MealEditor
啟動意圖發送不同的request code
。 當它返回時,您將獲得完全相同的ID。 使用此ID設置條件並相應地更新ListView
。
做這個
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);
}
然后
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);
}
}
最后
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);
}
注意:更好的保持等的值1001
, 1002
和1003
在一個int常量如CODE_BREAKFAST
, CODE_LUNCH
, CODE_DINNER
; 這樣您就不會錯誤地弄亂if-else
條件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.