所以我有一个充满卡片的列表视图,我想要longPress每一张卡片,并能够分享卡片上的文本。我做过关于longPress事件的研究,这是我想出来的:
在listview上设置listener
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方法,但都不支持worked...help!
发布于 2014-09-03 13:59:50
您没有在showOptionsMenu(int position)中使用输入position。
在OnLongClick中,参数为:
parent The AbsListView where the click happened
view The view within the AbsListView that was clicked
position The position of the view in the list
id The row id of the item that was clicked您可以使用:
parent.getItemAtPosition(位置)
以获取该位置的物品。然后,您可以将该项目传递给其他方法,或者可能是Toast卡片名称。
或者直接使用view参数,即所单击的视图,然后显示所需的内容。
发布于 2014-09-03 13:58:19
您应该在适配器上调用getItem并将对象(即数据数组的position处的数据)传递给showOptionsMenu
https://stackoverflow.com/questions/25637147
复制相似问题