从数据库中获取数据到 LisFragment。我需要使用这个 ListFragment 文件内容到 MainActivity.java 中。
扩展 ListFragment 的 SecondActivity:
String DB = "TestDB";
String TABLE_NAME = "addcamera";
SQLiteDatabase sampleDB = null;
ArrayList<String> results = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sampleDB = SQLiteDatabase.openOrCreateDatabase(DB, null);
Cursor c = sampleDB.rawQuery("SELECT CameraName FROM " + TABLE_NAME , null);
if (c != null) {
if (c.moveToFirst()) {
do {
String CameraName = c.getString(c.getColumnIndex("CameraName"));
results.add(CameraName);
}while (c.moveToNext());
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, results);
}
主.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.exercise.FragmentTest.SecondActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image_list_fragment">
</fragment>
</LinearLayout>
谁能帮我?