String[] classes = {"Activity","Activity1"};
for( i = 0; i < sources.length; i++) {
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),classes[i].class);
startActivityForResult(intent,0);
}
});
}
Here is problem with classes[i].class . Is it possible . Need help
So many things wrong with this code.
1)To access a variable in an anonymous inner class like that, it must be declared as final.
2)It won't work anyway- the class of "Activity" isn't Activity.class, it's String.class. Because "Activity" is a string. If you want this code to work, you need an array Class, not an array of Strings. And then the two members would need to be Activity.class and Activity1.class.
You can try this one:
You need to made few changes in your code:
First: You can declare your array string of classes, in this way:
String[] classes = {"YourPackageName.Activity","YourPackageName.Activity1"};
Second: For starting the activity.
for( i = 0; i < sources.length; i++) {
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent();
intent.setClassName(v.getContext(),classes[i]);
startActivityForResult(intent,0);
}
});
}
Hope this helps you.
Considering your for loop does not give any kind of Exception, Bcoz i can't test that. But above code should work in order to start your Activity.
Related
this is my first question being asked on stackoverflow. My question is regarding variable use across different recyclable intents.
e is declared like this.
final Bundle e=getIntent().getExtras();
Here i am creating new intents for different setOnClickListener() and passing a different variable for each intent.
Intent info = new Intent(EItemListView.this, ItemInfo.class);
Bundle extras = new Bundle();
int[] a=new int[listview.getAdapter().getCount()];
if (i == 0) {
extras.putIntArray("img", n5x_images);
extras.putString("info", n5x_info);
extras.putInt("pc",a[0]);
} else if (i == 1) {
extras.putIntArray("img", op3_images);
extras.putString("info", op3_info);
extras.putInt("pc",a[1]);
}
info.putExtras(extras);
startActivity(info);
Now this is the OnClickListener() where i am trying to update the variables which i passed through the intent extras, but am unable to update those variables.
addtc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int c=e.getInt("pc");
c=c+1;
Log.i("Log","value "+c);
}
The log message which i get from the above method is always 1, i think the variable in c is always set to 0 and then increments by 1 and hence the log message shows 1.
I need the variables a[0],a[1],a[2], etc to pertain its increment operation.
To make it more clear, this is the java file i am using. The error is in the OnClickListener of addtc button at the bottom of this code.
public class ItemInfo extends AppCompatActivity {
private ViewAnimator viewanimator;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_info);
Button next=(Button)findViewById(R.id.bnext);
Button prev=(Button)findViewById(R.id.bprev);
viewanimator=(ViewAnimator)findViewById(R.id.viewAnimator);
TextView info=(TextView)findViewById(R.id.item_info);
Button addtc=(Button)findViewById(R.id.badd);
Button test=(Button)findViewById(R.id.button);
Bundle e=getIntent().getExtras();
int img[]=e.getIntArray("img");
for(int i=0;i<img.length;i++)
{
ImageView imgview = new ImageView(getApplicationContext());
imgview.setImageResource(img[i]);
viewanimator.addView(imgview);
}
info.setText(e.getString("info"));
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewanimator.showNext();
}
});
prev.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewanimator.showPrevious();
}
});
addtc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int c=e.getInt("pc");
c=c++;
Log.i("Log","value "+c);
}
});
}
}
Thanks in advance!!!
Your approach is wrong. You cannot do this in this way. Your understanding of what an "extra" in an Intent is incorrect.
When you do this:
extras.putInt("pc",a[1]);
This adds an "extra" to the extras Bundle. The Bundle is simply a key/value pair map and you have added an entry that contains the key "pc" and the value is whatever a[1] is. It puts a copy of the value of a[1] into the Bundle, it does not put a reference to a[1] in the Bundle.
Therefore, if a[1] is 5 when you add it to the extras Bundle, a[1] will always be 5 and will never be changed to anything else.
You can't do this in this way.
Alternative: Depending on your application architecture and what you are trying to do, you can use one of the following methods:
1) Use startActivityForResult(), pass the data from one Activity to another, have the second Activity update the data and put it back into the Intent which is then returned to the "calling" Activity by using setResult().
2) Use a static variable (basically a "global" variable) to contain the data. Both activities can then access the data directly (you don't need to put the data in the Intent.
3) Put the data in a database. Both activities can then read/write from/to the database.
First advice I can give you is debugging and posting debug result. for example, are you sure that a[0] and a[1] aren't 0?
Assuming they are not, why are you declaring the bundle as final? referring to this probably final is not what you were looking for. Try removing it or replacing with private
Another suggestion is more for readable purpose, replace c = c+1; with c++; but this doesn't change the result, it just make it more linear and easier for reading.
Now after this fix (the final keyword one) tell me if something changed please :)
I'm working on eclipse (android) and I want to call outside a variable that is in the OnClick method. How can I do that? I thought to use a return but OnClick is a void method. Here is my code
backgroundE2.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
int randomIntE = random2.nextInt(Deck.length());
int drawableIDE = Deck.getResourceId(randomIntE, -1);
backgroundE2.setBackgroundResource(drawableIDE);
}
});
I'm trying to call the variable randomIntE. How can I do that if everything is closed? I have to call also other 4 variables that are in different setOnClickListener.
You can use a global variable declared outside the function.
public class MainActivity extends Activity {
int fnsetFlag= 0;
backgroundE2.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
fnsetFlag= 1;
}
});
}
you should save it somewhere, like in an instance variable or in an object that can be called from the piece of code you want to use.
I hope to have understand the question properly.
Andrea.
I am learning Android (I'm VERY newbie at the moment).
I was looking and reading another posts but I have not find this exactly (or not simple).
I want to pass a arraylist from an activity to a intent service, I think this is the simplest manner to do it; however I get NullPointer Exception.
public class MainActivity extends Activity {
private static final String TAG = "Main";
ArrayList<String> lista_actual = new ArrayList<String>();
...
public void onClick (View v) {
Intent msgIntent = new Intent(MainActivity.this, MiIntentService.class);
lista_actual.add("probasndo cad");
lista_actual.add("dfafsadf");
lista_actual.add("dfasf");
msgIntent.putStringArrayListExtra("lista", lista_actual);
msgIntent.putExtra("iteraciones", 10);
startService(msgIntent);
//copiar();
}});
Then where I try get the array:
protected void onHandleIntent(Intent intent)
{
ArrayList<String> lista_archivos = intent.getStringArrayListExtra("lista_actual");
Log.d ("Intent", Integer.toString(lista_archivos.size()));
.
.
.
Thanks.
While you are fetching your array list in intent service ur calling wrong key, it should be :
ArrayList<String> lista_archivos = intent.getStringArrayListExtra("lista");
Log.d ("Intent", Integer.toString(lista_archivos.size()));
Replace lista_actual with lista.
You need to serialize the arraylist in order to pass between activities. Further help will be found here. Hope that helps
Is it possible to start a random activity if e.g. a button is clicked?
I already saw some solutions with switch/case, but I dont really want to do something like this:
case 1: startintent1;
break;
case 2: startintent2;
break;
...
case 100: startintent100;
break;
Is it maybe possible to store the code that is used to open an activity in an array and then pick an item from that array with Random?
Thanks in advance!
You can create an array of classes like this:
Class<?>[] classes = new Class<?>[] { MainActivity.class, DatabaseActivity.class };
Or use an ArrayList:
ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
classes.add(MainActivity.class);
then use your random number as the index.
Intent i = new Intent(getActivity(), classes[randomNumber];
or
Intent i = new Intent(getActivity(), classes.get(randomNumber);
startActivity(i);
I have no idea what's wrong with using a switch case, but I guess you could put your Activities in an array and then use a random integer 0 to n to pick one.
Class c = Class.forName("classname");
Method m = c.getMethod("startintent"+i);
m.invoke(this);
This will invoke by name. If all your functions are all consistently named. Then use random to append a number.
Try this :
x.setOnClickListener(new OnClickListener() {
private static final Random random = new Random();
#Override
public void onClick(View v) {
//TODO --place your activity in an array list here
int randomMsgIndex = random.nextInt(yourarraylist.length - 1);
//TODO-start that activity using intents.
}
});
I am trying to build a char using the library from achartengine (http://www.achartengine.org/). So i try to run SalesGrowthChart.java on my own aplication so when someone clicks on a button it will show him the chart .
This is my code :
private IChart[] mCharts = new IChart[]{new generatedchart()};
And i try to generate it like this
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), generatedchart.class);
myIntent=mCharts[0].execute(this);
}
});
But this won't work. How can i make it to work.Hope you understand what i am saying.
This is the error :
The method execute(Context) in the type IChart is not applicable for the arguments (new View.OnClickListener(){})
Your problem is that the "this" mentioned inside that method refers to a view (which is what you are creating at that point.)
The method execute needs a Context, so you need to get the context in a different manner.
You should try getting the context like this:
YourActivityName.this
Which in your code would be like:
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), generatedchart.class);
myIntent=mCharts[0].execute(YourActivityName.this);
}
});
When you are calling this within execute(), it is referring to the OnClickListener class because of the dynamic class declaration. Try using getApplicationContext() instead of this.