NaCo NaCo - 4 months ago 20
Android Question

Android Dev: Intent for ExpandableList Child via Onclick Method

I am having issues implementing an intent

onclick
for a childPosition of the
ExpandableListView
. Basically, I am creating a simple recipe app. The app has 4 parent positions ( Breakfast, Salads, Main Dishes and Snacks). Each of the parent positions has 2 items beneath it. I would like to be able to click on of the child items and load a new intent (or page).

For example, when the child banana pancakes is clicked, I want it to load the breakfast_banana_pancakes.xml file. This file will house a picture and recipe instructions.

I know this is probably not the optimal way to do this, but I am doing it for a coursera course and am not allowed to use any databases or have the app use any permissions.

Here is my code in my mainScreen xml file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/paleoportraitxhdpi"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainScreen" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textViewPaleoFact"
android:layout_alignParentLeft="true"
android:paddingRight="4dp"
android:text="@string/paleoFactText" />

<TextView
android:id="@+id/textViewPaleoFact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/textView1"
android:text="@string/paleoFactText"
android:textSize="12sp" />

<ExpandableListView
android1:id="@+id/expandableListView1"
android1:layout_width="match_parent"
android1:layout_height="250dp"
android1:layout_alignLeft="@+id/textView1"
android1:layout_alignParentTop="true"
android1:layout_marginTop="14dp"
android1:padding="10dp" >
</ExpandableListView>




Here is my code for my mainScreenJava file.

public class MainScreen extends Activity {
ExpandableListView exv;
// Random Paleo Fact Via Strings.xml
String[] myString;
final Random rgenerator = new Random();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);


exv=(ExpandableListView)findViewById(R.id.expandableListView1);
exv.setAdapter(new MyAdapter(this));

//Paleo Fact
Resources res = getResources();

myString = res.getStringArray(R.array.paleoFacts);

String q = myString[rgenerator.nextInt(myString.length)];

TextView tv2 = (TextView)findViewById(R.id.textViewPaleoFact);
tv2.setText(q);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_screen, menu);
return true;
}

}


Here is my code for myAdapter file which controls my expandable list

@SuppressWarnings("unused")
public class MyAdapter extends BaseExpandableListAdapter {
private Context context;

String []parentList={"Breakfast", "Salads", "Main Dishes", "Snacks"};
String [][]childList={
{
"Banana Pancakes", "Breakfast Casserole"
},
{
"Bacon Chickn Avacado Salad", "Curried Salmon Salad"
},
{
"Watercress Bacon Soup", "Spicy Chicken and Bacon Poppers"
},
{
"Primal Trail Mix", "Fat Guacamole Devils"
},
};


public MyAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context=context;
}


public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return null;
}


public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}


public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView tv=new TextView(context);
tv.setText(childList[groupPosition][childPosition]);
tv.setPadding(100, 15, 0, 15);

return tv;
}


public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return childList[groupPosition].length;
}


public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}


public int getGroupCount() {
// TODO Auto-generated method stub
return parentList.length;
}


public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}


public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView tv=new TextView(context);
tv.setText(parentList[groupPosition]);
tv.setPadding(60, 15, 0, 15);
return tv;
}


public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}


public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub

return true;

}
}


Here is the code I tried to implement on MyAdapter.java file

public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {

if( groupPosition == 0 && childPosition == 0){
Intent i = new Intent(v.getContext(), breakfast_banana_pancakes.class );
v.getContext().startActivity(i);
Log.e ("Banana", "Banana pancakes was clicked");
}


//Intent i = new Intent(mActivity, TargetActivity.class);
//i.putExtra(TargetActivity.EXTRA_WHATEVER, MyAdapter.getChild(groupPosition, childPosition));
//startActivity(i);
return true;
}

Answer

If I'm undertanding you correctly, you'll want to listen for onChildClick:

@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
    Intent i = new Intent(mActivity, TargetActivity.class);
    i.putExtra(TargetActivity.EXTRA_WHATEVER, mListAdapter.getChild(groupPosition, childPosition));
    startActivity(i);
    return false;
}

This will launch you into the next activity. You can send whatever you need to load the proper layout through the extras in the Intent

Also, make sure the listview knows to call this method: getExpandableListview().setOnChildClickListener(this); (if the Activity implements OnChildClickListener)

Edit for formatting

onChildClick should exist in MainScreen.

Your definition should be:

public class MainScreen extends Activity implements ExpandableListView.OnChildClickListener {...}

You then need to tell the ListView to call onChildClick:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_screen);

    exv=(ExpandableListView)findViewById(R.id.expandableListView1);
    exv.setAdapter(new MyAdapter(this));
    exv.setOnChildClickListener(this); // Add this--make sure the list knows to use this as a listener, since we implemented the interface.
Comments