Android编程相当新,并且在单击按钮时无法更改字符串值“channel”。有问题试图关闭onCreate但它似乎只让我在活动结束时关闭它。我收到'token“}的错误。”请删除'如果我试图在其他地方关闭它。我很难试图绕过这个,即使它可能非常简单。
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 :(得分:0)
我猜你想按下按钮时想要改变RSS提要。但是,由于您在onCreate中获取RSS信息,即使按下按钮,也不会更新数据。但是你正在改变字符串通道的值。
onCreate仅在您创建活动时调用一次!
尝试将try-statement移动到单独的方法中,并在两个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());
}