'How to assign the textview value in to the button?

I have a function which is actually set the name into the text view, and I have a button listener which is actually defined inside the oncreate method. All the names have a unique object id. I need to get the object id of the selected name into the onclick function of the button and this object id is passing in the intent through bundle extras to the next activity. The function is defined outside the oncreate method. All the names and corresponding ids are coming through the web service call.

The function which sets the text view is

public void setMarker(){

  mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

    @Override
    public boolean onMarkerClick(Marker marker) {

      SaloonDetails = "";
      // Take some action here
      String snippet = marker.getSnippet();
      objectId = snippet;

      if (selectedMarker != null && !("currentLocation".equals(snippet))) {
        selectedMarker.setIcon(BitmapDescriptorFactory.fromResource(R.mipmap.map_red));
      }

      selectedMarker = marker;
      if (!("currentLocation".equals(snippet)))
        marker.setIcon(BitmapDescriptorFactory.fromResource(R.mipmap.map_blue));

      Log.i("TAG", "snippet::::" + snippet);

      if ("currentLocation".equals(snippet)) {
        selectedLayout.setVisibility(View.GONE);
        //button_bookmark.setVisibility(View.GONE);
        return false;
      } else {

        selectedLayout.setVisibility(View.VISIBLE);

        TextView nameView = ((TextView) selectedLayout
          .findViewById(R.id.name_txt_id));

        TextView addressView = ((TextView) selectedLayout
          .findViewById(R.id.address_txt_id));

        Button button = ((Button) selectedLayout
          .findViewById(R.id.selec_button_id));

        try {
          JSONObject jSalonData = (JSONObject)SalonData.get(snippet);

          nameView.setText(jSalonData.getString("SalonName"));

          // button.setText(jSalonData.getString("SalonName"));
          String  Slecteditem1= nameView.getText().toString();

          Log.i("Sltitem olemapsAivity",Slecteditem1);

        } catch (Exception e) {
          e.printStackTrace();
        }


        try {
          JSONObject jSalonData = (JSONObject)SalonData.get(snippet);
          Log.i("jSalonData",jSalonData.toString());

          City = jSalonData.optString("City");
          SaloonDetails = SaloonDetails + City;
          Zipcode = jSalonData.optString("Zipcode");
          SaloonDetails = SaloonDetails  +" "+ Zipcode;
          HouseNumber = jSalonData.optString("HouseNumber");
          SaloonDetails = SaloonDetails  +" "+ HouseNumber;
          Street = jSalonData.optString("Street");
          SaloonDetails = SaloonDetails +" "+ Street;
          Log.i("SaloonDetails", SaloonDetails);
          addressView.setText(SaloonDetails);

        } catch (Exception e) {
          e.printStackTrace();
        }
        return true;
      }
    }

  });
}

The onclick function of the button is

Button button = ((Button)         

selectedLayout.findViewById(R.id.selec_button_id));

button.setOnClickListener(new View.OnClickListener() {
    @Override
    // On click function
    public void onClick(View view) {

        Bundle extras = new Bundle();
        Button b = (Button)view;
        String buttonText = b.getText().toString();
        Log.i("buttonText",buttonText);
        //String Slecteditem = hm.get(buttonText);

        //Log.i("objectidddddd",Slecteditem);

        Intent intent = new Intent(view.getContext(), Salon_Detail.class);
        //extras.putString("Slecteditem", Slecteditem);
        startActivity(intent);
    }
});


Solution 1:[1]

Use setTag/getTag method of Button to get text according to clicked Button as:

1. Call setTag method by passing Slecteditem1 String:

  String  Slecteditem1= nameView.getText().toString();
  button.setTag(Slecteditem1);

2. In onClick method get selected text from Button using getTag().

 ...
 Bundle extras = new Bundle();
 extras.putString("Slecteditem", view.getTag().toString());
 startActivity(intent);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ρяσѕρєя K