0

我是 Android 的新手,我正在学习上下文菜单,但是在浏览了上下文菜单之后,我对 Adapter 和 Inflater 有点困惑。我看到 1 个使用适配器的程序和 1 个使用 Inflater 的程序。所以,请帮助我如何/何时使用 Adapter 和 Inflater。

这是一个使用充气机的例子......

public class MainActivity extends ListActivity {

    private String selectedName = "";
    private String[] nameList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        nameList = getResources().getStringArray(R.array.name_list);

        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, nameList));

        registerForContextMenu(getListView());

    }

    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        getMenuInflater().inflate(R.menu.context_menu, menu);
    }

    public boolean onContextItemSelected(MenuItem item) {

        AdapterContextMenuInfo adapInfo = (AdapterContextMenuInfo) item
                .getMenuInfo();
        selectedName = nameList[(int) adapInfo.id];

        switch (item.getItemId()) {
        case R.id.view:
            Toast.makeText(MainActivity.this,
                    "You have pressed View Context Menu for " + selectedName,
                    Toast.LENGTH_LONG).show();
            return true;
        case R.id.save:
            Toast.makeText(MainActivity.this,
                    "You have pressed Save Context Menu for " + selectedName,
                    Toast.LENGTH_LONG).show();
            return true;
        case R.id.edit:
            Toast.makeText(MainActivity.this,
                    "You have pressed Edit Context Menu for " + selectedName,
                    Toast.LENGTH_LONG).show();
            return true;
        case R.id.delete:
            Toast.makeText(MainActivity.this,
                    "You have pressed Delete Context Menu for " + selectedName,
                    Toast.LENGTH_LONG).show();
            return true;
        }
        return false;
    }
}

另一个使用适配器的例子:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Countries = getResources().getStringArray(R.array.Game);
    ListView list = (ListView) findViewById(R.id.list);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.listitem, Countries);
    list.setAdapter(adapter);
    registerForContextMenu(list);
}

public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    if (v.getId() == R.id.list) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        menu.setHeaderTitle(Countries[info.position]);
        String[] menuItems = getResources().getStringArray(
                R.array.contextmenu);
        for (int i = 0; i < menuItems.length; i++) {
            menu.add(Menu.NONE, i, i, menuItems[i]);
        }
    }
}

public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
            .getMenuInfo();
    int menuItemIndex = item.getItemId();

    String[] menuItems = getResources().getStringArray(R.array.contextmenu);
    String[] menuItems1 = getResources().getStringArray(R.array.game);
    String menuItemName = menuItems[menuItemIndex];
    String listItemName = menuItems1[info.position];
    // selectedName = nameList[(int) info.id];

    TextView text = (TextView) findViewById(R.id.textView1);
    text.setText(String.format("Selected %s for item %s", menuItemName,
            listItemName));
    return true;
}
4

1 回答 1

0

这些类型有不同的用途。

MenuInflatorXML 文件转换为表示菜单屏幕布局的 Menu 对象。在第一个示例中,R.menu.context_menu指的是一个关联的 XML 文件,res/menu/context_menu.xml它定义了将出现在菜单中的选项。有关XML 菜单资源的格式,请参阅菜单资源。这是一个简单的例子:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/open" android:title="Open"/>
    <item android:id="@+id/info" android:title="More Info"/>
    <item android:id="@+id/delete" android:title="Delete"/>
</menu>

AdapterContextMenuInfo为列表、网格等打开上下文菜单时,它会提供额外的信息。它允许您确定用户选择了哪个项目(长按)。请注意,您的两个示例都使用了它。

于 2013-04-14T19:02:00.307 回答