带有图像的ListView保存在SQLite中

时间:2011-12-07 10:40:02

标签: android sqlite android-listview

开发一个应用程序,其中将显示带有图像的ListView文本数据,并从服务中下载图像。我想在SQLite中保存数据我也想保存缩略图我的代码是

public class MainActivity extends Activity {
    // TAG for LogCat
public static final String TAG = "SQLiteDebug";
// list view control
    private ListView mListUserInfo; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_main);

    // load list view control
    mListUserInfo = (ListView)findViewById(R.id.listUsers);
    // create data creator
    UserInfoCreator creator = new UserInfoCreator(this);
    // open connection
    creator.open();
    // insert data
    creator.insert();     
    // add data to list view through adapter
    mListUserInfo.setAdapter(new UserInfoAdapter(this, creator.queryAll()));//,thumbURLStrings));        
    // close connection
    creator.close();

}
}

数据插入

public class UserInfoCreator {


    static String [] thumbURLStrings = null;
    // db adapter
    private UserDbAdapter mDbAdapter;

    /*
     * constructor
     */
    public UserInfoCreator(Context c) {
            mDbAdapter = new UserDbAdapter(c);
    }

    /*
     * open DBAdapter connection
     */
    public void open() {
            mDbAdapter.open();
    }

    /*
     * insert random data
     */
    public void insert() {
    mDbAdapter.insertUser("Toteninsel 100. Fall", 1, "André Marx");
    mDbAdapter.insertUser("Geisterbucht 150. Fall", 2, "Astrid Vollenbruch");
    mDbAdapter.insertUser("Feuermond 125. Fall", 3, "André Marx");
    mDbAdapter.insertUser("Top Secret Edition", 4, "Peter Lerangis");
    mDbAdapter.insertUser("Der dreiTag", 5, "Hendrik Buchna");
    mDbAdapter.insertUser("Die blutenden Bilder", 6, "Kari Erlhoff");

        String xml = XMLfunction.getXML("http://ddfzentrale.europa-websites.de/Zentrale_php/XML/Buecher.xml");
        Document doc = XMLfunction.XMLfromString(xml);

        NodeList nodes = doc.getElementsByTagName("book");
        String [] titleStrings = new String[nodes.getLength()];
        String [] authorStrings = new String[nodes.getLength()];

        Log.d("nodeLength","len: "+nodes.getLength());
        for(int i=0;i < nodes.getLength();i++){

            Element eleBook = (Element)nodes.item(i);
            Log.d("Node_TagName", eleBook.getTagName());
            NodeList titleNode = eleBook.getElementsByTagName("Title");
            Element TitleEle = (Element) titleNode.item(0);
            Log.i("Title", "Title - "+TitleEle.getFirstChild().getNodeValue());
            titleStrings[i]= TitleEle.getFirstChild().getNodeValue();
            NodeList AuthorFName1Node = eleBook.getElementsByTagName("AutorFName1");
            Element AuthorFName1Ele = (Element) AuthorFName1Node.item(0);
            Log.i("AuthorFName1","AuthorFName1 - "+AuthorFName1Ele.getFirstChild().getNodeValue());
            NodeList AuthorLName1Node = eleBook.getElementsByTagName("AutorLName1");
            Element AuthorLName1Ele = (Element) AuthorLName1Node.item(0);
            Log.i("AuthorLName1","AuthorLName1 - "+AuthorLName1Ele.getFirstChild().getNodeValue());
            authorStrings[i]=AuthorFName1Ele.getFirstChild().getNodeValue()+" "+AuthorLName1Ele.getFirstChild().getNodeValue();

           mDbAdapter.insertUser(titleStrings[i], i, authorStrings[i]);


        }
     }

    /*
     * query all user info from db
     */
    public List<UserInfo> queryAll() {
            return mDbAdapter.fetchAllUsers();
    }

    /*
     * close connection
     */
    public void close() {
            mDbAdapter.close();
    }

}

我在同一个类中解析和插入数据但是我希望显示文本并显示缩略图图像以显示带有我使用过的图像的缩略图https://github.com/thest1/LazyList请有人知道如何在SQLite中存储图像请帮帮我

2 个答案:

答案 0 :(得分:1)

检查此代码以将图像插入从服务器下载的数据库:

Download Image from Server store it as BLOB and Retrieve it back.

答案 1 :(得分:0)

不建议在SQLite数据库中存储图像。但仍有可能这样做。您可以将图像转换为字节数组,并将其作为BLOB存储在数据库中。我希望它有所帮助!