我有一个应用程序,它从在线XML文件中解析XML并将其显示在列表视图中。然后,用户可以单击某个项目并查看完整故事。这一切都运行正常,但我希望用户能够保存某些文章,以便他们可以在以后或离线时阅读它们。我认为这很简单,但我找不到任何东西引导我朝着正确的方向前进。以下是显示单篇新闻文章的活动。
public class SingleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
// getting intent data
Intent in = getIntent();
// Get XML values from previous intent
String title = in.getStringExtra("name");
String date = in.getStringExtra("date");
String content = in.getStringExtra("content");
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblDate = (TextView) findViewById(R.id.date_label);
TextView lblCont = (TextView) findViewById(R.id.content_label);
lblName.setText(title);
lblDate.setText(date);
lblCont.setText(Html.fromHtml(content));
}
}
答案 0 :(得分:2)
您可以使用SQLite数据库。为用户提供选项以保存。您可以使用按钮或图像按钮或任何可以引发事件的按钮。当引发事件时,只需将它们保存到数据库。如果您对数据库不满意,请获取示例here。 您修改后的代码将如下所示。
public class SingleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_list_item);
// getting intent data
Intent in = getIntent();
// Get XML values from previous intent
String title = in.getStringExtra("name");
String date = in.getStringExtra("date");
String content = in.getStringExtra("content");
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblDate = (TextView) findViewById(R.id.date_label);
TextView lblCont = (TextView) findViewById(R.id.content_label);
lblName.setText(title);
lblDate.setText(date);
lblCont.setText(Html.fromHtml(content));
Button b = (Button) findViewById(R.id.savebutton);
b.setonclicklistsener() {
saveToDB(lblName's text, lblDate's text, lblCont's text);
}
}
public void saveToDB(lblName's text, lblDate's text, lblCont's text){
// do database stuff here
}
}
答案 1 :(得分:0)