androidandroid-listviewlistviewitemcustom-adapterclipboardmanager

Clipboardmanager Within Custom List Adapter


Want to implement an onClickListener within a custom list adapter that will copy from a textView of the listView using Clipboard. I have tried the code shown below but I always get a null when I tried to click on the textView in the listView. The application crashes and the stack trace is as follows:

01-15 12:00:23.237  22526-22526/com.ivotism.ivotism E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ivotism.ivotism, PID: 22526
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
        at com.ivotism.ivotism.adapter.FeedListAdapter$5.onClick(FeedListAdapter.java:377)
        at android.view.View.performClick(View.java:4848)
        at android.view.View$PerformClick.run(View.java:20262)
        at android.os.Handler.handleCallback(Handler.java:815)
        at android.os.Handler.dispatchMessage(Handler.java:104)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5637)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

Here is part of my code:

private Activity activity;
    private LayoutInflater inflater;
    private List<FeedItem> feedItems;
    private ListView listView;
    Context context;

public FeedListAdapter(Activity activity, List<FeedItem> feedItems) {
    this.activity = activity;
    this.feedItems = feedItems;
    session = new SessionManager(activity.getApplicationContext());
    //this.context = context;
}
public FeedListAdapter(Context context){
    this.context = context;
}

@Override
public int getCount() {
    retu

rn feedItems.size();
    }

    @Override
    public Object getItem(int location) {
        return feedItems.get(location);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

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



    //        session = new SessionManager(MediaActivity.class);
                if (inflater == null)

                inflater = (LayoutInflater) activity
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if (convertView == null)
                convertView = inflater.inflate(R.layout.feed_item, null);

            if (imageLoader == null)
                imageLoader = AppController.getInstance().getImageLoader();

            final TextView statusMsg = (TextView)convertView.findViewById(R.id.txtStatusMsg);

            final FeedItem item = feedItems.get(position);
    statusMsg.setTextIsSelectable(true);

 statusMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                    final android.content.ClipboardManager clipboardManager = (android.content.ClipboardManager) context
                            .getSystemService(Context.CLIPBOARD_SERVICE);
                    final android.content.ClipData clipData = android.content.ClipData
                            .newPlainText("Copy", statusMsg.getText());
                    clipboardManager.setPrimaryClip(clipData);
                    Toast.makeText(context, "Copied to clipboard", Toast.LENGTH_SHORT).show();

                } else {
                    final android.text.ClipboardManager clipboardManager = (android.text.ClipboardManager) context
                            .getSystemService(Context.CLIPBOARD_SERVICE);
                    clipboardManager.setText(statusMsg.getText());
                    Toast.makeText(context, "Copied to clipboard", Toast.LENGTH_SHORT).show();

                }



                //ClipboardManager cm = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
                //cm.setText(item.getStatus());
                //Toast.makeText(context, "Copied to clipboard", Toast.LENGTH_SHORT).show();
            }
        });


            // Chcek for empty status message
            if (!TextUtils.isEmpty(item.getStatus())) {
                // Making url clickable
                statusMsg.setText(item.getStatus());

                statusMsg.setVisibility(View.VISIBLE);


     } else {
                    // status is empty, remove from view
                    statusMsg.setVisibility(View.GONE);
                }



return convertView;
        }

What is the right way to implement the routine above? Any help that will point me towards the answer is appreciable. Can I also make some text within the textView electable to copy and paste?


Solution

  • May be it will help someone out there, what I did was to capture the contex from the view of the onClickListener(View view).

    statusMsg.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View view) {
                    //Intent intent = new Intent(view.getContext(), MainActivity.class);
    // THIS IS WHAT I DID, GET CONTEXT FROM VIEW
                    context = view.getContext();
    
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                        final android.content.ClipboardManager clipboardManager = (android.content.ClipboardManager) context
                                .getSystemService(Context.CLIPBOARD_SERVICE);
                        final android.content.ClipData clipData = android.content.ClipData
                                .newPlainText("Copy", statusMsg.getText());
                        clipboardManager.setPrimaryClip(clipData);
                        Toast.makeText(context, "Copied to clipboard", Toast.LENGTH_SHORT).show();
    
                    } else {
                        final android.text.ClipboardManager clipboardManager = (android.text.ClipboardManager) context
                                .getSystemService(Context.CLIPBOARD_SERVICE);
                        clipboardManager.setText(statusMsg.getText());
                        Toast.makeText(context, "Copied to clipboard", Toast.LENGTH_SHORT).show();
    
                    }
    
    
    
                    //ClipboardManager cm = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
                    //cm.setText(item.getStatus());
                    //Toast.makeText(context, "Copied to clipboard", Toast.LENGTH_SHORT).show();
                }
            });