Search code examples
androidtextviewsettext

Android TextView setText does not work


I read some problems from other users facing the same problem, but the solutions didn't work for me. I have defined a TextView in my layout xml:

<TextView
  android:id="@+id/currentLocation"
  android:layout_column="0"
  android:layout_row="14"
  android:text="@string/currentLocation" />

Now in my Activity I get the TextView by

location = (TextView) findViewById(R.id.currentLocation);

In my activity I'm also able to start two other activities (one is a googlemap-View, the other one is the contactlist of the phone). My onActivityResult() method in the main activity looks like the following:

public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);

    switch (reqCode) {
        case (PICK_CONTACT) :
            if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                Cursor c =  managedQuery(contactData, null, null, null, null);
                if (c.moveToFirst()) {
                    try{
                        String address = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
                        convertAddressToLatLon(address);
                    }
                    catch(Exception e){
                        errorDialog.setMessage("Could not get Address. Try another contact or use Google Maps.");
                        errorDialog.show();
                    }

                }
            }
        case (PICK_GMAPS) :
            if (resultCode == Activity.RESULT_OK) {
                Bundle googleData = data.getExtras();
                location.setText(googleData.getString("address"));
            }
        break;
    }
}

In the convertAddressToLatLon(address) method there is also a location.setText(); with definatelly a stringvalue!

So if i return from the googlemap-activity, location-text changes to given input. In the contactactivity part it does not work. But there is no error. It reaches the location.setText() method, but does not change the text. I have no idea whats wrong. its not possible, that there is a wrong character in the string. Even if I put 'location.setText("test")' after the closing braket of the catch block, it does not work. (again, the program goes into the if(c.moveToFirst()){ part!). I have no idea whats wrong, because like I said the googlepart works correct.


Solution

  • Found the mistake. I forgot to put a "break;" at the end of the PICK_CONTACT-case. Now its working...