Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnitemClick returns the wrong string value

Working with Firebase. I'm trying to populate an image view, Textview using Firebase and a custom listview. when the user selects an item from the list it is supposed to save the id of the Firebase item to the user section of my database.

All items populate correctly in the list view but when i select an item only position 0 and 5 are actually working the rest of the items always return pos 1-5 looping back. can you please take a look at my code n tell me what I've done wrong thanks :)

private void Display_Images() {

    ListView listOfImages = findViewById(R.id.avatar_list);
    listOfImages.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            FirebaseAuth mAuth  =FirebaseAuth.getInstance();
            String userid = mAuth.getUid();
            avatar_id = AVATAR_ID.get(position);
            DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("Users").child(userid);
            db.child("avatarID").setValue(avatar_id);
            finish();
        }
    });

    adapter = new FirebaseListAdapter<Image_Selector>(this, Image_Selector.class,
            R.layout.avatars, FirebaseDatabase.getInstance().getReference().child("Avatars")) {
        @Override
        protected void populateView(View v, Image_Selector model, int position) {
            imageView = v.findViewById(R.id.image);
             tv = v.findViewById(R.id.tvavatar);
             AVATAR_ID.add(model.getID());
            tv.setText(model.getName());
            Glide.with(getBaseContext()).load(model.getUrl()).into(imageView);

        }


    };
    listOfImages.setAdapter(adapter);
}

This is my database

 "Avatars" : {
"Batman1" : {
  "id" : "Batman1",
  "name" : "Batman Logo",
  "url" : "url to image"
},
"Default" : {
  "id" : "Default",
  "name" : "Default",
  "url" : "url to image"
},
"Test" : {
  "id" : "Test",
  "name" : "TEST",
  "url" : ""
}

I'm trying to save the id as a string when an item in listview is clicked. Then Saving it to the user section of my database. example below.

"Users" : {
"F3vHZSClnPhE9cDjPeY5x0PuTmz1" : {
  "Username" : "Username Appears here.",
  "avatarID" : "here is where the id should be saved",
},

****EDIT****

this is my firebase model class

 public class Image_Selector {
String name;
String url;
String id;
public Image_Selector(String name,String url,String id){
   this.name = name;
   this.url = url;
   this.id = id;

}
public Image_Selector(){

}
 public void setName(String name) {
    this.name = name;
}

public void setUrl(String url) {
    this.url = url;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public String getUrl() {
    return url;
}

public String getId() {
    return id;
}

**** EDIT ****

Example of what is happening is if i select batman the id returned is batman1. but if i select Test item it still returns the id Batman 1. if i select Default it returns id default. I've tried adding a few more items to the database and Depending on the height and width of my custom listviewitem.xml will make a sort of loop. Only allowing me to capture ids that are from a list view item i can see. any items off screen will return the wrong id looping back to pos 0,1,2,3 and so on.

****EDIT****

Added Logs to my onitem click method

 String pos =  String.valueOf(position);
            Log.i("String_ID ",avatar_id);
            Log.i("String_POSITION ",pos);
            Log.i("String_USERID",userid);

The results came back as follows for each of the three items

item 1 in my listview is batman and returns

2019-07-11 12:04:51.152 3685-3685/studios.p9p.chatomatic.chat_o_matic 
I/String_ID: Batman1
2019-07-11 12:04:51.152 3685-3685/studios.p9p.chatomatic.chat_o_matic 
I/String_POSITION: 0
2019-07-11 12:04:51.152 3685-3685/studios.p9p.chatomatic.chat_o_matic 
I/String_USERID: F3vHZSClnPhE9cDjPeY5x0PuTmz1

item 2 in my listview is default and returns

2019-07-11 12:06:22.920 3685-3685/studios.p9p.chatomatic.chat_o_matic 
I/String_ID: Default
2019-07-11 12:06:22.935 3685-3685/studios.p9p.chatomatic.chat_o_matic 
I/String_POSITION: 1
2019-07-11 12:06:22.935 3685-3685/studios.p9p.chatomatic.chat_o_matic 
I/String_USERID: F3vHZSClnPhE9cDjPeY5x0PuTmz1

item 3 in my listview is test and returns

2019-07-11 12:07:18.983 3685-3685/studios.p9p.chatomatic.chat_o_matic 
I/String_ID: Batman1
2019-07-11 12:07:18.984 3685-3685/studios.p9p.chatomatic.chat_o_matic 
I/String_POSITION: 2
2019-07-11 12:07:18.984 3685-3685/studios.p9p.chatomatic.chat_o_matic 
I/String_USERID: F3vHZSClnPhE9cDjPeY5x0PuTmz1

so from this i have figured its something to do with the avatarid string Ive added a log to the populate method to see if the actual model gives the right id in the first place

 String modelid = model.getID();
            Log.i("String_id_AtSource",modelid);

2019-07-11 12:20:34.504 3685-3685/studios.p9p.chatomatic.chat_o_matic 
I/String_id_AtSource: Test

and it gives the right id. so it has to be something to do with The Array AVATAR_ID?

*****EDIT 21/7/2019******

Ok so using the answer below it seemed to be fixed. but since ive added more items to the database. it seems still to be doing it. So from further testing, i can tell you when it happens..

all ids capture correctly until you scroll. this means that as long as all the list items fit on the screen they return the right id. hence why i thought it was fixed with the below answer.

i first had the imageview size set to 300dp squared so it was a big list view item hence only 2 items click working correctly. changing the size to 90dp squared made it so the 5 items fit.

the list items ids return in a loop from the first 5. e.g clicking item 6 returns id of item 0, item 7 returns id of item 1 n so on. Yet the position of the list view item and all data inside the list is correct

The first page all work correctly. I've removed the image view to try to see if it was perhaps the image was too big

The second page all ids return in a loop from the first page e.g. the item Mario returns the Id for Batman and the item spiderman returns id for DJ Lama

like image 900
markharrop Avatar asked Jul 08 '19 21:07

markharrop


2 Answers

try fixing Imageview height and width.

imageView = v.findViewById(R.id.image);
imageView.height:150;
imageView.width:150;
like image 116
Jin Thakur Avatar answered Nov 13 '22 13:11

Jin Thakur


Cause

The order in which populateView() is called for positions can't be predicted unless all the list fits in the screen. They won't be called all at once in the order of position otherwise. Therefore the list AVATAR_ID is invalid if the list is larger.


Solution

Replace avatar_id = AVATAR_ID.get(position);

with avatar_id = ((Image_Selector)parent.getItemAtPosition(position)).getID();

like image 1
Bertram Gilfoyle Avatar answered Nov 13 '22 12:11

Bertram Gilfoyle