开发者

Android - How to compile a class which has others activities inside

开发者 https://www.devze.com 2023-03-04 09:40 出处:网络
I have an ExpandableListView which has a lot of children and for every child I would start an activity. Someone told me to create an unique class with all the activities of every children. Is it possi

I have an ExpandableListView which has a lot of children and for every child I would start an activity. Someone told me to create an unique class with all the activities of every children. Is it possible? How? In my project I have a Child.class, a Group.class, a MyExpandableListView.class, a XmlHandler.class and the main activity. I take the name for the ExpandableList from xml files that are in res/raw folder. I hope someone could help me. Thank yo开发者_StackOverflowu.


If your activities triggered by the ExpandableListView's child click event are similar (I mean only the data they display is different), it's enough for you to have a single activity, and make it's content dynamic based on the selected (clicked) child of the ExpandableListView.

Let's call your new activity Details.
If your main activity doesn't extend ExpandableListActivity, but you have inside a member of type MyExpandableListView myExpandableListView, you should set the `OnChildClickListener on that:

final ExpandableListView myExpandableListView = getExpandableListView();
myExpandableListView.setOnChildClickListener(new OnChildClickListener()
{
    @Override
    public boolean onChildClick(ExpandableListView parent, View v, 
            int groupPosition, int childPosition, long id)
    {
        final Child selectedChild = groups.get(groupPosition)
                .getChildren().get(childPosition);
        final Intent intent = new Intent(testactivity.this, Details.class);
        intent.putExtra("selectedChild", selectedChild);
        startActivity(intent);
        return true;
    }
});

If your main activity extends ExpandableListActivity, to call the Detail activity, you need to override the onChildClick event of your ExpandableListActivity:

@Override
public boolean onChildClick(ExpandableListView parent, View v, 
        int groupPosition, int childPosition, long id)
{
    final Child selectedChild = groups.get(groupPosition)
            .getChildren().get(childPosition);
    final Intent intent = new Intent(this, Details.class);
    intent.putExtra("selectedChild", selectedChild);
    startActivity(intent);
    return true;
}

You add the clicked child's value as an extra to the Details activity's intent by the putExtra method, and then just start the activity.

Inside the Details activity you can retrieve the passed Child (the clicked item on your exp.listactivity) from the activity's Intent using the getSerializableExtra method (for this to work, your Child class must implement java.io.Serializable!).
Details.java:

public class Details extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.details);

        final Intent intent = getIntent();
        if (intent.hasExtra("selectedChild"))
        {
            final Child selectedChild = (Child)intent.
                getSerializableExtra("selectedChild");
            if (selectedChild != null)
            {
                ((TextView)findViewById(R.id.nameText)).
                    setText(selectedChild.getName());
                ((ImageView)findViewById(R.id.image)).setImageResource(getResources().
                    getIdentifier(selectedChild.getImage(), "drawable", "com.test.com"));
            }
        }
    }
}

Your Details activity's layout should contain a text and an image
details.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="100dip"
    android:orientation="vertical">
    <TextView android:id="@+id/nameText" 
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:paddingLeft="10dip" android:textColor="@android:color/white" 
        android:textSize="30dip" android:gravity="center_vertical|center_horizontal" />
    <ImageView android:id="@+id/image"
        android:layout_width="fill_parent" android:layout_height="fill_parent" 
        android:scaleType="fitCenter" android:layout_below="@id/nameText" />
</RelativeLayout>

By this you can achieve, that when you click on a child item of your ExpandableListActivity, you start a new activity (Details) where the selected Child's image and text are displayed.

If you put more information inside your Child class (or url from where to fetch more data), you can get it more complicated.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号