Android 重写 Activity 中的多个类
直接的答案是否定的,这在 Java 中是不可能的。我理解这一点,这是一个有点菜鸟的问题,但我已经用这种方式编码有一段时间了,我总是觉得这是一种肮脏的方法,所以我想澄清一下我在做什么。举例来说,我有一个扩展 Activity 类的 Activity,因此我可以使用 onCreate 等...在该 Activity 中,我有一个填充 ListView 的 SimpleCursorAdapter。
String sql = "SELECT * FROM myTable";
Cursor data = database.rawQuery(sql, null);
String fields[] = {"field1", "field2", "field3"};
adapter = new CustomCursorAdapter(this, R.layout.custom_row, data, fields, new int[] { R.id.Field1, R.id.Field2, R.id.Field2 });
list.setAdapter(adapter);
我将这个 CustomCursorAdapter 命名为 CustomCursorAdapter 是因为我正在创建一个名为 CustomCursorAdapter 的全新类,它扩展了 SimpleCursorAdapter,这样我就可以使用 bindView、newView 等方法来使用 ListView 对象上的按钮。
public class CustomCursorAdapter extends SimpleCursorAdapter {
private Context myContext;
private myActivity parentActivity;
private Button delButton;
public CustomCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
myContext = context;
parentActivity = (myActivity) myContext;
@Override
public void bindView(View view, Context context, Cursor cursor) {
int idColumn = cursor.getColumnIndex("_id");
final int getId = cursor.getInt(idColumn);
final double increment = 0.25;
UnitsConversions convert = new UnitsConversions();
int nameColumn = cursor.getColumnIndex("name");
String getName = cursor.getString(nameColumn);
TextView name = (TextView)view.findViewById(R.id.GrainName);
name.setText(getName);
delButton = (Button)view.findViewById(R.id.DeleteButton);
delButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
parentActivity.deleteItem(getId);
}
});
@Override
public View newView(Context context, Cursor cursor, final ViewGroup parent) {
View view = View.inflate(context, R.layout.list_item, null);
return view;
}
我在这里将我的代码缩小了很多,只是为了用作示例,因此,如果我删除了任何使该代码无法工作的内容,我表示歉意,但无法工作的代码并不是我问题的目的。我的问题是,除非我使用 SimpleCursorAdapter 扩展活动本身,否则这是覆盖方法的唯一方法吗?这看起来没什么大不了的,但是当我有 10 个不同的活动时,所有活动基本上都在做相同的事情,但是使用不同的 ListView 和项目,并且我必须创建 10 个不同的 CustomCursorAdapter,这看起来又脏又多余。也许有一种方法可以只创建 1 个其他活动,然后传递我需要的项目?看起来只使用 SimpleCursorAdapter 会更干净,而不必创建一个自定义适配器并重写我在 Activity 中需要的方法。如果我没有正确命名这个问题,请随时编辑。
The immediate answer is no, this is not possible in Java. I understand that, and this is a bit of a noob question, but i've been coding this way for some time now and i've always sort of felt it was a dirty method so i'd like to clarify what I am doing. Say for instance I have an Activity that is extending the Activity class so I can use onCreate etc etc...within that Activity, I have a SimpleCursorAdapter that populates a ListView.
String sql = "SELECT * FROM myTable";
Cursor data = database.rawQuery(sql, null);
String fields[] = {"field1", "field2", "field3"};
adapter = new CustomCursorAdapter(this, R.layout.custom_row, data, fields, new int[] { R.id.Field1, R.id.Field2, R.id.Field2 });
list.setAdapter(adapter);
I have named this CustomCursorAdapter because I am creating an entirely new class called CustomCursorAdapter that extends SimpleCursorAdapter so I can use methods such as bindView, newView etc to use buttons on my ListView objects.
public class CustomCursorAdapter extends SimpleCursorAdapter {
private Context myContext;
private myActivity parentActivity;
private Button delButton;
public CustomCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
myContext = context;
parentActivity = (myActivity) myContext;
@Override
public void bindView(View view, Context context, Cursor cursor) {
int idColumn = cursor.getColumnIndex("_id");
final int getId = cursor.getInt(idColumn);
final double increment = 0.25;
UnitsConversions convert = new UnitsConversions();
int nameColumn = cursor.getColumnIndex("name");
String getName = cursor.getString(nameColumn);
TextView name = (TextView)view.findViewById(R.id.GrainName);
name.setText(getName);
delButton = (Button)view.findViewById(R.id.DeleteButton);
delButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
parentActivity.deleteItem(getId);
}
});
@Override
public View newView(Context context, Cursor cursor, final ViewGroup parent) {
View view = View.inflate(context, R.layout.list_item, null);
return view;
}
I have scaled my code down a lot here just to use as an example so if I removed anything that makes this non working code I appologize, but non-working code isn't the purpose of my question. My question is, is this the only way to override methods unless I extend the activity itself with a SimpleCursorAdapter? It doesn't seem like a big deal, but when I have 10 different activities, all doing basically the same thing but with different ListViews and items and I have to create 10 different CustomCursorAdapter's it seems dirty and redundant. Maybe there is a way to create only 1 other activity and then pass in the items I need? It seems like it would be a lot cleaner to just use SimpleCursorAdapter instead of having to create a custom one and override the methods I need right in the Activity. If I didn't name this question properly, please feel free to edit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,有一个更好的方法。您可以创建 Activity 的子类(如果您愿意,可以将其声明为抽象),该子类实现您在 10 个类中共享代码的每个方法的重写版本。然后,对于您需要的每个实际活动,扩展该活动的子类,并对每个方法进行所需的更改。如果每个类中的方法不同,但与其他活动共享其大部分代码,那么您可以调用常规 super.method(),然后应用特定的更改。
Yes, there is a nicer way. You can create a subclass of Activity (and declare it abstract if you like) which implements overrided versions of every method in which you share code across your 10 classes. Then for each real Activity you need, extend that subclass of Activity and just make the changes you need to each method. If a method is different in each class but shares most of its code with the other Activities, then you can call the general super.method() and then apply the specific changes.