gpt4 book ai didi

android - 适当存储和显示我的书签/历史 Activity ?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:31:39 24 4
gpt4 key购买 nike

我想要我的应用程序的简单书签和/或历史记录,我想知道最合适的存储方式是什么?文本文件或首选项中的文本,或者可能是数据库?哪个更新最灵活,空间和查找时间最高效?

对于显示,我在想this将是一个很好的起点,但向某些项目添加图标是否容易?

编辑:

我终于设置了一个应该连接到数据库的书签 Activity :

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bookmarkview);
Cursor cursor = managedQuery(getIntent().getData(), new String[] {Bookmark.TITLE, Bookmark.URL},
null, null, Bookmark.DEFAULT_SORT_ORDER);

setListAdapter(new SimpleCursorAdapter(this, R.layout.bookmarkitem, cursor,
new String[] { Bookmark.TITLE }, new int[] { android.R.id.text1 }));
findViewById(R.id.addBookmark).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ContentValues values = new ContentValues();
values.put("url", _url);
values.put("title", _title);

// When the update completes,
// the content provider will notify the cursor of the change, which will
// cause the UI to be updated.
getContentResolver().update(_myuri, values, null, null);
}
});
}

书签.java:

package com.tunes.viewer.Bookmarks;

import android.net.Uri;
import android.provider.BaseColumns;

/*
* Database will have:
* pk - primary key
* title - the name of the bookmark.
* url - the url.
*/
public class Bookmark implements BaseColumns{

public static final String AUTHORITY = "com.tunes.viewer";

/**
* The content:// style URL for this table
*/
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/Bookmarks");

/**
* The MIME type of {@link #CONTENT_URI} providing a directory of notes.
*/
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.google.note";

/**
* The MIME type of a {@link #CONTENT_URI} sub-directory of a single note.
*/
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.google.note";

/**
* The default sort order for this table
*/
public static final String DEFAULT_SORT_ORDER = "title";

/**
* The title of the note
* <P>Type: TEXT</P>
*/
public static final String TITLE = "title";

/**
* The url
* <P>Type: TEXT</P>
*/
public static final String URL = "url";

}

我似乎已经解决了我遇到的大部分问题,但不幸的是,当我单击“添加”按钮(调用上面的 onclick)时,它并没有添加到数据库中。此外,我将数据添加到数据库中,但它没有显示在 View 中。这里的光标/适配器有什么问题?完整来源是 here .

最佳答案

我建议您使用数据库。这将是满足您需求的简单高效的解决方案。

sqlite 中的单个表将足以满足您的要求。因为您需要维护 url 的列表你访问过。该表还将满足您存储书签的要求。

您的表格格式可能是这样的。

_____________________________________________________________________________________
Id(Auto-increment) | Title of page | Url of Page |name of icon(if needed) |isBookmark |
_____________________________________________________________________________________

这可能是一个很好的结构来满足您的要求。将 isBookmark 设置为 0/1 以将特定链接设置为书签或取消书签。

编辑

  • 我没有建议您使用 SharedPreferences,我也不会(尽管它很直接且易于实现),原因在于 SharedPreferences 的定义,它说:
    "SharedPreferences 类提供了一个通用框架,允许您保存和检索原始数据类型的持久键值对。您可以使用 SharedPreferences 保存任何原始数据: bool 值、 float 、整数、长整型和字符串
    现在我无法想象一种单一的存储方式ArrayList<String> (Urls) 这些原始数据类型之一。

  • 还有另一种解决方法。它是对象序列化。您可以将完整的 arraylist 实例保存到一个文件中,下次当您需要该对象时,以类似的方式反序列化它。这是序列化的示例代码。

.

public void serializeMap(ArrayList<String> list) {
try {
FileOutputStream fStream = openFileOutput(namefile.bin, Context.MODE_PRIVATE) ;
ObjectOutputStream oStream = new ObjectOutputStream(fStream);
oStream.writeObject(list);
oStream.flush();
oStream.close();
Log.v("Serialization success", "Success");
} catch (Exception e) {
Log.v("IO Exception", e.getMessage());
}
}

但是这种方法并不是很推荐。

关于android - 适当存储和显示我的书签/历史 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10593047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com