无法使用Intent启动新活动

时间:2018-03-11 21:39:47

标签: java android android-intent android-activity

我正在调用一个活动,并且还将值从适配器传输到下一个活动,但是传输了值,但无法为活动加注星标,并且没有错误,只是下一个活动无法打开

持有人:

public class ContactReadHolder extends RecyclerView.ViewHolder {

    TextView contactName, contactPhone;
    ImageButton contactCall;
    public ContactReadHolder(View itemView) {
        super(itemView);

        context =itemView.getContext();

        contactName = (TextView) itemView.findViewById(R.id.contact_list_name);
        contactPhone = (TextView) itemView.findViewById(R.id.contact_list_phone);
        contactCall = (ImageButton) itemView.findViewById(R.id.contact_list_call);
    }
}

这是我的适配器代码,我已经在onBindViewHolder中实现了listner,但它仍然无法工作,而且我也尝试过Holder类

适配器:

public class ContactReadAdapter extends RecyclerView.Adapter<ContactReadAdapter.ContactReadHolder> {

    private LayoutInflater layoutInflater;
    private List<ContactListModel> contactListModelList;
    private Context context;

    public ContactReadAdapter(List<ContactListModel> contactListModelList, Context context) {

        this.contactListModelList = contactListModelList;
        this.context = context;
    }


    @NonNull
    @Override
    public ContactReadHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        layoutInflater = LayoutInflater.from(context);
        View view = layoutInflater.inflate(R.layout.abc_contact_layout, parent, false);

        ContactReadHolder contactReadHolder = new ContactReadHolder(view);
        return contactReadHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ContactReadHolder holder, int position) {

        final TextView contactName, contactPhone;

        contactName = holder.contactName;
        contactPhone = holder.contactPhone;

        contactName.setText(contactListModelList.get(position).getModel_contact_name());
        contactPhone.setText(contactListModelList.get(position).getModel_contact_phone());

        holder.contactCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.d("duit","clicked ");
                Intent intent=new Intent(context,OutgoingCallActivity.class);
                String string_contactName=holder.contactName.getText().toString();
                String string_contactPhone=holder.contactPhone.getText().toString();

                Log.d("duit","clicked " + string_contactName +" "+  string_contactPhone);
                intent.putExtra("user_name",string_contactName);
                intent.putExtra("calleeNumber", string_contactPhone);

                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return contactListModelList.size();
    }

3 个答案:

答案 0 :(得分:1)

试试这个

 Intent intent=new Intent(v.getContext(),OutgoingCallActivity.class);
 // put extra methods
 v.getContext().startActivity(intent);

答案 1 :(得分:0)

onBindViewHolder 功能上写下 onclicklistener ,它会起作用。

答案 2 :(得分:0)

       Intent intent=new Intent(contactName.getContext(),OutgoingCallActivity.class);
        String string_contactName=holder.contactName.getText().toString();
        String string_contactPhone=holder.contactPhone.getText().toString();

        Log.d("duit","clicked " + string_contactName +" "+  string_contactPhone);
        intent.putExtra("user_name",string_contactName);
        intent.putExtra("calleeNumber", string_contactPhone);

        contactName.getContext().startActivity(intent);

               Intent intent=new Intent(holder.contactName.getContext(),OutgoingCallActivity.class);
            String string_contactName=holder.contactName.getText().toString();
            String string_contactPhone=holder.contactPhone.getText().toString();

            Log.d("duit","clicked " + string_contactName +" "+  string_contactPhone);
            intent.putExtra("user_name",string_contactName);
            intent.putExtra("calleeNumber", string_contactPhone);

            holder.contactName.getContext().startActivity(intent);