如何从Base适配器打开操作意图?

时间:2015-12-30 11:44:44

标签: android android-intent

我尝试从Intent打开纯文本BaseAdapter。不幸的是,应用程序在打开操作Intent时崩溃。

我也试过v.getContext().startActivity()。它仍然无效。

以下是我的代码:

String shareBody = "Here is the share content body";
                Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                sharingIntent.putExtra("address", "9898989898");
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
                sharingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(Intent.createChooser(sharingIntent, "Inviting Via..."));

收到以下错误消息:

   12-30 17:22:07.274    9819-9869/com.example.anupamsarfare.lykke_native_android E/NativeCrypto﹕ ssl=0x626ee910 cert_verify_callback x509_store_ctx=0x61bb0938 arg=0x0
12-30 17:22:07.274    9819-9869/com.example.anupamsarfare.lykke_native_android E/NativeCrypto﹕ ssl=0x626ee910 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_RSA
12-30 17:22:11.368    9819-9819/com.example.anupamsarfare.lykke_native_android E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.anupamsarfare.lykke_native_android, PID: 9819
    android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
            at android.app.ContextImpl.startActivity(ContextImpl.java:1129)
            at android.app.ContextImpl.startActivity(ContextImpl.java:1116)
            at android.content.ContextWrapper.startActivity(ContextWrapper.java:312)
            at com.example.anupamsarfare.lykke_native_android.adapters.FriendsAdapter$1.onClick(FriendsAdapter.java:123)
            at android.view.View.performClick(View.java:4474)
            at android.view.View$PerformClick.run(View.java:18781)
            at android.os.Handler.handleCallback(Handler.java:808)
            at android.os.Handler.dispatchMessage(Handler.java:103)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5292)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
            at dalvik.system.NativeStart.main(Native Method)

下面是我的整个适配器类:

package com.example.anupamsarfare.lykke_native_android.adapters;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Filter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.anupamsarfare.lykke_native_android.AppController;
import com.example.anupamsarfare.lykke_native_android.MainActivity;
import com.example.anupamsarfare.lykke_native_android.R;
import com.example.anupamsarfare.lykke_native_android.activities.Otp;
import com.example.anupamsarfare.lykke_native_android.models.ContactModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pratikgadekar on 12/30/15.
 */

public class FriendsAdapter extends BaseAdapter {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;

    private Context context;
    private List<ContactModel> mData;
    private LayoutInflater mInflater;
    private ImageLoader mImageLoader;
    List<ContactModel> mStringFilterList;
    ValueFilter valueFilter;


    public FriendsAdapter(Context context, List<ContactModel> mData) {
        this.context = context;
        this.mData = mData;
        this.mStringFilterList = mData;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public void addItem(ContactModel contactModel) {
        mData.add(contactModel);
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return mData.indexOf(getItem(position));
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (mImageLoader == null) {
            mImageLoader = AppController.getInstance().getImageLoader();
        }


        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.friends_item, null);
        }


        TextView externalId = (TextView) convertView.findViewById(R.id.externalId);
        NetworkImageView avatar = (NetworkImageView) convertView.findViewById(R.id.avatar);
        TextView username = (TextView) convertView.findViewById(R.id.username);
        Button invite = (Button) convertView.findViewById(R.id.invite);

        ContactModel currentData = mData.get(position);
        externalId.setText(currentData.getExternalId());
        username.setText(currentData.getPhoneBookName());


        if (!currentData.getPictureUrl().isEmpty()) {
            avatar.setImageUrl(currentData.getPictureUrl(), mImageLoader);
        } else {
            avatar.setDefaultImageResId(R.drawable.user_default);
        }


        if (currentData.getStatus() == 1) {
            invite.setVisibility(View.VISIBLE);
        } else {
            invite.setVisibility(View.INVISIBLE);
        }

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

                String shareBody = "Here is the share content body";
                Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                sharingIntent.putExtra("address", "9892781009");
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
                sharingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                if(v.getContext()!=null)
                v.getContext().startActivity(Intent.createChooser(sharingIntent, "Inviting Via..."));



            }
        });


        return convertView;
    }

    public Filter getFilter() {
        if (valueFilter == null) {
            valueFilter = new ValueFilter();
        }
        return valueFilter;
    }

    public class ValueFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();


            if (constraint != null && constraint.length() > 0) {
                List<ContactModel> filterList = new ArrayList<ContactModel>();
                for (int i = 0; i < mStringFilterList.size(); i++) {
                    if ((mStringFilterList.get(i).getPhoneBookName().toUpperCase())
                            .contains(constraint.toString().toUpperCase())) {

                        ContactModel contact = new ContactModel(mStringFilterList.get(i)
                                .getId(),
                                mStringFilterList.get(i).getExternalId(),
                                mStringFilterList.get(i).getPhone(),
                                mStringFilterList.get(i).getPhoneBookName(),
                                mStringFilterList.get(i).getPictureUrl(),
                                mStringFilterList.get(i).getStatus(),
                                mStringFilterList.get(i).getIsDirty(),
                                mStringFilterList.get(i).getRecentTransaction());

                        filterList.add(contact);
                    }
                }
                results.count = filterList.size();
                results.values = filterList;
            } else {
                results.count = mStringFilterList.size();
                results.values = mStringFilterList;
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            mData = (ArrayList<ContactModel>) results.values;
            notifyDataSetChanged();

        }
    }


}

2 个答案:

答案 0 :(得分:2)

您可能通过在构造函数中传递getApplicationContext()而不是getActivity()来初始化适配器。将其更改为getActivity()然后您应该可以致电:

context.startActivity(Intent.createChooser(sharingIntent, "Inviting Via..."));

此外,您不需要在适配器中保留LayoutInflater,它应该是getView()的本地变量。

答案 1 :(得分:1)

<强>定义

Activity activity;

像这样传递意图。您需要使用的任何地方。

Intent i = new Intent(activity, ABC.class);
i.putExtra("Name", "value");
activity.startActivity(i);