【发布时间】:2015-05-15 10:16:29
【问题描述】:
我在 FormActivity.java 中创建了一个本地 SQLite 数据库,如下所示:
db = new SQLiteHandler(getApplicationContext());
String name = user.getString("name");
String surname = user.getString("surname");
String title= user.getString("title");
db.addUser(name, surname, title);
然后我有一个带有多个文本字段的片段。我希望用我在活动中创建的数据库中的数据填充这些字段。如下:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_first, container, false);
//set inputs for fields
inputTitle = (EditText) v.findViewById(R.id.titleText);
inputName = (EditText) v.findViewById(R.id.foreText);
inputSurname = (EditText) v.findViewById(R.id.surnameText);
TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
tv.setText(getArguments().getString("msg"));
return v;
// Fetching user details from sqlite
HashMap<String, String> user = db.getUserDetails();
String name = user.get("name");
String surname = user.get("surname");
// Displaying the user details on the screen
inputName.setText(name);
inputSurname.setText(surname);
inputTitle.setText(title);
}
我目前的问题是 db.getUserDetails(),我不知道也找不到如何从片段中访问数据库。 我敢肯定这是一个简单的修复,但有人可以把我安排在这里吗? 谢谢。
【问题讨论】:
标签: android sqlite android-fragments