FindViewById where ID is dynamic string

user1926837 picture user1926837 · Dec 27, 2012 · Viewed 15.8k times · Source

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

Answer

jprofitt picture jprofitt · Dec 27, 2012

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.