android contextmenu listview,Android ListView ContextMenu

问题

I'm trying to create a ContextMenu when user tap hold on an item in my ListView. However my code doesn't show anything when I tap hold on my item. Could somebody please check on my code. Thank you.

public class MyD extends SherlockListFragment implements

ActionBar.TabListener {

private File file;

private List myList;

private Fragment mFragment;

private ListView DLListView;

private ArrayAdapter listAdapter;

@Override

public void onActivityCreated(Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);

getActivity().setContentView(R.layout.dl_listview);

View empty = getActivity().findViewById(R.id.empty);

DLListView = (ListView) getActivity().findViewById(R.id.DLListView);

DLListView.setEmptyView(empty);

if (!Environment.getExternalStorageState().equals(

Environment.MEDIA_MOUNTED)) {

Toast.makeText(getActivity(),

"Error! No SDCARD Found!",

Toast.LENGTH_LONG).show();

} else {

File directory = new File(Environment.getExternalStorageDirectory()

+ File.separator + "St");

directory.mkdirs();

}

DLListView = (ListView) getActivity().findViewById(R.id.DLListView);

myList = new ArrayList();

File sdCard = Environment.getExternalStorageDirectory();

file = new File(sdCard.getAbsolutePath() + "/St/");

File list[] = file.listFiles();

for (int i = 0; i < list.length; i++) {

String fileName = list[i].getName();

fileName = fileName.replace(".txt", "");

myList.add(fileName);

}

listAdapter = new ArrayAdapter(getActivity(),

R.layout.simplerow, myList);

DLListView.setAdapter(listAdapter);

DLListView.setOnItemClickListener(new OnItemClickListener() {

@Override

public void onItemClick(AdapterView> parent, View view,

int position, long id) {

// TODO Auto-generated method stub

String textToPass = myList.get(position);

Intent i = new Intent(getActivity(), ViewActivity.class);

textToPass = textToPass.replace(textToPass + "", textToPass

+ ".txt");

i.putExtra("textToPass", textToPass);

startActivity(i);

}

});

DLListView.setOnItemLongClickListener(new OnItemLongClickListener() {

@Override

public boolean onItemLongClick(AdapterView> parent, View view,

int position, long id) {

// TODO Auto-generated method stub

return false;

}

});

}

public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)

{

super.onCreateContextMenu(menu, v, menuInfo);

AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;

String name = ((TextView) info.targetView).getText().toString();

menu.setHeaderTitle(name);

menu.add(0,v.getId(), 0, "Play");

menu.add(0,v.getId(),1,"Delete");

}

回答1:

Try this:

DLListView.setOnItemLongClickListener(new OnItemLongClickListener() {

@Override

public boolean onItemLongClick(AdapterView> parent, View view,

int position, long id) {

// TODO Auto-generated method stub

registerForContextMenu( view );

openContextMenu( view );

return false;

}

});

Even you can use closeContextMenu() if you want to close. Hope this helps.

回答2:

If you mean longClick, you're lacking the OnItemLongClickListener().

And, to call the Context menu, check this: how to call context menu

回答3:

You must register the ViewGroup (DLListView) to recognize the component in the menu.

DLListView = (ListView) getActivity().findViewById(R.id.DLListView);

registerForContextMenu(DLListView);

With these changes is not necessary to register each element in the ListView component.

来源:https://stackoverflow.com/questions/14035550/android-listview-contextmenu

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值