Tecnologia do Blogger.
RSS

[androidbrasil-dev] Master/Detail SharedPreferences

Fala, pessoal! Beleza?

Bom, primeiramente já digo que sou COMPLETAMENTE noob em desenvolvimento Java e Android.

Preciso fazer um app que vai usar o flow Master/Detail. Com isso, depois de fuçar bastante, fiz a 'troca' de activities de uma forma, digamos assim, peculiar.
Tenho uma classe Item que retorna pra mim os itens da lista que chamará os fragments. Casa Item recebe um título e o ID de um fragment:


public class Item implements Serializable
{
    private static final long serialVersionUID = -6099312954099962806L;

    private String title;
    private Integer fragment;

    public Item(String title, Integer fragment)
    {
        this.title = title;
        this.fragment = fragment;
    }

    public String getTitle()
    {
        return this.title;
    }

    public Integer getFragment()
    {
        return this.fragment;
    }

    public static ArrayList<Item> getItems(Context context)
    {
        Resources resources = context.getResources();

        ArrayList<Item> items = new ArrayList<Item>();
        items.add(new Item(resources.getString(R.string.highway_title), R.layout.fragment_highway));
        items.add(new Item(resources.getString(R.string.work_section_title), R.layout.fragment_work_section));
        items.add(new Item(resources.getString(R.string.work_body_title), R.layout.fragment_work_body));
        items.add(new Item(
            resources.getString(R.string.vegetation_terrain_title),
            R.layout.fragment_vegetation_terrain
        ));
        items.add(new Item(
            resources.getString(R.string.upstream_downstream_title),
            R.layout.fragment_upstream_downstream
        ));
        items.add(new Item(resources.getString(R.string.sketch_title), R.layout.fragment_sketch));
        items.add(new Item(resources.getString(R.string.obs_title), R.layout.fragment_obs));

        return items;
    }

    @Override
    public String toString()
    {
        return getTitle();
    }
}

Nas outras classes, eu não faço IDEIA de como funciona o fluxo. Tipo, o que chama o que, o que é primeiro, etc. Já liguei o debugger, mas fiquei mais confuso ainda. =/
Só sei que na classe ItemDetailFragment, eu 'inflo' o fragment e ele é exibido na tela:


public class ItemDetailFragment extends Fragment
{
    private Item item;

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

        item = (Item) getArguments().getSerializable("item");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {

        return inflater.inflate(item.getFragment(), container, false);
    }

    public static ItemDetailFragment newInstance(Item item)
    {
        ItemDetailFragment fragmentDemo = new ItemDetailFragment();

        Bundle args = new Bundle();
        args.putSerializable("item", item);

        fragmentDemo.setArguments(args);

        return fragmentDemo;
    }
}


Com isso, eu queria saber direito como funciona esse fluxo para depois, salvar e recuperar o que foi 'inputado' nas telas, pois na última tela, eu salvarei o que foi inputado em todas as outras num SQLite.
Não faço ideia de como fazer isso, estou quase louco já.
HAUHAUHUA

Segue o resto do código:

public class ItemDetailActivity extends FragmentActivity
{
    ItemDetailFragment fragmentItemDetail;

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

        setContentView(R.layout.activity_item_detail);
        Item item = (Item) getIntent().getSerializableExtra("item");

        if (savedInstanceState == null) {
            fragmentItemDetail = ItemDetailFragment.newInstance(item);
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.flDetailContainer, fragmentItemDetail);
            ft.commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.item_detail, menu);

        return true;
    }
}


import ItemsListFragment.OnItemSelectedListener;

public class ItemsListActivity extends FragmentActivity implements OnItemSelectedListener
{
    private boolean isTwoPane = false;

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

        setContentView(R.layout.activity_items);
        determinePaneLayout();
    }

    private void determinePaneLayout()
    {
        FrameLayout fragmentItemDetail = (FrameLayout) findViewById(R.id.flDetailContainer);

        if (fragmentItemDetail != null) {
            this.isTwoPane = true;
            ItemsListFragment fragmentItemsList =
                (ItemsListFragment) getSupportFragmentManager().findFragmentById(R.id.fragmentItemsList);
            fragmentItemsList.setActivateOnItemClick(true);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.items, menu);

        return true;
    }

    @Override
    public void onItemSelected(Item item)
    {
        if (this.isTwoPane) { // single activity with list and detail
            // Replace frame layout with correct detail fragment
            ItemDetailFragment fragmentItem = ItemDetailFragment.newInstance(item);
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.flDetailContainer, fragmentItem);
            ft.commit();
        } else { // separate activities
            // launch detail activity using intent
            Intent i = new Intent(this, ItemDetailActivity.class);
            i.putExtra("item", item);
            startActivity(i);
        }
    }
}


public class ItemsListFragment extends Fragment
{
    private ArrayAdapter<Item> adapterItems;
    private ListView lvItems;
    private OnItemSelectedListener listener;

    public interface OnItemSelectedListener
    {
        public void onItemSelected(Item i);
    }

    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);

        if (activity instanceof OnItemSelectedListener) {
            listener = (OnItemSelectedListener) activity;
        } else {
            throw new ClassCastException(activity.toString()
                + " must implement ItemsListFragment.OnItemSelectedListener");
        }
    }

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

        // Create arraylist from item fixtures
        ArrayList<Item> items = Item.getItems(getActivity().getApplicationContext());
        adapterItems = new ArrayAdapter<Item>(getActivity(), android.R.layout.simple_list_item_activated_1, items);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Inflate view
        View view = inflater.inflate(R.layout.fragment_items_list, container, false);

        // Bind adapter to ListView
        lvItems = (ListView) view.findViewById(R.id.lvItems);
        lvItems.setAdapter(adapterItems);
        lvItems.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View item, int position, long rowId) {
                // Retrieve item based on position
                Item i = adapterItems.getItem(position);
                // Fire selected event for item
                listener.onItemSelected(i);
            }
        });

        return view;
    }

    /**
     * Turns on activate-on-click mode. When this mode is on, list items will be
     * given the 'activated' state when touched.
     */
    public void setActivateOnItemClick(boolean activateOnItemClick)
    {
        // When setting CHOICE_MODE_SINGLE, ListView will automatically
        // give items the 'activated' state when touched.
        lvItems.setChoiceMode(
            activateOnItemClick
                ? ListView.CHOICE_MODE_SINGLE
                : ListView.CHOICE_MODE_NONE
        );
    }
}




Alguém pode me ajudar?

Desde já, agradeço! :)

--
You received this message because you are subscribed to the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbrasil-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 comentários:

Postar um comentário