Home > Software engineering >  listview not getting data correctly
listview not getting data correctly

Time:04-24

okay, so i got a listView in my android app to display messages, everything has been going good ever since i got help on time but now i got a bug where the last message wont show.

the message part wont show if its a "system" message. this is linked to firebase.

but when i get the data it all works good for each item besides for the type of message.

// this is what the message looks like in the database 
  "messages": "hi",
  "number": "1",
  "pfp": "",
  "time": "2022-04-22T00:39:00.520Z",

// this tells the app what type of message it is
  "type": "user",


  "user_uid": "user id",
  "username": "frugalnormal"

even switched to the recycleview and same thing

note: i just put a toast to tell me if it thinks the message is system and sure enough every timeenter image description here i send a "user" message it tells me that its a system message and wont show that part but can still show the time

also the time is supposed to appear over a text message

Update: okay so I've tired pretty much everything now the last message shows but the app still thinks the last message is always a "system" message

And also so the switch returns both somehow "User" and "system" for some reason

update2: So now I'm really confused, scrolling through it makes the system message thing happen randomly on different messages

UPDATE 3 /////////////////////////////////////////////////////////////////////////////// CODE FOR THE WHOLE ADAPTER:

this is for applying the data to the view:

 _ch_child_listener = new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot _param1, String _param2) {
                GenericTypeIndicator<HashMap<String, Object>> _ind = new GenericTypeIndicator<HashMap<String, Object>>() {};
                final String _childKey = _param1.getKey();
                final HashMap<String, Object> _childValue = _param1.getValue(_ind);
                ch.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot _dataSnapshot) {
                        lop = new ArrayList<>();
                        try {
                            GenericTypeIndicator<HashMap<String, Object>> _ind = new GenericTypeIndicator<HashMap<String, Object>>() {};
                            for (DataSnapshot _data : _dataSnapshot.getChildren()) {
                                HashMap<String, Object> _map = _data.getValue(_ind);
                                lop.add(_map);
                            }
                        }
                        catch (Exception _e) {
                            _e.printStackTrace();
                        }
                        listview2.setAdapter(new Listview2Adapter(lop));
                        ((BaseAdapter)listview2.getAdapter()).notifyDataSetChanged();
                        x.add(_childKey);
                    }

and here is the rest:

public class Listview2Adapter extends BaseAdapter {

    ArrayList<HashMap<String, Object>> _data;

    public Listview2Adapter(ArrayList<HashMap<String, Object>> _arr) {
        _data = _arr;
    }

    @Override
    public int getCount() {
        return _data.size();
    }

    @Override
    public HashMap<String, Object> getItem(int _index) {
        return _data.get(_index);
    }

