我写了一个字典应用程序并将其保存在Row的文本文件中,因为我无法使用apijexcel主要问题是应用程序尚未响应。我创建了数据库mysql,类从文本文件读取数据,类有2个构造函数word&翻译,listview项目的适配器类。
我不知道代码中的主要错误是什么。 有什么帮助吗?
public class Adapter extends BaseAdapter {
Context context;
ArrayList<WordTranslation> AllTranslations= new ArrayList<WordTranslation>();
private DictionaryDataBaseHelper myDictionaryDatabaseHelper;
TextView Translation_textview;
TextView Word_textview;
public Adapter (Context context , ArrayList<WordTranslation>AllTranslations){
this.context=context;
this.AllTranslations=AllTranslations;
}
@Override
public int getCount() {
return AllTranslations.size() ;
}
@Override
public Object getItem(int i) {
return AllTranslations.get(i);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
AllTranslations = myDictionaryDatabaseHelper.getAllWords();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView= inflater.inflate(R.layout.activity_list_item, null);
Translation_textview = (TextView) convertView.findViewById(R.id.trans3);
Word_textview= (TextView) convertView.findViewById(R.id.word3);
Translation_textview.setText(AllTranslations.get(position).translation);
Word_textview.setText(AllTranslations.get(position).word);
return convertView;
}
}
WordTranslation类
public class WordTranslation {
String word, translation;
//constractor with 2params
public WordTranslation(String word, ArrayList<String> AllTranslation) {
this.word = word;
// StringBuilder equals to String decleration as well as StringBuffered
// JavaTpoint website
StringBuilder stringBuilder = new StringBuilder();
for (String string : AllTranslation)
stringBuilder.append(string);
stringBuilder.insert(translation.length() , "/ ") ;
this.translation = stringBuilder.toString();
}
public WordTranslation(String word, String AllTranslation) {
this.word = word;
this.translation = AllTranslation;
}
}
DictionaryDataBaseHelper Class
public class DictionaryDataBaseHelper extends SQLiteOpenHelper {
final static String Dictionary_DataBase = "Dictionary";
final static String ID_ITEM_COLUMN = "id";
final static String WORD_COLUMN = "word";
final static String TRANSLATION_COLUMN = "translation";
final static String CREATE_DATABASE_QUERY = "CREATE TABLE " + Dictionary_DataBase + "(" +
ID_ITEM_COLUMN + "INTEGER PRAMARY KEY COLUMN , " +
WORD_COLUMN + "TEXT ," +
TRANSLATION_COLUMN + "TEXT )";
final static String ON_UPGRADE_QUERY = "DROP TABLE" + Dictionary_DataBase;
Context context;
public DictionaryDataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, Dictionary_DataBase, factory, version);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DATABASE_QUERY);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(ON_UPGRADE_QUERY);
onCreate(db);
}
public long insertData(WordTranslation wordTranslation) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(WORD_COLUMN, wordTranslation.word);
values.put(TRANSLATION_COLUMN, wordTranslation.translation);
return database.insert(Dictionary_DataBase, null, values);
}
public long updateData(WordTranslation wordTranslation) {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(WORD_COLUMN, wordTranslation.word);
values.put(TRANSLATION_COLUMN, wordTranslation.translation);
return database.update(Dictionary_DataBase, values, WORD_COLUMN + "=?", new String[]{wordTranslation.word});
}
public void DeleteData(WordTranslation wordTranslation) {
SQLiteDatabase database = this.getWritableDatabase();
String QueryString = "DELETE FROM" + Dictionary_DataBase + "WHERE" + WORD_COLUMN + " = ' " + wordTranslation.word + "'";
database.execSQL(QueryString);
}
public ArrayList<WordTranslation> getAllWords() {
ArrayList<WordTranslation> arrayList = new ArrayList<>();
SQLiteDatabase database = this.getReadableDatabase();
String SelectAllQueryString = "SELECT * FROM " + Dictionary_DataBase;
Cursor cursor = database.rawQuery(SelectAllQueryString, null);
if (cursor.moveToFirst()) {
do {
WordTranslation wordTranslation = new WordTranslation(cursor.getString(cursor.getColumnIndex(WORD_COLUMN)),
cursor.getString(cursor.getColumnIndex(TRANSLATION_COLUMN)));
arrayList.add(wordTranslation);
} while (cursor.moveToNext());
}
return arrayList;
}
public WordTranslation getWordTranslation(String word) {
SQLiteDatabase database = this.getReadableDatabase();
WordTranslation wordTranslation = null;
String SelectQueryString = "SELECT * FROM " + Dictionary_DataBase + "WHERE " + WORD_COLUMN + " = ' " + word + " '";
Cursor cursor = database.rawQuery(SelectQueryString, null);
if (cursor.moveToFirst()) {
wordTranslation = new WordTranslation(cursor.getString(cursor.getColumnIndex(WORD_COLUMN)),
cursor.getString(cursor.getColumnIndex(TRANSLATION_COLUMN)));
}
return wordTranslation;
}
public WordTranslation getWordTranslation(long id) {
SQLiteDatabase database = this.getReadableDatabase();
WordTranslation wordTranslation = null;
String SelectQueryString = "SELECT * FROM " + Dictionary_DataBase + "WHERE " + ID_ITEM_COLUMN + " = ' " + id + " '";
Cursor cursor = database.rawQuery(SelectQueryString, null);
if (cursor.moveToFirst()) {
wordTranslation = new WordTranslation(cursor.getString(cursor.getColumnIndex(WORD_COLUMN)),
cursor.getString(cursor.getColumnIndex(TRANSLATION_COLUMN)));
}
return wordTranslation;
}
public void InitialDBForFrist(ArrayList<WordTranslation> wordTranslations) {
SQLiteDatabase database = this.getWritableDatabase();
database.execSQL("BEGIN");
ContentValues values = new ContentValues();
for (WordTranslation wordTranslation : wordTranslations) {
values.put(WORD_COLUMN, wordTranslation.word);
values.put(TRANSLATION_COLUMN, wordTranslation.translation);
database.insert(Dictionary_DataBase, null, values);
}
database.execSQL("COMMIT");
}
}
DictionaryLoader类
public class DictionaryLoader {
public static void loadData(BufferedReader bufferedReader, DictionaryDataBaseHelper dictionaryDataBase) {
ArrayList<WordTranslation> allwords = new ArrayList<WordTranslation>();
allwords = new ArrayList<WordTranslation>();
BufferedReader fileReader = bufferedReader;
int c = 17;
try {
c = fileReader.read();
} catch (IOException e) {
e.printStackTrace();
}
while (c != (-1)) {
StringBuilder stringBuilder = new StringBuilder();
while ((char) c != '\n' && c != -1) {
stringBuilder.append((char) c);
System.out.println(stringBuilder.length());
try {
c = fileReader.read();
} catch (IOException e) {
e.printStackTrace();
}
if (c == -1) {
return;
}
}`String wordString = stringBuilder.toString();
ArrayList <String> translate = new ArrayList<String>();
while (c!= '\n' || c!= '\t') ;
try {
c= fileReader.read();
} catch (IOException e) {
e.printStackTrace();
}
if (c == '\n' || c== '\t' || c == '\r') {
StringBuilder stringBuilder1 = new StringBuilder();
while (c != '\n') {
stringBuilder1.append((char) c);
try {
c = fileReader.read();
} catch (IOException e) {
e.printStackTrace();
}
String translateString = stringBuilder1.toString();
translate.add(translateString);
}
}else {
break;
}
wordString = wordString.trim();
allwords.add( new WordTranslation(wordString , translate));
}
try {
dictionaryDataBase.InitialDBForFrist(allwords);
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
这主要是因为您尝试逐行解析text
文件,以便最终到达meaning
和translation
,因此这是一个时间超过5秒,这反过来阻止应用程序的UI线程,你会看到可怕的Application not responding
或ANR
对话框。
我宁愿让你创建一个SQLITE
数据库,在那里你可以存储你的meaning
和translation
,并最终使用约定queries
来达到你预期的结果。