Search code examples
androidstringfinalonitemclick

OnItemclick loosing value of string


Good day everyone.

Unfortunately i don't have much experience with java and i run into a probably very basic issue. I am filling a list view and when i click one of the list items i want to send a string to another intent. but the value of the string is always the same even tough it show up correctly in the list view. I guess it has to do with the String getting overwritten every time a new item appears.

I want parts[3] to be send to another function in the onItemClick how would i accomplish that?

Here is the code

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            ViewHolder holder;
            final String uuidbeacon;
            final String pid;
            uuidbeacon = arrayL.get(position).getProximityUuid().toString();
            minorbeacon = arrayL.get(position).getMinor();
            rangebeacon = arrayL.get(position).getAccuracy();
            parts = fetchTums(uuidbeacon).split("::");

            if (convertView != null) {
                holder = (ViewHolder) convertView.getTag();
            }
            else{

                if(parts[0].equals("1")) {
                    holder = new ViewHolder(convertView = inflater.inflate(R.layout.tupple_monitoring, null));
                    holder.beacon_image.setImageDrawable(getTumImage("http://127.0.0.1/uploads/face/" + parts[4]));
                    holder.beacon_uuid.setText(parts[1]);
                    holder.beacon_txpower.setText(parts[3]);

                    if(parts[2].equals("1"))
                    {
                        holder.beacon_row.setBackgroundColor(Color.parseColor("#B6B6B6"));
                        holder.image_lock.setVisibility(View.VISIBLE);
                    }
                }
                if(parts[0].equals("0"))
                {
                    holder = new ViewHolder(convertView = inflater.inflate(R.layout.tupple_monitoringe, null));

                }
            }
            pid = parts[3];
            if (arrayL.get(position).getProximityUuid() != null)



            list.setOnItemClickListener(new AdapterView.OnItemClickListener()
            {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id )
                {
                    //list.setItemChecked(position, true);
                    //view.setBackgroundColor(Color.BLUE);
                    toastThis(pid);
                   if(parts[2].equals("1")){

                       pin(pid);
                   }
                   else{
                        if(pid != null){
                            openPage(parts[3], "", parts[1]);
                        }
                   }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }


        return convertView;
    }

Solution

  • You have to pick the value using the position received from onItemClick to get exact item clicked. In your case you will have to get the value at index (position) in your arrayL like below.

    String item = arrayL[position];