findViewById with getIdentifier not working - java

I'm looping through an array, in that for-loop I'm also using this array to get IDs for a couple of spinners:
Integer[] ids;
ids = new Integer[8];
ids[0] = R.id.nr_area;
ids[1] = R.id.nr_municipality;
ids[2] = R.id.nr_method;
ids[3] = R.id.nr_bait;
ids[4] = R.id.nr_sessionhours;
ids[5] = R.id.nr_bifangst_art;
ids[6] = R.id.nr_bifangst_released;
ids[7] = R.id.nr_name;
In the for loop:
for (int i = 0; i < fields.length; i++) {
if(fields[i].contains("*") || fields[i].matches("")){
String test = ids[i].toString();
int resID = getResources().getIdentifier(test, "id", getPackageName());
Spinner spnr = (Spinner) findViewById(resID); //error is on this row
spnr.setBackgroundColor(Color.GREEN);
}
}
Now the error I get is as follows:
android.support.v7.internal.widget.TintEditText cannot be cast to android.widget.Spinner
So I guess this can't be used with a spinner as I found the example code for a EditText.. but how can I do what I'm trying to do with a spinner?
I would appreciate any help! Been fighting with this for hours. Thanks alot..

I think the problem is that either one of the ids is of EditText or id was of EditText and you have changed it to Spinner in your xml lately. Make sure that ids are correct, relate them from xml, if still problem persists, try cleaning your project for indexing again, that should do it.
Hope this helps ...

Related

How to overwrite a text in TextView using a button?

I'm working on an app that generates passwords randomly using the array. The password is in TextView. Everything is good unless I want to generate a new password second time. How can I "delete" the old text (password) from the TextView and replace it with the new one using the same button?
Here are the variables that I'm using:
EditText dlugosc;
String haslo = "";
String pustak = "";
TextView haslo0;
And this is a code that I use to generate a password:
(znaki is the name of array)
dlugosc = findViewById(R.id.password_len);
haslo0 = findViewById(R.id.password);
String yui = dlugosc.getText().toString();
int x = Integer.parseInt(yui);
for(int i = 0; i < x; i++){
int Index = generator.nextInt(znaki.length);
haslo = znaki[Index] + haslo;
}
I have already tried doing an if structure:
if (haslo0 != null){
haslo0.setText(pustak);
haslo0.setText(haslo);
}
else
haslo0.setText(haslo);
But it doesn't help :(
When I want to have 7 chars in the password and click the button first time, the result is correct e.g. PKAjzQL. But when I click the button second time, the result is nBzcRjQPKAjzQL instead of nBzcRjQ.
Why are you appending the old string haslo behind the newly generated one in haslo = znaki[Index] + haslo;
Probably that's why you are getting output like that. Can you please try just setting newly generated password into the textview like
haslo = znaki[Index];
And then try to set text in the text view using haslo0.setText(haslo);
How can I "delete" the old text (password) from the TextView and replace it with the new one using the same button?
The problem is not to "delete" the old text, the problem is that you have to clear the list for example, every time user clicks on the Button you clear the list doing : znaki.clear(), then it will only show the new password generated.
If you see your output :
First output :
PKAjzQL --> This is correct
Second output :
nBzcRjQPKAjzQL --> this is the new output + the old one
Can you give the code of the OnClickButton? And why are you setting the same TextView with diferents Strings when you click?
haslo0.setText(pustak);
haslo0.setText(haslo);
?

Iterating over drawables in Android

In Android, I have only found answers as to how to open a single specific Drawable from MainActivity.java, but not how to iterate over each Drawable from res/drawables. The Drawables names do not follow any patterns (e.g. being numbered from 0 to 25), so the answer suggested here sadly doesn't solve my problem. Does anyone know how to do the latter?
Thank you in advance :)
First, put your drawables into an arrays
<array name="dashboard_item_menu_drawable">
<item>#drawable/ic_file_green</item>
<item>#drawable/ic_email_green</item>
<item>#drawable/ic_linear_scale_green</item>
<item>#drawable/ic_undo_green</item>
<item>#drawable/ic_check_circle_green</item>
<item>#drawable/ic_archive_green</item>
</array>
Then, iterate your array drawables
val icons = ArrayList<Int>()
val arr = resources.obtainTypedArray(R.array.dashboard_item_menu_drawable)
(0 until arr.length()).forEach {
// get resource id of each drawable
val icon = arr.getResourceId(it, -1)
icons.add(icon)
}
Next, recycles resource
arr.recycle()
Then you can use your drawable
iconView.setImageDrawable(ContextCompat.getDrawable(this, icons[index]))
If you want to iterate through drawables that have similar names like: image1, image2, ..., image10 you can do it like this:
for (int i = 0; i < 10; i++) {
int id = getResources().getIdentifier("image" + i, "drawable", getPackageName());
Drawable d = ContextCompat.getDrawable(this, id);
// your code here
}
The easiest way to do it is to put the names of your drawables in a String array:
String[] symbols = {"first_img", "second_img", "third_img", "fourth_img"};
Then iterate over them like this (I put the images into a GridLayout):
for(String symbol : symbols) {
int id = getResources().getIdentifier(symbol, "drawable", getPackageName());
ImageView img = new ImageView(this);
LinearLayout.LayoutParams imgParams = new LinearLayout.LayoutParams(300, 300);
imgParams.setMargins(30, 30, 30, 30);
img.setLayoutParams(imgParams);
img.setBackgroundResource(id);
symbolGrid.addView(img);
}

