I am trying to use a RecyclerView with an adapter to call a specific
person. In the adapter I am using a call button and taking a reference from
PlaceCallActivity. The problem is that PlaceCallActivity is
using the above values which I can't access from the adapter. Thanks in advance for your help.
My PlaceCallActivity:
package techheromanish.example.com.videochatapp;
import com.sinch.android.rtc.calling.Call;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class PlaceCallActivity extends BaseActivity {
    private Button mCallButton;
    private EditText mCallName;
    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;
    private List<ListItem> listItems;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainn);
        //initializing UI elements
        //mCallName = (EditText) findViewById(R.id.callName);
        mCallButton = (Button) findViewById(R.id.callButton);
        //mCallButton.setEnabled(false);
        //mCallButton.setOnClickListener(buttonClickListener);
        Button stopButton = (Button) findViewById(R.id.stopButton);
        stopButton.setOnClickListener(buttonClickListener);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        listItems = new ArrayList<>();
//        loadRecyclerViewData();
        ListItem listItem = new ListItem(
                R.drawable.user,
                "Jawad",
                "Android Developer");
        listItems.add(listItem);
        ListItem listItem1 = new ListItem(
                R.drawable.user,
                "Malik",
                "Developer");
        listItems.add(listItem1);
        adapter = new MyAdapter(listItems, this);
        recyclerView.setAdapter(adapter);
    }
    // invoked when the connection with SinchServer is established
    @Override
    protected void onServiceConnected() {
        TextView userName = (TextView) findViewById(R.id.loggedInName);
        userName.setText(getSinchServiceInterface().getUserName());
        //mCallButton.setEnabled(true);
    }
    @Override
    public void onDestroy() {
        if (getSinchServiceInterface() != null) {
            getSinchServiceInterface().stopClient();
        }
        super.onDestroy();
    }
    //to kill the current session of SinchService
    private void stopButtonClicked() {
        if (getSinchServiceInterface() != null) {
            getSinchServiceInterface().stopClient();
        }
        finish();
    }
    //to place the call to the entered name
    public void callButtonClicked(String user) {
        //String userName = mCallName.getText().toString();
        if (user.isEmpty()) {
            Toast.makeText(this, "Please enter a user to call", Toast.LENGTH_LONG).show();
            return;
        }
        Call call = getSinchServiceInterface().callUserVideo(user);
        String callId = call.getCallId();
        Intent callScreen = new Intent(this, CallScreenActivity.class);
        callScreen.putExtra(SinchService.CALL_ID, callId);
        startActivity(callScreen);
    }
    private OnClickListener buttonClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.callButton:
                    String str = MyAdapter.getStr();
                    Toast.makeText(PlaceCallActivity.this, "" + str, Toast.LENGTH_SHORT).show();
                    //callButtonClicked(str);
                    break;
                case R.id.stopButton:
                    stopButtonClicked();
                    break;
            }
        }
    };
}
and MyAdapter Class:
package techheromanish.example.com.videochatapp;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<ListItem> listItems;
    private Context context;
    private static String str;
    public MyAdapter(List<ListItem> listItems, Context context) {
        this.listItems = listItems;
        this.context = context;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_view, parent, false);
        return new ViewHolder(v);
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final ListItem listItem = listItems.get(position);
        holder.imageViewProfile.setImageResource(listItem.getImage());
        holder.textViewName.setText(listItem.getName());
        holder.textViewBio.setText(listItem.getBio());
        /*Picasso.with(context)
                .load(listItem.getImageUrl())
                .into(holder.imageView);
*/
//        str = listItem.getName();
        holder.callButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "You Clicked " + listItem.getName(), Toast.LENGTH_SHORT).show();
                PlaceCallActivity placeCallActivity = new PlaceCallActivity();
                placeCallActivity.callButtonClicked(listItem.getName());
            }
        });
    }
    @Override
    public int getItemCount() {
        return listItems.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView textViewName, textViewBio;
        public ImageView imageViewProfile;
        public Button callButton;
        ViewHolder(View itemView) {
            super(itemView);
            textViewName = (TextView) itemView.findViewById(R.id.textViewName);
            textViewBio = (TextView) itemView.findViewById(R.id.textViewBio);
            imageViewProfile = (ImageView) itemView.findViewById(R.id.imageViewProfile);
            callButton = (Button) itemView.findViewById(R.id.callButton);
        }
    }
    public static String getStr() {
        return str;
    }
}
In this adapter I am using button to call from PlaceCallActivity.
 
     
     
    