自定义列表适配器中的剪贴板管理器
Clipboardmanager Within Custom List Adapter
想要在自定义列表适配器中实现一个 onClickListener
,它将使用 Clipboard 从 listView
的 textView
复制。我已经尝试了下面显示的代码,但是当我尝试单击 listView 中的 textView 时,我总是得到一个 null。应用程序崩溃,堆栈跟踪如下:
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.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 中的一些文本可选择复制和粘贴吗?
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)
}
});
可能会对外面的人有所帮助,我所做的是从 onClickListener(View 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();
}
});
想要在自定义列表适配器中实现一个 onClickListener
,它将使用 Clipboard 从 listView
的 textView
复制。我已经尝试了下面显示的代码,但是当我尝试单击 listView 中的 textView 时,我总是得到一个 null。应用程序崩溃,堆栈跟踪如下:
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.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 中的一些文本可选择复制和粘贴吗?
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)
}
});
可能会对外面的人有所帮助,我所做的是从 onClickListener(View 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();
}
});