Answer the question
In order to leave comments, you need to log in
Calling static method MainActivity from another class?
Calling static method MainActivity from another class?
The bottom line is what I call the API class from MainActivity
api.translate(editVvodText.getText().toString(), API.ENGLISH, API.RUSSIAN);
public void updateAdapter()
{
// получаем данные из удаленной БД
Cursor cursor = getContentResolver().query(WORD_URI, null, null, null, null);
startManagingCursor(cursor);
words.clear();
if(cursor!=null)
{
// выбираем данные
if (cursor.moveToFirst())
{
do {
Log.d(TAG,"ID = " + cursor.getInt(0) + ", word_ru = "
+ cursor.getString(1) + ", word_en = "
+ cursor.getString(2));
words.add(new Word(cursor.getString(1)));
} while (cursor.moveToNext());
}
else
{
Log.d(TAG, "0 rows");
words.clear();
}
cursor.close();
}
wordAdapter = new WordAdapter(this, words);
wordAdapter.notifyDataSetChanged();
}
Answer the question
In order to leave comments, you need to log in
Full MainActivity code:
public class MainActivity extends AppCompatActivity
{
final String TAG = "myLogs";
EditText editVvodText;
Button butSearch;
// класс для retrofit
API api;
final Uri WORD_URI = Uri.parse("content://net.kinomovies.wordprovider.TranslateOnline/words");
final String WORD_RU = "word_ru";
final String WORD_EN = "word_en";
ArrayList<Word> words = new ArrayList<Word>();
WordAdapter wordAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// добавляем progressbar в заголовке
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// показываем progressbar в заголовке
//setProgressBarIndeterminateVisibility(true);
updateAdapter();
// настраиваем список
ListView lvMain = (ListView) findViewById(R.id.listView);
lvMain.setAdapter(wordAdapter);
// подкючаем API
api = new API(MainActivity.this);
butSearch = (Button) findViewById(R.id.butSearch);
butSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (editVvodText.getText().length() > 0) {
if (editVvodText.getText().toString().charAt(0) < 127) {
api.translate(editVvodText.getText().toString(), API.ENGLISH, API.RUSSIAN);
//Toast.makeText(getApplicationContext(), "ENGLISH", Toast.LENGTH_SHORT).show();
} else {
api.translate(editVvodText.getText().toString(), API.RUSSIAN, API.ENGLISH);
//Toast.makeText(getApplicationContext(), "RUSSIAN", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), R.string.not_words_search, Toast.LENGTH_SHORT).show();
}
// очищаем edittext
editVvodText.setText("");
updateAdapter();
}
});
editVvodText = (EditText) findViewById(R.id.editVvodText);
editVvodText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
wordAdapter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
public void updateAdapter()
{
Log.d(TAG, "updateAdapter!!!");
// получаем данные из удаленной БД
Cursor cursor = getContentResolver().query(WORD_URI, null, null, null, null);
startManagingCursor(cursor);
words.clear();
if(cursor!=null)
{
// выбираем данные
if (cursor.moveToFirst())
{
do {
Log.d(TAG,"ID = " + cursor.getInt(0) + ", word_ru = "
+ cursor.getString(1) + ", word_en = "
+ cursor.getString(2));
words.add(new Word(cursor.getString(1)));
} while (cursor.moveToNext());
}
else
{
Log.d(TAG, "0 rows");
words.clear();
}
cursor.close();
}
wordAdapter = new WordAdapter(this, words);
wordAdapter.notifyDataSetChanged();
}
}
public class API
{
private static final String SOURCE = "http://api.mymemory.translated.net";
public final static String RUSSIAN = "RU";
public final static String ENGLISH = "EN";
private final TranslateService mService;
final String TAG = "myLogs";
Context ctx;
final Uri WORD_URI = Uri.parse("content://net.kinomovies.wordprovider.TranslateOnline/words");
final String WORD_RU = "word_ru";
final String WORD_EN = "word_en";
public interface TranslateService {
@GET("/get")
Call<TranslatedData> getTranslation(@Query("q") String textTranslate,@Query(value = "langpair", encoded = true) String language);
}
public API(Context ctx) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SOURCE)
.addConverterFactory(GsonConverterFactory.create())
.build();
mService = retrofit.create(TranslateService.class);
this.ctx = ctx;
}
public void translate(final String textToTranslate, final String fromLanguage, final String toLanguage) {
mService.getTranslation(textToTranslate, URLEncoder.encode(fromLanguage + "|" + toLanguage))
.enqueue(new Callback<TranslatedData>() {
@Override
public void onResponse(Response<TranslatedData> response, Retrofit retrofit) {
Log.d("myLogs", fromLanguage + "->" + toLanguage + "=" + response.body().responseData.translatedText);
Log.d("myLogs", "match="+Float.valueOf(response.body().responseData.match));
//if(Float.valueOf(response.body().responseData.match)==1)
if(response.body().responseData.match!=textToTranslate)
{
ContentValues cv = new ContentValues();
if(fromLanguage==RUSSIAN)
{
cv.put(WORD_RU, textToTranslate);
cv.put(WORD_EN, response.body().responseData.translatedText);
}
else
{
cv.put(WORD_EN, textToTranslate);
cv.put(WORD_RU, response.body().responseData.translatedText);
}
Uri newUri = ctx.getContentResolver().insert(WORD_URI, cv);
Log.d(TAG, "insert, result Uri : " + newUri.toString());
}
else
{
Toast.makeText(ctx, "НЕ верный запрос!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Throwable t) {
Log.d("myLogs", "Failed!!!");
}
});
}
}
class TranslatedData {
ResponseData responseData;
}
class ResponseData {
String translatedText;
String match;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question