我的回收站视图适配器返回null

时间:2018-09-25 20:13:38

标签: java android android-recyclerview recycler-adapter

我正在尝试将RecyclerView与适配器一起使用来调用特定 人。在适配器中,我使用呼叫按钮并从中获取参考 PlaceCallActivity。问题是PlaceCallActivity是 使用我无法从适配器访问的上述值。预先感谢您的帮助。

我的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;
            }
        }
    };
}


和MyAdapter类:

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;
    }
}

在此适配器中,我正在使用按钮从PlaceCallActivity进行呼叫。

3 个答案:

答案 0 :(得分:1)

请更改此:

private RecyclerView.Adapter adapter;

对此:

private MyAdapter adapter;

答案 1 :(得分:0)

您可以创建一个界面:

public interface CallButtonClickListener{
    void onButtonClicked(String name);
}

修改

public class PlaceCallActivity extends BaseActivity {
    private Button mCallButton;
    private EditText mCallName;

    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;
    private List<ListItem> listItems;

    //new code
    private CallButtonClickListener buttonListener;

    @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);

         //implement the logic there
        adapter = new MyAdapter(listItems, this, new CallButtonClickListener(){
         @Override
         public void onButtonClicked(String name){
            //your code here.
         }
       });
        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;
            }
        }
    };
}

适配器

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private List<ListItem> listItems;
    private Context context;
    private static String str;
    private CallButtonClickListener buttonListener;

    public MyAdapter(List<ListItem> listItems, Context context, CallButtonClickListener buttonListener) {
        this.listItems = listItems;
        this.context = context;
        this.buttonListener = buttonListner;
    }

    @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();


            buttonListener.onButtonClicked(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;
    }
}

答案 2 :(得分:0)

创建活动的实例不会使其经历整个生命周期;如果使代码运行所必需的属性的声明取决于您活动的生命周期,那么在正确初始化的活动实例上调用方法应该可以解决此问题。

MyAdapter类:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private List<ListItem> listItems;
    private Context context;
    private PlaceCallActivity placeCallActivity;
    private static String str;

    public MyAdapter(List<ListItem> listItems, Context context) {
        this.listItems = listItems;
        this.context = context;
        // this.placeCallActivity now references the parent activity instance
        this.placeCallActivity=(PlaceCallActivity)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.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;
    }
}