Я довольно новичок в программировании для Android, и у меня возникают проблемы с обновлением TextView в моем коде. Я могу установить его, и он корректно обновляется с onResume().Android: TextView не обновляется по invalidate() или postInvalidate()
Позже я называю
startActivityForIntent(my.android.activity,0)
Затем я использую SetText в этом методе:
onActivityResult(int requestCode, int resultCode, Intent returnIntent){
if (resultCode == Activity.RESULT_OK){
String myString= returnIntent.getString("mystr","empty");
myClass=myClass.interperetResults(myString); //this method returns correctly
leftText.setText(""+myClass.toString());
leftText.invalidate() //neither invalidate() or postInvalidate() changes result
}
};
и появится обновленный внутри класса TextView (я использовал System.out для отладки это) , но он не обновляется на экране.
Из того, что я могу сказать, мне просто нужно вызвать TextView.invalidate() или TextView.postInvalidate(), ни один из которых не эффективен. Есть что-то, что мне не хватает, или что? Я пробовал много разных предложений от других сообщений, но безрезультатно.
У меня также есть объект rightText и операторы if для возвращаемой строки, но это раздел, с которым у меня возникают проблемы. Если вам нужны другие части моего кода, пожалуйста, дайте мне знать. Я был бы более чем счастлив помочь. Еще раз спасибо за ваш вклад.
EDIT: В соответствии с запросом, вот мой xml-макет. Хотя проблема заключается не в том, что я не вижу вид. Когда я вызываю setText в onResume(), он отображается и изменяется правильно. Во всяком случае, вот мой xml-код. Мне не хочется менять имена переменных на общие вещи, поэтому на самом деле это так. Я работаю с партнером по проекту, и именно он создал xml и макет, поэтому я не совсем уверен, что все здесь. Надеюсь, вы, ребята, можете это понять.
Вот OnCreate:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
}
И onResume:
protected void onResume() {
super.onResume();
// The activity has become visible (it is now "resumed").
setContentView(R.layout.play);
myEquation = new Equation(getResources());
leftText = (TextView) this.findViewById(R.id.playLeftEquation);
rightText = (TextView) this.findViewById(R.id.playRightEquation);
//leftText.setText("No data");
//rightText.setText("No data"); //The textviews update fine here.
//Currently commented out because I thought that maybe this was
//overwriting the other setText, but still no change.
}
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/atomic_bckgrd"
android:columnCount="11" >
<ImageButton
android:id="@+id/playTrashCanButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnSpan="2"
android:layout_gravity="left"
android:layout_row="0"
android:contentDescription="@string/trash"
android:onClick="clickHandler"
android:src="@drawable/trash" />
<Button
android:id="@+id/playEditButton"
android:layout_column="2"
android:layout_row="0"
android:onClick="clickHandler"
android:text="@string/edit" />
<ImageButton
android:id="@+id/playRulesButton"
android:layout_column="4"
android:layout_columnSpan="5"
android:layout_row="0"
android:contentDescription="@string/rules"
android:onClick="clickHandler"
android:src="@drawable/rules1" />
<TextView //One of the non-updating fields
android:id="@+id/playLeftEquation"
android:layout_column="1"
android:layout_gravity="bottom"
android:layout_row="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageButton
android:id="@+id/playEnterButton"
android:layout_column="2"
android:layout_gravity="bottom"
android:layout_row="1"
android:contentDescription="@string/enter"
android:onClick="clickHandler"
android:src="@drawable/enter1" />
<TextView //The other non-updating field
android:id="@+id/playRightEquation"
android:layout_column="3"
android:layout_columnSpan="6"
android:layout_gravity="bottom"
android:layout_row="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Space
android:layout_width="52dp"
android:layout_height="1dp"
android:layout_column="0"
android:layout_row="0" />
<Space
android:layout_width="148dp"
android:layout_height="1dp"
android:layout_column="1"
android:layout_row="0" />
<Space
android:layout_width="161dp"
android:layout_height="1dp"
android:layout_column="2"
android:layout_row="0" />
<Space
android:layout_width="46dp"
android:layout_height="1dp"
android:layout_column="3"
android:layout_row="0" />
<Space
android:layout_width="36dp"
android:layout_height="1dp"
android:layout_column="4"
android:layout_gravity="fill_horizontal"
android:layout_row="0" />
<Space
android:layout_width="1dp"
android:layout_height="160dp"
android:layout_column="0"
android:layout_gravity="fill_horizontal"
android:layout_row="0" />
</GridLayout>
Можете ли вы разместить свой xml-макет и метод onCreate для этой операции? – gutiory
@powerisall вам не нужно будет аннулировать текст. вид, вероятно, показывает ваш текст и по какой-то причине вы не можете его увидеть. как предположил gutiory, мы могли бы найти его в вашем XML-макете. – Joe
Хорошо, я обновил основной вопрос, включив соответствующий макет xml. – powerisall