• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How do you swap views in an android Application

rritoch

Lurker
I am working on my first Android App. I created an android application project and have dug through the code and It is unclear how I'm going to set separate views for each list item. The original code is as follows

Code:
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);

        // Show the dummy content as text in a TextView.
        if (mItem != null) {
            ((TextView) rootView.findViewById(R.id.item_detail)).setText(mItem.content);
        }

        return rootView;
    }
On reviewing this code it is clear that the view hasn't been changed, only the content has been changed from the dummy item. I would prefer to have each dummyItem have its own view, layout, etc.

The code for establishing these views are in the dummy content

Code:
    static {
        // Add 3 sample items.
        addItem(new DummyItem("1", "Live"));
        addItem(new DummyItem("2", "Settings"));
        addItem(new DummyItem("3", "About"));
    }
What I would like to do would be more like the following where each item would have its own view and layout.

Code:
    static {
        // Add 3 sample items.
        addItem(new LiveDummyItem("1", "Live"));
        addItem(new SettingsDummyItem("2", "Settings"));
        addItem(new AboutDummyItem("3", "About"));
    }
Can someone tell me how this can be done or provide a link to a comprehensive tutorial that explains how to do this? I thought there may be some type of container I can use to swap views in and out or possibly that I may be able to grab a view from the dummy item itself, but none of the tutorials I can find show anything like this.
 
Can anyone help? Here is some more complete and updated code where I attempted to use a ViewFlipper. The problem now is that no content is being displayed when the list items are selected.

src/com/vnet/helloworld/ItemDetailFragment.java
Code:
package com.vnet.helloworld;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.vnet.helloworld.dummy.DummyContent;

/**
 * A fragment representing a single Item detail screen.
 * This fragment is either contained in a {@link ItemListActivity}
 * in two-pane mode (on tablets) or a {@link ItemDetailActivity}
 * on handsets.
 */
public class ItemDetailFragment extends Fragment {
    /**
     * The fragment argument representing the item ID that this fragment
     * represents.
     */
    public static final String ARG_ITEM_ID = "item_id";

    /**
     * The dummy content this fragment is presenting.
     */
    private Page mItem;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public ItemDetailFragment() {
    }

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

        if (getArguments().containsKey(ARG_ITEM_ID)) {
            // Load the dummy content specified by the fragment
            // arguments. In a real-world scenario, use a Loader
            // to load content from a content provider.
            mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.pageflipper, container, false);
        
        // Show the dummy content as text in a TextView.
        if (mItem != null) {
            //((TextView) rootView.findViewById(R.id.item_detail)).setText(mItem.content);
            mItem.render(rootView, inflater);
        }

        return rootView;
    }
}
src/com/vnet/helloworld/Page.java
Code:
package com.vnet.helloworld;

import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.View;
import android.view.LayoutInflater;

abstract public class Page {

    protected abstract View getView(ViewGroup container, LayoutInflater inflater);
    
    public String id;
    
    public void render(ViewGroup container,LayoutInflater inflater) 
    {
        container.removeAllViews();
        container.addView(getView(container,inflater));        
    }

}
src/com/vnet/helloworld/dummy/DummyItem.java
Code:
package com.vnet.helloworld.dummy;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.View;
import android.widget.TextView;
import com.vnet.helloworld.Page;
import com.vnet.helloworld.R;

public class DummyItem extends Page {
    
        public String id;
        public String content;

        public DummyItem(String id, String content) {
            this.id = id;
            this.content = content;
        }

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

        @Override
        public View getView(ViewGroup container, LayoutInflater inflater) {
            // TODO Auto-generated method stub
            View ret =  inflater.inflate(R.layout.dummy_layout, container, false);
            ((TextView)ret).setText(this.content);
            return ret;
        }    
}

res\layout\pageflipper.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    

</ViewFlipper>

res\layout\dummy_layout.xml
Code:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/item_detail"
    style="?android:attr/textAppearanceLarge"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:textIsSelectable="true"
    tools:context=".ItemDetailFragment" />
 
After so long without a response I re-visited this project and the problem was something extremely simple. If you look at DummyItem it has "String id;" which is also in the parent class Page. This caused a shadow so when accessing the id property in the object as a Page I was getting null instead of the value established in the constructor. Commenting out the "String id;" in the DummyItem solved this problem.

This post was made seven months ago and no one even attempted to assist. Can someone suggest a better place to get assistance with android development?
 
Back
Top Bottom