16

I have a table of 40 squares and each has an ID. When I'm passing Bundle from another activity I need to extract Note, Color and ID from that bundle. And then the app will change/add text and change background of the square specified by extracted ID. Each square has ID of int format. Each ID passed from other activity is in string format. I can't figure out how to make it to findViewById(R.id.passed_id), and how to get two different formats working together. I've tried to change ID of each square but eclipse says that ID have to have a letter along with a number. I'm lost....Here's the code:

package com.tt;

 import android.os.Bundle;
 import android.app.Activity;
 import android.content.Intent;
import android.content.res.Resources;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity
 {
String gotNotes;
String n;
String gotDOW;
String gotID;
public String Id;
String gotHour;
TextView notes;
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initialize();
    Button settings = (Button)findViewById(R.id.settings);

    Bundle gotPackage = getIntent().getExtras();
    if (gotPackage != null){
    gotNotes = gotPackage.getString("AddedNote");
   // if (gotNotes.equals(" ")){n = "Empty";}else n = gotNotes;
    //gotDOW = gotPackage.getString("Day");
    //gotHour = gotPackage.getInt("Hour");
    gotID = gotPackage.getString("ID");

    Id = gotID;
    notes.setText(gotNotes + (" \n") + gotID);

    }
    else{}



  settings.setOnClickListener(new OnClickListener()
    {



          public void onClick(View v)
         {
              Intent i = new Intent(v.getContext(),Settings.class);
              startActivityForResult(i,0);
         }

     });

   }

  private void initialize() 
       {
    // TODO Auto-generated method stub


    notes = (TextView)findViewById(R.id.);
    notes.setGravity(Gravity.CENTER);
    notes.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
}



    @Override
            public boolean onCreateOptionsMenu(Menu menu) 
             {
               getMenuInflater().inflate(R.menu.activity_main, menu);
               return true;

             }

      }

UPDATE

Ok.. here's the log:

12-27 16:18:55.661: D/AndroidRuntime(12497): Shutting down VM
12-27 16:18:55.661: W/dalvikvm(12497): threadid=1: thread exiting with uncaught exception (group=0x40abf228)
12-27 16:18:55.671: E/AndroidRuntime(12497): FATAL EXCEPTION: main
12-27 16:18:55.671: E/AndroidRuntime(12497): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tt/com.tt.MainActivity}: java.lang.NullPointerException
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.os.Looper.loop(Looper.java:154)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.ActivityThread.main(ActivityThread.java:4944)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at java.lang.reflect.Method.invokeNative(Native Method)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at java.lang.reflect.Method.invoke(Method.java:511)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at dalvik.system.NativeStart.main(Native Method)
12-27 16:18:55.671: E/AndroidRuntime(12497): Caused by: java.lang.NullPointerException
12-27 16:18:55.671: E/AndroidRuntime(12497):    at com.tt.MainActivity.initialize(MainActivity.java:69)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at com.tt.MainActivity.onCreate(MainActivity.java:30)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.Activity.performCreate(Activity.java:4531)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
12-27 16:18:55.671: E/AndroidRuntime(12497):    ... 11 more
Blackbelt
  • 156,034
  • 29
  • 297
  • 305

3 Answers3

40

You can get an identifier from a string by using:

notes = (TextView)findViewById(getResources().getIdentifier(VIEW_NAME, "id", getPackageName()));

Where VIEW_NAME is whatever identifier string you're generating. After that, you can set the text of it like you currently are. This also works if you need to get strings and drawables as well, just change id to the appropriate type.

jprofitt
  • 10,874
  • 4
  • 36
  • 46
  • 1
    This works like a charm. I've written in Kotlin in this way `var txtview = findViewById(resources.getIdentifier(VIEW_NAME, "id", packageName))` – Kshitij Apr 24 '19 at 11:20
0

Assuming you're passing in the actual string "R.id.passed_id", I would recommend passing in the integer value instead. Thus, simply pass in R.id.passed_id.

All values in the R file of an application are simply integers that are mapped to resource locations. The findById() methods are only looking for integers, so if you start the Activity and pass in the integer value of the resource you want, it will find it.

Note: This will only work if the resources are within the same application. R files remain constant for an application, but they can change from one app to another even if they have the same name. If, however, the two activities fall under the same application package, it will work.

DeeV
  • 35,865
  • 9
  • 108
  • 95
-2

Man I'm not understating what exactly you want to do. Id is the id off an android widget then on function initialize() you should cast string to int this way: Integer.ValueOf(Id) Hope this is what you want.

gezimi005
  • 381
  • 1
  • 8
  • But will it pass only integer value form actual ID or it will pass whole ID (which is m1 or m2 or ..depending on day and time slot)? –  Dec 27 '12 at 15:33
  • I've changed passed value from string to an int, but now app crashes without even getting launched.. –  Dec 27 '12 at 15:43
  • Strange ! can you paste logs here to see where the problem might be ? – gezimi005 Dec 27 '12 at 16:00
  • can you check first if notes is null or not: if(notes != null){ notes.setGravity(Gravity.CENTER); notes.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12); } – gezimi005 Dec 27 '12 at 16:29
  • an other thing: you should call initialize(); after you called Id = gotID; and put this instruction inside initialize() function: notes.setText(gotNotes + (" \n") + gotID); – gezimi005 Dec 27 '12 at 16:34
  • Ok.. I did all that - app launched but when I tried to add notes to certain square it crashes with that message : 12-27 16:42:18.529: E/AndroidRuntime(14406): java.lang.NumberFormatException: Invalid int: "m1" –  Dec 27 '12 at 16:44
  • I've tried different options and I'm getting error at this line: ID = Integer.valueOf(IDs); –  Dec 27 '12 at 16:54
  • IDs must be numeric string, for example: "1", "120", "6666" etc. If it's not then the instr: Integer.valueOf(IDs) will give an exception ... One question why you do not change the type of IDs to int, where the IDs contain value of R.id.editTextRandID which is integer.? – gezimi005 Dec 27 '12 at 17:01
  • ...but id of each square is combination of letter and number , like m1, t4 and so on... (And again looks like its defined by eclipse as an int).... I've tried to generate the same IDs but it can exist only as a string..And when i use findViewById it requires an int... I don't understand how it works..... –  Dec 27 '12 at 18:08
  • Id that is passed from another activity is also combination of two values defined by a user (day+time).. Which is a string ... –  Dec 27 '12 at 19:19
  • jprofitt Thanks a lot man That worked !!!! Now its just left to figure out how to add more notes to a different squares without removing previously entered.... –  Dec 27 '12 at 20:05