0

アプリケーションでsqliteデータベースの画像を表示したい。私の要件は、テキストビューとそれに関連する画像を表示することです。データベースファイルをアセットフォルダに保存しました。しかし実際には、私の画像はサーバーの「allimages」フォルダーに保存されています。

私のデータベース列(私が保持していたアセットフォルダー内のdb)は次のとおりです。

S.説明なし

1 div style = "text-align:justify;">ガンディーは素晴らしいリーダーですimg height = "194" width = "281" src = "/ allimages / img.png" alt = "" div

2 div style = "text-align:justify;">Subashは優れたリーダーですimgheight = "194" width = "281" src = "/ allimages / img.png" alt = "" />
------ --------- -------------- -------------- --------------- ---------------------------------

今、私の問題は私のデータベースにあり、説明の列に示したように画像パスが保存されています。Androidでは、ドローアブルフォルダに新しいフォルダを作成して画像のパスを参照することはできません。私のデータベースは2000から3000のレコードで構成されています。これで、ローカルデータベースの画像のパスを変更できません。では、プロジェクトフォルダのどこに「allimages」フォルダを作成する必要があり、それらの画像をどのように参照できますか?誰かが私がこの問題を解決するのを手伝ってください.....私はこの問題に苦労しています...

前もって感謝します.....

4

1 に答える 1

0

上記のコメントに基づいて:

ループを介してデータベース内のすべての要素を更新できます

残念ながら、データベースからデータを抽出し、何らかのリストに入れ、リストを変更してパスを変更し、変更した値でデータベースを更新する必要があります。

注: これを行う必要があるのは 1 回だけですが、更新するレコードが数千ある場合は時間がかかります。

以下は、すべてのパスを更新するために実行する必要がある手順の擬似コードです。

すべてのレコードを取得し、データを抽出してパスを変更します

//Get all the records
Cursor allRecords = database.query(Table_Name, null, null, null, null, null, null);

//Extract the data from the Cursor and create a list
List<SomeDataObject> listOfModifiedDataObjects = new ArrayList<SomeDataObject>();

allRecords.moveToFirst();

while(!allRecords.isAfterLast())
{
    int id = allRecords.getInt(allRecords.getColumnIndex("NameOfIDColumn"));
    String description = allRecords.getString(allRecords.getColumnIndex("NameOfDescriptionColumn"));

    //Modify the description to change the path of the src...
    description = description.replace("allimages", "some/other/path/allimages");

    //Create the data object and store it in the list
    SomeDataObject tempObj = new SomeDataObject();
    tempObj.ID = id;
    tempObj.Description = description;

    listOfModifiedDataObjects.add(tempObj);

}

変更されたデータでデータベースを更新します 注: これは、SQLiteDatabase オブジェクトにアクセスできるデータベース ヘルパー クラスで行う必要があります。

//Iterate through all the modified data objects
for(SomeDataObject curObj: listOfModifiedDataObjects)
{
    ContentValues values = new ContentValues();

    //Set the new description into the content values object
    values.put("NameOfDescriptionColumn", curObj.Description);

    //Update the table with the new value. The record that gets updated is based on the curObj.ID.
    //In this case, I'm guessing your id column is named "_id"
    database.update(TABLE_NAME, values, "_id = ?", new String[]{String.ValueOf(curObj.ID)});
}

私はこのコードをコンパイルしておらず、便利な IDE も持っていないため、小さなタイプミスがあるかもしれません。しかし、これで各レコードを更新する方法の一般的なアイデアが得られることを願っています。

于 2012-05-16T18:42:11.057 に答える