【发布时间】:2014-09-03 05:53:32
【问题描述】:
所以我有一个充满卡片的列表视图,我想长按每一个并能够分享卡片上的文字。我对 longPress 事件进行了研究,这就是我想出的:
在列表视图上设置监听器
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
showOptionsMenu(position);
return false;
}
});
这里是 showOptionsMenu 方法
public void showOptionsMenu(int position) {
new AlertDialog.Builder(this).setCancelable(true).setItems(R.array.longClickItems,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
TextView tvMessage = (TextView) findViewById(R.id.tvContent);
TextView tvName = (TextView) findViewById(R.id.tvTitle);
String message = tvMessage.getText().toString();
String name = tvName.getText().toString();
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "Message:\n" + message + "\n" + "\nSigned by:\n" + name;
shareBody = shareBody.replace("-", "");
sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Send To:"));
}
}).show();
}
现在,这一切都很好,除了有一个问题:每次我长按时,无论列表项的位置如何,检索到的文本总是来自列表中的第一项。我尝试了 listview 类中的一些 getPosition 方法,但没有任何效果...感谢您的帮助!
【问题讨论】:
-
返回假;这是什么
-
哪个xml有
tvContent和tTitle? -
Plus 你在函数
showOptionsMenu中传递position,但不要使用它。请纠正你的逻辑。根据位置初始化 textview 并相应地检索文本。 -
请使用函数showOptionsMenu(position)中的位置;
标签: android android-listview onitemclicklistener