2016-02-20 60 views
0

我正在製作一個應用程序,它的一部分是添加一個註釋到一個列表視圖(這是一個簡單的膳食計劃器應用程序)。我一直在關注琳達的一門課程,將其實施到我的項目中(構建適用於Android的筆記記錄應用程序(2013))。Android - 添加到列表視圖取決於按下哪個按鈕

我有3個ListViews和3個按鈕在我的活動和我的Java類。 Java類中的ListViews被命名爲mLBMon, mLLMon, mLDMon(B,L,D表示早餐等,L表示ListView,Mon只是星期一)。並且按鈕的XML使用一個onClick,android:onClick="onClick".的onclick方法是:

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()

當用戶返回到主頁

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控件被更新

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上,因爲當我將它更改爲mLLMon或mLDMon時,它將顯示在ListVie上W上。

所以我試圖做的是點擊不同的按鈕時,它會顯示在「鏈接」到該按鈕ListView控件:

MondayLunchButton 點擊 - >用戶做筆記編輯器&印刷機回 - > MondayLunchListView更新

這將是很大的幫助,如果有人能指導我在正確的方向,如果需要的話,我很樂意提供更多的代碼。這是我第一次使用Android,我正在學習它,所以我決定做一個項目,所以我很抱歉,如果我沒有提供足夠的信息。謝謝。

回答

1

對於按下的每個按鈕,發送不同的request codeMealEditor啓動意向。當它返回時,你會得到完全相同的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); 
} 

注:更好的保持值喜歡100110021003在INT等常數CODE_BREAKFASTCODE_LUNCHCODE_DINNER;所以你不要錯誤地搞亂if-else條件。

+0

謝謝!現在確實如此。然而,在應用程序中,當我添加「Breakfast」的註釋時,出現一個與其應該完全相同的註釋,但是當我爲「Lunch」添加註釋時,輸入的註釋出現,但我輸入的註釋「早餐」。有沒有辦法解決?感謝您的幫助,我很感激! – Hp123

相關問題