什么是在SQLite中存储阿拉伯文本并在Android应用程序中读取的最佳方法

时间:2013-12-23 13:56:24

标签: android sqlite textview arabic

我正在使用数据库SQLite创建Android应用程序。
我试图在我的数据库中存储阿拉伯文本。对于1和2我尝试插入阿拉伯文本,4我从阿拉伯文本插入unicode。
enter image description here

String textDoa = "";
try {
    textDoa = new String(cursor.getString(cursor.getColumnIndex("DOA_ARAB")).getBytes(), "UTF-8");
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}

doaDetail.setDoaArab(Html.fromHtml(textDoa).toString());

这是我从数据库中获取数据的代码。
我使用了this

中的ArabicReshaper
TextView textDoa = new TextView(this);
try{
    textDoa.setTypeface(tf);
    textDoa.setText(ArabicUtilities.reshape(doa.getDoaArab()));
    //textDoa.setText(doa.getDoaArab());
    textDoa.setTextSize(17f);
} catch(Exception ex){
    textDoa.setText("font cannot load: "+ ex.toString() );
}

但结果与我的预期不同。

对于1,TextView显示为:. الْحَمْد٠لÙلَّه٠الَّذÙÙŠ أَحْيَاناَ بَعْدَ مَا أَمَاتَنَا ÙˆÙŽØ¥Ùلَيْه٠النّÙØ´ÙوْرÙ

对于4出现与数据库一样,没有更改为阿拉伯语文本。

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

很多人都遇到了这个问题,我建议你用UTF-8存储为文本文件并将其存储在sql中。然后检索它们。不需要大谈话。

如果您不知道如何阅读文本文件。例如SDCard

 //Find the directory for the SD Card using the API
 //*Don't* hardcode "/sdcard"
 File sdcard = Environment.getExternalStorageDirectory();

 //Get the text file
 File file = new File(sdcard,"file.txt");

 //Read text from file
 StringBuilder text = new StringBuilder();

 try {
     BufferedReader br = new BufferedReader(new FileReader(file));
     String line;

     while ((line = br.readLine()) != null) {
        text.append(line);
        text.append('\n');
     }
 }
 catch (IOException e) {
     //You'll need to add proper error handling here
 }

 //Find the view by its id
 TextEdit tv = (TextEdit)findViewById(R.id.text_view);

 //Set the text
 tv.setText(text);

原帖(How can I read a text file from the SD card in Android?

答案 1 :(得分:0)

SQLite中的解决方案阿拉伯语文本 这个问题的解决方案是通过Arab命令保持数据(必须是按代码插入数据(Insert into Table_name()Values())不要在SQlite上插入数据直接这个用于阿拉伯语但英文没问题)

public class SQLiteAdapter
public static final String MYDATABASE_NAME = "TestSQLilte.s3db";
@SuppressLint("SdCardPath")
// Do not forget This code DB_DIR Because the program does not work on Mobile     without this code
    private static String DB_DIR = "/data/data/PackageName/databases/";
   @SuppressWarnings("unused")
 // Do not forget This code DB_DIR Because the program does not work on Mobile  without this code

   private static String DB_PATH = DB_DIR + MYDATABASE_NAME;
   public static final String MYDATABASE_TABLE = "Table_Name";
   public static final int MYDATABASE_VERSION = 2;
   public static final String KEY_ID = "_id";
   public static final String KEY_Name = "Name";
   public static final String KEY_Adress = "Address";
   public static final String KEY_Area = "Phone";

   private static final String SCRIPT_CREATE_DATABASE =
          "create table  IF NOT EXISTS " + MYDATABASE_TABLE + " ("
          + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
          + KEY_Name + " Text not null"
                + ", "
          + KEY_Adress + " Text not null"
                + ", "
          + KEY_Phone + " Text not null);";
  private Context context;
  public SQLiteAdapter(Context c){
  context = c;
 } 

//不要忘记此代码DB_DIR因为没有此代码,程序无法在Mobile上运行       public SQLiteAdapter openToRead()抛出android.database.SQLException {

       sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null,    MYDATABASE_VERSION);

        DB_PATH = context.getDatabasePath(MYDATABASE_NAME).getAbsolutePath();

        sqLiteDatabase = sqLiteHelper.getReadableDatabase();

       return this; 
     }

//不要忘记此代码DB_DIR因为没有此代码,程序无法在Mobile上运行            public SQLiteAdapter openToWrite()抛出android.database.SQLException {               sqLiteHelper = new SQLiteHelper(context,MYDATABASE_NAME,null,MYDATABASE_VERSION);

            DB_PATH = context.getDatabasePath(MYDATABASE_NAME).getAbsolutePath();
           sqLiteDatabase = sqLiteHelper.getWritableDatabase(); 
          return this; 
         }
 // Do not forget This code mySQLiteAdapter.insert Because the program does not Display arabic without this code
  public long insert(String Name,String Adress,String Phone){
        ContentValues contentValues = new ContentValues();
        contentValues.put(KEY_About_Content, Name);
        contentValues.put(KEY_Adress,Adress);
        contentValues.put(KEY_Area ,Area);
        return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
         }  
  public class MainActivity
  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.Test);
  mySQLiteAdapter = new SQLiteAdapter(this);
        mySQLiteAdapter.openToWrite();
  // Do not forget This code mySQLiteAdapter.insert Because the program does not  Display arabic without this code
  mySQLiteAdapter.insert    ("1الاسم","العنوان","التلفون");
   mySQLiteAdapter.insert   ("2الاسم","العنوان","التلفون");
   mySQLiteAdapter.insert   ("3الاسم","العنوان","التلفون");
   mySQLiteAdapter.insert   ("4الاسم","العنوان","التلفون");
 }

I've been suffering from this problem, thank God, it has been resolved in this wa