Android ListView ContextMenu

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android ListView ContextMenu相关的知识,希望对你有一定的参考价值。

我正在尝试创建一个ContextMenu当用户点击我的ListView中的项目。但是当我按住我的项目时,我的代码没有显示任何内容。有人请检查我的代码。谢谢。

public class MyD extends SherlockListFragment implements
        ActionBar.TabListener {

    private File file;
    private List<String> myList;

    private Fragment mFragment;

    private ListView DLListView;
    private ArrayAdapter<String> 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<String>();

        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<String>(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"); 
    }
答案

试试这个:

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;
    }
});

如果你想关闭,即使你可以使用closeContextMenu()。希望这可以帮助。

另一答案

如果你的意思是longClick,你就缺少OnItemLongClickListener()

并且,要调用上下文菜单,请检查:how to call context menu

另一答案

您必须注册ViewGroup(DLListView)才能识别菜单中的组件。

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

使用这些更改不需要注册ListView组件中的每个元素。

以上是关于Android ListView ContextMenu的主要内容,如果未能解决你的问题,请参考以下文章

Android——listview android:cacheColorHint,android:listSelector属性作用

38.Android之ListView简单学习

Android listview 的用法? 越详细越好。

Android——ListView

ListView / SimpleAdapter 不刷新 - Android

android 怎么让listview 不够一屏 占满一屏