自定义列表适配器中的剪贴板管理器
Posted
技术标签:
【中文标题】自定义列表适配器中的剪贴板管理器【英文标题】:Clipboardmanager Within Custom List Adapter 【发布时间】:2016-04-20 23:36:48 【问题描述】:想要在自定义列表适配器中实现onClickListener
,该适配器将使用剪贴板从listView
的textView
复制。我已经尝试了下面显示的代码,但是当我尝试单击 listView 中的 textView 时,我总是得到一个空值。应用程序崩溃,堆栈跟踪如下:
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)
这是我的部分代码:
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;
实现上述例程的正确方法是什么?任何可以帮助我找到答案的帮助都是可观的。我还可以在 textView 中选择一些文本来复制和粘贴吗?
【问题讨论】:
抱歉,我已经添加了 onClickListener,因为我很早就省略了它,我认为这是我的问题被否决的原因。 【参考方案1】:可能它会帮助那里的人,我所做的是从view
的onClickListener(View view)
中捕获contex
。
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();
);
【讨论】:
【参考方案2】:Activity activity;
statusMsg .setOnClickListener(new View.OnClickListener()
public void onClick(View view)
Intent mail = new Intent(activity,yourActivity.class);
//do what you want here
activity.startActivity(i)
);
【讨论】:
这只是开始一个新的活动。我想复制粘贴。谢谢 这个监听器有什么意义?为什么它会解决 OP 的问题? @Alfabravo 你能帮忙吗?谢谢。 @MandeKira 抱歉,我没有答案,希望更多人来帮忙。期待 nitesh 提供更有见地的帮助。 Nitesh 只开始一个新活动,代码完全错误。以上是关于自定义列表适配器中的剪贴板管理器的主要内容,如果未能解决你的问题,请参考以下文章