    @Override
    public long getItemId(int _index) {
        return _index;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public View getView(final int _position, View _v, ViewGroup _container) {
        LayoutInflater _inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View _view = _v;
        if (_view == null) {
            _view = _inflater.inflate(R.layout.messgebubble, listview2, false);
        }

        final LinearLayout time = _view.findViewById(R.id.time);
        final TextView textview1 = _view.findViewById(R.id.textview1);
        final LinearLayout linear4 = _view.findViewById(R.id.linear4);
        final TextView timetext = _view.findViewById(R.id.timetext);
        final LinearLayout left = _view.findViewById(R.id.left);
        final LinearLayout linear1 = _view.findViewById(R.id.linear1);
        final de.hdodenhof.circleimageview.CircleImageView circleimageview1 = _view.findViewById(R.id.circleimageview1);
        final TextView message = _view.findViewById(R.id.message);
        final ImageView imageview1 = _view.findViewById(R.id.imageview1);
        final TextView SystemText = _view.findViewById(R.id.systemtext);
        final LinearLayout system = _view.findViewById(R.id.system);


        Instant i = Instant.parse(_data.get(_position).get("time").toString());
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").withZone(ZoneId.systemDefault());;
        String Time = dateTimeFormatter.format(i);
        timetext.setText(Time);

        String messageType = lop.get(_position).get("type").toString();


        switch (messageType){
            case "system":

                SystemText.setText(_data.get(_position).get("messages").toString());

                Toast.makeText(GroupChatActiviy.this, "This is a system message", Toast.LENGTH_SHORT).show();

                system.setVisibility(View.VISIBLE);


                break;
            case "user":
                linear4.setVisibility(View.VISIBLE);

                if (_data.get((int)_position).get("messages").toString().equals("")) {

                    if (_data.get((int)_position).containsKey("image")) {
                        imageview1.setVisibility(View.VISIBLE);


                        if (_data.get((int)_position).get("user_uid").toString().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())) {
                            linear4.setGravity(Gravity.RIGHT);
                            linear1.setGravity(Gravity.RIGHT);
                        }
                        else {
                            linear4.setGravity(Gravity.LEFT);
                            linear1.setGravity(Gravity.LEFT);
                        }
                    }
                    else {

                    }
                }
                else {
                    message.setText(_data.get((int)_position).get("messages").toString());
                    message.setVisibility(View.VISIBLE);
                    if (lop.get((int)_position).get("user_uid").toString().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())) {
                        linear4.setGravity(Gravity.RIGHT);
                        linear1.setGravity(Gravity.RIGHT);
                        linear1.setBackgroundResource(R.drawable.start);
                    }
                    else {
                        linear4.setGravity(Gravity.LEFT);
                        linear1.setGravity(Gravity.LEFT);
                        linear1.setBackgroundResource(R.drawable.right);
                    }
                    if (_data.get((int)_position).containsKey("image")) {
                        imageview1.setVisibility(View.VISIBLE);


                    }
                    else {

                    }
                }

                String LastTimeText = _data.get((int)_position - 1).get("time").toString();
                Instant LastI = Instant.parse(LastTimeText);
                Duration Dur = Duration.between(LastI, i);
                if (Dur.getSeconds() <= 600) {

                }
                else {
                    if (lop.get((int)_position).get("user_uid").toString().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())) {

                    }
                    else {
                        circleimageview1.setVisibility(View.VISIBLE);
                        textview1.setVisibility(View.VISIBLE);
                        //Glide.with(getApplicationContext()).load(Uri.parse(_data.get((int)_position).get("pfp").toString())).into(circleimageview1);
                        textview1.setText(_data.get((int)_position).get("username").toString());
                    }
                    time.setVisibility(View.VISIBLE);

                }


                break;

        }

        if(_data.get((int)_position).get("number") != null){
            if (_data.get((int)_position).get("number").toString().equals("")) {

         }
            else {
                if (Numberrr < Double.parseDouble(_data.get((int)_position).get("number").toString())) {
                 Numberrr = Double.parseDouble(_data.get((int)_position).get("number").toString())   1;
              }
           }
        }


        return _view;
    }
}

CodePudding user response:

Your adapter is based on _data but the message type is being grabbed from lop. These two can periodically get out of sync as new data comes in. Make sure your adapter is only accessing row item information from _data and it should work as expected.

CodePudding user response:

Issue can be of visibility, whenever item render we should always hide everything like you can create group user and system and at first hide all views of it.on every bind view we will first hide everything and depending open the data will make particular all view visible and then do data setup.

