何时使用适配器以及何时使用inflater

时间:2013-04-09 09:49:00

标签: android android-adapter layout-inflater android-contextmenu

我是Android的新手,我学习上下文菜单,但在浏览了上下文菜单后,我在Adapter和Inflater中有点混乱。我看到1个使用适配器的程序和1个使用Inflater的程序。那么,请帮助我如何/何时使用Adapter和Inflater。

以下是使用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;
}

1 个答案:

答案 0 :(得分:0)

这些类型有不同的用途。

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

<?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在为列表,网格等提供上下文菜单时提供额外信息。它允许您确定用户选择的项目(长按)。请注意,您的两个示例都使用此功能。