我有Fragment
一个HistoryFragment
包含ListView
. 的项目ListView
由另一个名为 的类添加DataModel
。我要做的是在将项目添加到列表时保存列表。(目前,我列表中的视图只有TextView
s,但后来我希望向它们添加图像,并且有多行等,所以这些也需要保存)。我的清单是:ArrayList<Map<String, String>>
它被称为mPlanetList
.
我认为保存到 aSQLiteDataBase
是解决方案,但即使在阅读了多个答案/教程之后似乎也很复杂。
我看过的一些网站:
http://www.vogella.com/articles/AndroidSQLite/
http://developer.android.com/guide/topics/data/data-storage.html#db
http://stackoverflow.com/q/5482402/2442638
http://stackoverflow.com/q/3142285/2442638
这个“问题”中有很多问题,但我的主要问题分为两部分:
a) 是DataBase
正确的解决方案吗?
--> 如果“是”--> b) 制作一个简单的方法是什么?我宁愿不使用 JSON/GSON
--> 如果'否' --> c) 我应该用什么代替? SharedPreferences
?
假设 aDataBase
是正确的解决方案,我想我需要序列化我的 arraylist 或者可能使用 StringSet - 我不知道该怎么做,因为我有一个这样的 arraylist:ArrayList<Map<String, String>>
而不是没有 map 对象的 arraylist。
此外,我试图从我的DataModel
班级访问我的数据库(因为这是我将我的项目添加到列表中的地方),但我无法让它工作。
我已经添加了我当前的代码 - 数据库的工作尚未完成,但我仍然坚持下一步应该做的事情。
这是我的HistoryFragment
课:
// Remove imports
public class HistoryFragment extends FragmentBase implements OnItemAddedHandler {
// FragmentBase is just another class which extends Fragment and the moment.
ListView lv;
SimpleAdapter simpleAdpt;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.history, container, false);
ListView lv = (ListView) view.findViewById(R.id.listView);
List<Map<String, String>> planetsList = DataModel.getInstance()
.getPlanetList();
simpleAdpt = new SimpleAdapter(getActivity(), planetsList,
android.R.layout.simple_list_item_1, new String[] { "planet" },
new int[] { android.R.id.text1 });
lv.setAdapter(simpleAdpt);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentAdapter, View view,
int position, long id) {
TextView clickedView = (TextView) view;
// Display a dialog
}
});
return view;
}
@Override
public void onDetach() {
super.onDetach();
DataModel.getInstance().setOnItemAddedHandler(null);
}
@Override
public void onItemAdded(Object data) {
simpleAdpt.notifyDataSetChanged();
lv.scrollTo(0, 0); // Scrolls to top of list
}
@Override
public void onStart() {
super.onStart();
DataModel.getInstance().setOnItemAddedHandler(this);
}
}
这是我的DataModel
:
public class DataModel {
private List<Map<String, String>> mPlanetsList = new ArrayList<Map<String, String>>();
private static DataModel instance;
private static OnItemAddedHandler mOnItemAddHandler;
private DataModel() {
initList();
}
public static DataModel getInstance() {
if (null == instance) {
instance = new DataModel();
}
return instance;
}
private void initList() {
// Here is where I want to load the saved listitems.
}
public List<Map<String, String>> getPlanetList() {
return mPlanetsList;
}
private HashMap<String, String> createPlanet(String key, String name) {
HashMap<String, String> planet = new HashMap<String, String>();
planet.put(key, name);
return planet;
}
public void setOnItemAddedHandler(OnItemAddedHandler handler) {
mOnItemAddHandler = handler;
}
public void addItem(Object data) { // Save stuff here?
Bundle data1 = new Bundle();
String string = ((Bundle) data).getString("keyTitle");
mPlanetsList.add(0, createPlanet("planet", string));
// This is where I want to save my items to the DataBase
HistoryDataBase.getInstance(); //Adding .getWritableDatabase() gives me a nullpointerexception.
// I don't understand what else I need to do here to add my mPlanetList to the DataBase
if (null != mOnItemAddHandler) {
mOnItemAddHandler.onItemAdded(data1);
}
}
}
最后是我的数据库:
public class HistoryDataBase extends SQLiteOpenHelper {
private static HistoryDataBase instance;
public static final String TABLE_COMMENTS = "comments";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_COMMENT = "comment";
private static final String DATABASE_NAME = "commments.db";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_COMMENTS + "(" + COLUMN_ID
+ " KEY, " + COLUMN_COMMENT
+ " text not null);";
static Context c;
HistoryDataBase(Context context) {
super(c, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
public static HistoryDataBase getInstance() {
if (null == instance) {
instance = new HistoryDataBase(c);
}
return instance;
}
}
非常基本:我想永久保存我的列表项,并能够检索、编辑和删除它们。
感谢您抽出时间来阅读。如果需要任何澄清,请说。