Search code examples
androidandroid-adapterlayout-inflaterandroid-contextmenu

when to use adapter and when to use inflater


I'm newbie in Android and i learning context menu but after surfing about context menu i have little bit confusion in Adapter and Inflater. I saw 1 program with using adapter and 1 using Inflater. So, please help me how/when to use Adapter and Inflater.

Here is an example using 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;
    }
}

Another example using adapter:

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

Solution

  • These types serve different purposes.

    The MenuInflator converts XML files into a Menu object representing the on-screen layout of the menu. In the first example, R.menu.context_menu refers to an associated XML file at res/menu/context_menu.xml that defines the choices that will appear in the menu. See Menu Resource for the format of XML menu resources. Here's a simple example:

    <?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>
    

    The AdapterContextMenuInfo provides extra information when a context menu is brought up for a list, grid, etc. It allows you to determine which item the user selected (long pressed). Notice that both of your examples use this.