Inflater and loop not working as it should - java

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);

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);
?

Load multiple bitmaps in Android Studio one at time with threads

I'm currently working on a project which takes pictures from camera and saves the photoPath in a Database, this is working fine and actually I'm handling the memory correctly generating thumbnails when I load all images.
The thing I want is that I'm trying to dedicate threads to go loading images one per one in the layout, because for example, if I have 50, with normal load it shows the layout until all 50 are loaded, meanwhile it doesn't show anything.
I've already tried to implement this threads to load one, and then another one, but it's the same, until it loads all, it shows all, here my code:
String query = "Select id_reg_mem, information from Memory_REG where type = 'PHOTO' and id_memory =" + id_memory;
final Cursor resultado = memoryDB.rawQuery(query, null);
if(resultado.moveToFirst())
noPhotos = resultado.getCount();
for(int i=0; i<noPhotos; i++)
{
new Thread()
{
public void run()
{
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
updateNewPhoto(resultado.getString(1)); // I send the path from the DB to a method I made to add a tablerow with the image (inside an imageview)
}
});
}
}.start();
if(i + 1 == noPhotos)
break;
resultado.moveToNext();
}
Here this method, where I'm loading a single image to an ImageView inside a TableRow. This method is actually working to load all images from DB.
protected void updateNewPhoto(String path)
{
ImageView iv;
if(rows == null) {
rows = new ArrayList<TableRow>();
rows.add(new TableRow(TL.getContext()));
iv = new ImageView(TL.getContext());
iv.setLayoutParams(new TableRow.LayoutParams(250,250));
iv.setTag(DB_Utils.maxIDRegMem(memoryDB) - 1);
iv.setImageBitmap(getBitmapThumbnail(path));
rows.get(0).addView(iv);
TL.addView(rows.get(0));
count = 2;
}
else
{
iv = new ImageView(TL.getContext());
iv.setLayoutParams(new TableRow.LayoutParams(250,250));
iv.setTag(DB_Utils.maxIDRegMem(memoryDB) - 1);
iv.setImageBitmap(getBitmapThumbnail(path));
if(count == 2)
{
rows.get(rows.size() - 1).addView(iv);
count = 1;
}
else
{
rows.add(new TableRow(TL.getContext()));
rows.get(rows.size() - 1).addView(iv);
TL.addView(rows.get(rows.size() - 1));
count = 2;
}
}
Unfortunately for me, all images aren't shown until all of them are loaded (kind of 3 seconds it takes). I'm thinking in no limit of images, that's why I want a way where images be loaded one per one, not all together.
Any ideas you have? I appreciate your help.
Thanks.

findViewById with getIdentifier not working

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 ...

Why File.exists returns false positive

I have a weird problem with my android File.exists functionality. Below is my code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.fragment_file_browser, container);
DataBaseHelper tmpHelper = new DataBaseHelper(getActivity());
ButtonInfoContainer infoContainer = tmpHelper.getVideoWithId(videoId);
//Tmp way to use this. When UX is finished will create a better way.
File f = new File(Environment.getExternalStorageDirectory() + "/aslkfj");
Log.i(StaticValues.TAG, Environment.getExternalStorageDirectory() + "/aslkfj");
LinearLayout tmpView2 = (LinearLayout) view.findViewById(R.id.custom_recordings);
if(f.exists());
{
Log.i(StaticValues.TAG, "f exists");
String[] tmp = f.list();
View tmpView = null;
if(tmp != null)
{
for(int i = 0; i < tmp.length; i ++)
{
tmpView = inflater.inflate(R.layout.list_item, null);
TextView tmpText = (TextView) tmpView.findViewById(R.id.product_name);
tmpText.setText(tmp[i]);
tmpView2.addView(tmpView);
}
}
}
return view;
}
Everything is cool but the weird part is that File.exists returns a false positive for me. It always says that the file exists (just in this app though). the path given above /aslkfj does not exist but when I run this code my logcat outputs f exists. Why would something like that happen.
You have a semi-colon at the end of your if condition: -
if(f.exists()); <-- Semi-colon here is causing problem
Due to this, the following block is just a local block, which will be executed regardless of what your if condition evaluate to.
You have a critical typo:
if(f.exists());
The ; basically ignores the if statement.
This is very handy for quick conditionals like:
if(f.exists())
Log.i(StaticValues.TAG, "f exists");
But an oopies here.

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