2013-04-08 89 views
0

對於android編程來說是新鮮事物,並且在單擊按鈕時無法更改字符串值「channel」。嘗試關閉onCreate時出現問題,但似乎只能讓我在活動結束時關閉它。我收到「令牌」}「的錯誤。請刪除'如果我嘗試在其他地方關閉它。儘管這可能非常簡單,但我仍然很難將這個問題包括在內。在OnClick中更改字符串值

public class MainActivity extends Activity { 

    String channel = "bbc1"; 

    // This method creates main application view 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     // Set view 
     setContentView(R.layout.main); 
     final ViewSwitcher switcher = (ViewSwitcher)findViewById(R.id.ViewSwitcher1); 
     Button bbcButton = (Button) findViewById(R.id.bbcButton); 
     Button bbc2Button = (Button) findViewById(R.id.bbc2Button); 



     bbcButton.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v) { 
       channel = "bbc1"; 
       switcher.showNext();  
      } 
     }); 


     bbc2Button.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v) { 
       channel = "bbc2"; 
       switcher.showNext(); 
      } 
     }); 


     try{ 
      // This line creates RSS reader 
      RssReader rssReader = new RssReader("http://bleb.org/tv/data/rss.php?ch="+channel+"&day=0"); 

      // This line gets a ListView from main view 
      ListView tvItems = (ListView) findViewById(R.id.listMainView); 

      // This line creates a list adapter 
      ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(this,android.R.layout.simple_list_item_1, rssReader.getItems()); 

      // This line sets list adapter for the ListView 
      tvItems.setAdapter(adapter); 


     } catch (Exception e) { 
      Log.e("Tv RSS Reader", e.getMessage()); 
     } 

    } 
} 
+0

從您發佈的代碼中,一切似乎都很好。最後的兩個結束'}'是'onCreate'方法,另一個是類。我對代碼進行了格式化,以便您可以更好地看到它。 – 2013-04-08 13:42:46

+1

嘗試複製完整的代碼,刪除代碼並粘貼代碼。 CTRL + A,CTRL + C,CTRL + A,DELETE,Ctrl + V。 – 2013-04-08 13:46:05

回答

0

我猜你想在按下按鈕時更改RSS提要。但是,由於您正在獲取onCreate中的RSS信息,即使按下按鈕,也不會更新數據。但是你正在改變字符串頻道的價值。

只有在創建活動時纔會調用onCreate!

嘗試將try語句移入單獨的方法,並在兩個onClickListeners中調用此方法。

bbc2Button.setOnClickListener(new OnClickListener(){ 
     public void onClick(View v) { 
      channel = "bbc2"; 
      updateRSS(); 
      switcher.showNext(); 
     } 
    }); 

    public void updateRSS(){ 
     try{ 
      // This line creates RSS reader 
      RssReader rssReader = new RssReader("http://bleb.org/tv/data/rss.php?ch="+channel+"&day=0"); 

      // This line gets a ListView from main view 
      ListView tvItems = (ListView) findViewById(R.id.listMainView); 

      // This line creates a list adapter 
      ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(this,android.R.layout.simple_list_item_1, rssReader.getItems()); 

      // This line sets list adapter for the ListView 
      tvItems.setAdapter(adapter); 


     } catch (Exception e) { 
      Log.e("Tv RSS Reader", e.getMessage()); 
    } 
+0

謝謝!這是一個巨大的幫助。現在總覺得,愚蠢的錯誤,盯着代碼太久,我想哈哈 – HotLovingCoder3 2013-04-08 13:58:51