我寫了一本字典應用程序並將其保存在文本文件中的行,因爲我不能apijexcel的主要問題是工作程序沒有響應呢。我創建了數據庫mysql,類從文本文件中讀取數據,類有2個構造函數word & translation,適配器類爲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類
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();
}
}
}
1.定義「不響應」。 2.什麼是錯誤? – Carcigenicate
請正確設計您的問題。在這裏閱讀[詢問好問題](http://stackoverflow.com/help/how-to-ask)。 請勿發佈您的代碼的屏幕截圖。在此處插入代碼。 並分享你的錯誤,因爲它顯示在你的屏幕上。我什至不明白什麼是database1,2,3和loader1,looader2。請清除你的問題。 –
沒有錯誤出現,只是關於應用程序沒有響應的對話框。 –