Thursday, 11 July 2019

OnitemClick returns the wrong string value

Working with Firebase. I'm trying to populate an imageview,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 1 are actually working the rest of the items always return position 0. 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.getImageText());
            Glide.with(getBaseContext()).load(model.getImage()).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. Saving it to the user section of the data base example below

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

if i add more than 2 items to my database the list view will only allow me to select the first 2 items. the rest of the items return position 0 which in this case is avatarID = Batman1

****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 String getImage(){
    return url;
}

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

public String getImageText() {
    return name;
}

public void setImageTextString(String name) {
    this.name = name;
}
public String getID(){
    return id;
}
public void setId(String id){
    this.id = 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 all of which return Batman1

****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?



from OnitemClick returns the wrong string value

No comments:

Post a Comment