Do on thing like below code in you adapter's get View method

 @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public View getView(final int _position, View _v, ViewGroup _container) {
        LayoutInflater _inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View _view = _v;
        if (_view == null) {
            _view = _inflater.inflate(R.layout.messgebubble, listview2, false);
        }

        final LinearLayout time = _view.findViewById(R.id.time);
        final TextView textview1 = _view.findViewById(R.id.textview1);
        final LinearLayout linear4 = _view.findViewById(R.id.linear4);
        final TextView timetext = _view.findViewById(R.id.timetext);
        final LinearLayout left = _view.findViewById(R.id.left);
        final LinearLayout linear1 = _view.findViewById(R.id.linear1);
        final de.hdodenhof.circleimageview.CircleImageView circleimageview1 = _view.findViewById(R.id.circleimageview1);
        final TextView message = _view.findViewById(R.id.message);
        final ImageView imageview1 = _view.findViewById(R.id.imageview1);
        final TextView SystemText = _view.findViewById(R.id.systemtext);
        final LinearLayout system = _view.findViewById(R.id.system);


        time.setVisibility(View.GONE);
        textview1.setVisibility(View.GONE);
        linear4.setVisibility(View.GONE);
        timetext.setVisibility(View.GONE);
        left.setVisibility(View.GONE);
        linear1.setVisibility(View.GONE);
        circleimageview1.setVisibility(View.GONE);
        message.setVisibility(View.GONE);
        imageview1.setVisibility(View.GONE);
        SystemText.setVisibility(View.GONE);
        system.setVisibility(View.GONE);

        Instant i = Instant.parse(_data.get(_position).get("time").toString());
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").withZone(ZoneId.systemDefault());;
        String Time = dateTimeFormatter.format(i);
        timetext.setText(Time);

        String messageType = lop.get(_position).get("type").toString();


        switch (messageType){
            case "system":

                SystemText.setText(_data.get(_position).get("messages").toString());

                Toast.makeText(GroupChatActiviy.this, "This is a system message", Toast.LENGTH_SHORT).show();

                system.setVisibility(View.VISIBLE);


                break;
            case "user":
                linear4.setVisibility(View.VISIBLE);

                if (_data.get((int)_position).get("messages").toString().equals("")) {

                    if (_data.get((int)_position).containsKey("image")) {
                        imageview1.setVisibility(View.VISIBLE);


                        if (_data.get((int)_position).get("user_uid").toString().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())) {
                            linear4.setGravity(Gravity.RIGHT);
                            linear1.setGravity(Gravity.RIGHT);
                        }
                        else {
                            linear4.setGravity(Gravity.LEFT);
                            linear1.setGravity(Gravity.LEFT);
                        }
                    }
                    else {

                    }
                }
                else {
                    message.setText(_data.get((int)_position).get("messages").toString());
                    message.setVisibility(View.VISIBLE);
                    if (lop.get((int)_position).get("user_uid").toString().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())) {
                        linear4.setGravity(Gravity.RIGHT);
                        linear1.setGravity(Gravity.RIGHT);
                        linear1.setBackgroundResource(R.drawable.start);
                    }
                    else {
                        linear4.setGravity(Gravity.LEFT);
                        linear1.setGravity(Gravity.LEFT);
                        linear1.setBackgroundResource(R.drawable.right);
                    }
                    if (_data.get((int)_position).containsKey("image")) {
                        imageview1.setVisibility(View.VISIBLE);


                    }
                    else {

                    }
                }

                String LastTimeText = _data.get((int)_position - 1).get("time").toString();
                Instant LastI = Instant.parse(LastTimeText);
                Duration Dur = Duration.between(LastI, i);
                if (Dur.getSeconds() <= 600) {

                }
                else {
                    if (lop.get((int)_position).get("user_uid").toString().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())) {

                    }
                    else {
                        circleimageview1.setVisibility(View.VISIBLE);
                        textview1.setVisibility(View.VISIBLE);
                        //Glide.with(getApplicationContext()).load(Uri.parse(_data.get((int)_position).get("pfp").toString())).into(circleimageview1);
                        textview1.setText(_data.get((int)_position).get("username").toString());
                    }
                    time.setVisibility(View.VISIBLE);

                }


                break;

        }

        if(_data.get((int)_position).get("number") != null){
            if (_data.get((int)_position).get("number").toString().equals("")) {

         }
            else {
                if (Numberrr < Double.parseDouble(_data.get((int)_position).get("number").toString())) {
                 Numberrr = Double.parseDouble(_data.get((int)_position).get("number").toString())   1;
              }
           }
        }


        return _view;
    }

 
  • Related