Spinner from database - get value

I am learning Android and I use this tutorial: http://www.androidhive.info/2012/06/android-populating-spinner-data-from-sqlite-database/ to generate Spinner with data from database.
This working ok, but i can't get selected item.
final Spinner spinner = (Spinner) findViewById(R.id.spinner); //this is ok
and next i would like get selected item:
Log.i("test", spinner.getSelectedItem().toString());
this return me:
+++ LOG: entry corrupt or truncated
EDIT:
I try also:
Log.i("test", spinner.getSelectedView().toString());
but this return:
android.widget.TextView#410a2ae8
Try the following:
TextView textView = (TextView)spinner.getSelectedView();
String result = textView.getText().toString();

Inflater and loop not working as it should

I am having a problem with a custom view I am currently doing for an app on android, I know there are many questions related with inflaters, but I cannot get around this problem.
the inflater i working just fine, but it should be doing the loop 3 times and is only doing it 1 so I only get one view on my final layout.
the relevant part of the code is this one
void populate(String strcline, String url){
lLfD = (LinearLayout)findViewById(R.id.lLfD);
try{
JSONArray a1 = new JSONArray(strcline);
for(int i = 0; i < a1.length(); i++){
JSONArray a2 = a1.getJSONArray(i);
final String fUserId = a2.getString(0);
String userName = a2.getString(1);
String userPicture = url + a2.getString(2);
View child = getLayoutInflater().inflate(R.layout.cellevery, lLfD);
ImageView avatar = (ImageView)findViewById(R.id.cellAvatar);
downloadFile(userPicture, avatar);
TextView cellName = (TextView)findViewById(R.id.cellName);
cellName.setText(userName);
lLfD.addView(child);
}
}catch(Exception e){
}
pDialog.dismiss();
}
You look like you need to run findViewById only on the inflated view, otherwise it will just find the first one which is only the first one in your loop:
View child = getLayoutInflater().inflate(R.layout.cellevery, lLfD);
ImageView avatar = (ImageView)child.findViewById(R.id.cellAvatar);
downloadFile(userPicture, avatar);
TextView cellName = (TextView)child.findViewById(R.id.cellName);
cellName.setText(userName);
Here's an explanation of findViewById in your loop:
Loop 1:
1LfD->child1->R.id.cellAvatar (findViewById(R.id.cellAvatar) finds this one)
Loop 2:
1Lfd->
child1->R.id.cellAvatar
child2->R.id.cellAvatar (findViewById(R.id.cellAvatar) finds the child1.cellAvatar again)
Loop 3:
1LfD->
child1->R.id.cellAvatar
child2->R.id.cellAvatar
child3->R.id.cellAvatar (findViewById(R.id.cellAvatar) finds the child1.cellAvatar again)
by using child.findViewById(R.id.cellAvatar), it ensures that you find the correct R.id.cellAvatar for each run of the loop.
Does that make sense?
Update 2:
When you call:
getLayoutInflater().inflate(R.layout.cellevery, lLfD);
You are already setting the parent view as the second argument so you don't need to call:
lLfD.addView(child);

getLocationOnScreen crashes

I need to get the coordinates of my app on screen to use them as an offset for a popout.
View tempView = (View) findViewById(R.layout.main);
int[] loc = {0,0};
tempView.getLocationOnScreen(loc); /// crashes here!
Tried this code, but the last line of it makes the app crash. Maybe the tempView I'm getting from the main layout somehow doesn't correspond with the layout on screen?
Any suggestions... thanks! :)
added:
solved
int[] loc = new int[2];
View tempView = (View) findViewById(R.id.LL_whole);
tempView.getLocationOnScreen(loc);
post( String.valueOf(loc[1]) );
works! :)
Try checking to see if tempView is null. I suspect findViewById() is failing to find the id you've given it.
int[] loc = {0,0};
View tempView = (View) findViewById(R.layout.main);
if (tempView != null) {
tempView.getLocationOnScreen(loc);
} else {
Log.d("YourComponent", "tempView was null.");
}
In your XML you have something like this:
<SomeLayout android:id="#+id/myLayout" ... >
<SomeView android:id="#+id/myView" ... />
<SomeOtherView android:id="#+id/myOtherView" .../>
</SomeLayout>
So you want to say:
SomeView v = (SomeView)findViewById(R.id.myView);
Or perhaps:
SomeLayout l = (SomeLayout)findViewById(R.id.myLayout);
findViewById(R.layout.main)
You should place here R.id.something not R.layout.something.

Categories