Is there a way to call a function in a parent class in java?
I'm making a base class for my androids screen, it is has a list control. Is there a way for when the base class's onListItemClick gets called, saying a list item was selected. It could call a function in the parent class??
Code:
public class cHome extends cBase {
String[] MyItems={
"Gate Directions",
"Food & Beverges",
"Shjops",
"Banking",
"Official Agencies",
"Amenities & Services",
"Restrooms"
};
public void onCreate(Bundle icicle) {
super.onCreate(icicle, MyItems);
// Display=MyItems;
}
Code:
public class cBase extends ListActivity {
String[] items={"CASUAL","DRESSES","CAREER","OUTWEAR","FOOTWEAR",
"JEWELRY","ACCESSORIES"};
String[] Display;
public void onCreate(Bundle icicle, String[] items2) {
Display=items2;
super.onCreate(icicle);
setContentView(R.layout.baselayout);
setListAdapter(new IconicAdapter(this));
// selection=(TextView)findViewById(R.id.selection);
// set values in header
// set header
TextView mFlight = (TextView)findViewById(R.id.idFlyerFlightNumber);
mFlight.setText( cGlobals.mFlightNumber);
TextView mDes = (开发者_开发技巧TextView)findViewById(R.id.idFlyerDestanation);
mDes.setText( cGlobals.mDestanation);
}
class IconicAdapter extends ArrayAdapter {
Activity context;
IconicAdapter(Activity context) {
super(context, R.layout.row, Display);
this.context=context;
}
public View getView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View row=inflater.inflate(R.layout.row, null);
TextView label=(TextView)row.findViewById(R.id.label);
label.setText(Display[position]);
ImageView icon=(ImageView)row.findViewById(R.id.icon);
// String[] items={"CASUAL","DR ESSES","CAREER","OUTWEAR","FOOTWEAR",
// "JEWELRY","ACCESSORIES"};
switch(position)
{
case 0:
// icon.setImageResource(R.drawable.formobile_items);
break;
case 1:
// icon.setImageResource(R.drawable.fashion_dress);
break;
case 2:
// icon.setImageResource(R.drawable.fashion_career);
break;
case 3:
// icon.setImageResource(R.drawable.fashion_outwear);
break;
case 4:
// icon.setImageResource(R.drawable.fashion_footwear);
break;
case 5:
// icon.setImageResource(R.drawable.fashion_jewelry);
break;
case 6:
// icon.setImageResource(R.drawable.fashion_accessories);
break;
}
return(row);
}
}
}
If I understand your question, what you want is to have a way for the base class to invoke a method on a class that extends it.
One way to accomplish this is to establish a "contract" that dictate what methods an extending class must implement. You do that with either an abstract class or an interface. The base class then knows what methods WILL be available to it when a class extends it, and can make the call.
public abstract class BaseThing {
public void foo() {
bar();
}
public abstract bar();
}
then:
public ParentThing extends BaseThing {
public void bar() {
System.out.println( "Hey, look at that. It worked!" );
}
}
You can override onListItemClick
in your child class and just call super.onListItemClick
on it. Or don't override it and onListItemClick
method of base class will be called automatically when item in the list selected.
精彩评论