Android的 - 调用方法,而不是类 - SDK [英] Android - call method instead of class - sdk

查看:96
本文介绍了Android的 - 调用方法,而不是类 - SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的主应用程序类,如下所示,什么我想知道的是如何改变一行无论是从同一个类或者其他类调用方法,而其他人仍然可以调用活动。这里是code:

I have my main application class as follows and what I would like to know is how to change one line to call a method either from same class or another class, whilst the others still call activities. Here is the code:

public class InfoActivity extends GDListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle(R.string.info_activity_title);

    ItemAdapter adapter = new ItemAdapter(this);
    adapter.add(createTextItem(R.string.info_about, AboutActivity.class));
    adapter.add(createTextItem(R.string.info_terms, TermsActivity.class));
    adapter.add(createTextItem(R.string.info_privacy, PrivacyActivity.class));

    setListAdapter(adapter);
}

private TextItem createTextItem(int stringId, Class<?> klass) {
    final TextItem textItem = new TextItem(getString(stringId));
    textItem.setTag(klass);
    return textItem;
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    final TextItem textItem = (TextItem) l.getAdapter().getItem(position);
    Intent intent = new Intent(InfoActivity.this, (Class<?>) textItem.getTag());
    startActivity(intent);
}
}

在这行:

adapter.add(createTextItem(R.string.info_about, AboutActivity.class));

我想调用哪个作为一个例子做了​​这样的方法:

I would like to call a method which as an example does this:

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { AboutActivity.this.getString(R.string.feedback_email) } );
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, AboutActivity.this.getString(R.string.feedback_subject)); 
    emailIntent.setType("plain/text"); 
    startActivity(emailIntent);

现在,我有这AboutActivity的onCreate方法,但自然没有理由还有一个活动中这个功能(发送电子邮件)。相反,它可以只跑出原样。所以,我怎么能做到这一点?

Right now I have this in the onCreate method of AboutActivity, but there naturally is no reason to have this functionality (sending email) within an activity. Instead it can just be ran as is. So, how could I do this?

谢谢!

另外两行:

 adapter.add(createTextItem(R.string.info_terms, TermsActivity.class));
    adapter.add(createTextItem(R.string.info_privacy, PrivacyActivity.class));

,他们可以在功能方面保持相同。这个问题是从这个(我刚才问,得到了回答)增编:

they can remain the same in terms of functionality. This question is an addendum from this one (which I asked earlier and got answered):

Android的 - 调用方法,而不是类 - SDK

推荐答案

如何过你选择去了解这一点,关键是要真正移动决策约当一个特定的项目被点击向下进入<$怎么办C $ C> onListItemClick 方法。这里有一个简单的方法。首先,改变你的 createTextItem 方法如下:

How ever you choose to go about this, the trick is to actually move the decision making about what to do when a particular item is clicked down into the onListItemClick method. Here's a simple approach. First, change your createTextItem method to this:

private TextItem createTextItem(int stringId) {
    final TextItem textItem = new TextItem(getString(stringId));
    return textItem;
}

然后,改变你的 onListItemClick 来的:

protected void onListItemClick(ListView l, View v, int position, long id) {
    final TextItem textItem = (TextItem) l.getAdapter().getItem(position);
    String textItemContents = textItem.getString(); //I don't know if this is actually correct. I don't know what the TextItem class is. But I think you get the idea.
    Intent intent = getIntentForString(textItemContents);
    startActivity(Intent);
}

然后

getIntentForString()方法看起来像这样(注意,我们不能用一个开关,因为使用switch语句处理字符串的支持才刚刚被添加到Java):

Your getIntentForString() method would then look something like this (note we can't use a switch because support for using switch statements with strings has only just recently been added to java):

private Intent getIntentForString(String textViewContents){
  if(textViewContents.equals(getString(R.string.info_about))){
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { 
      AboutActivity.this.getString(R.string.feedback_email) } );
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 
      AboutActivity.this.getString(R.string.feedback_subject)); 
    emailIntent.setType("plain/text"); 
    return emailIntent;
  }
  else if(textViewContents.equals(getString(R.string.info_terms)){
    return new Intent(InfoActivity.this, TermsActivity.class);
  }
  else if(textViewContents.equals(getString(R.string.info_privacy)){
    return new Intent(InfoActivity.this, Privacy.class);
  }
  else{
    return null;
  }
}

请注意这种方法有垮台虽然。如果你要跟开始加入一些不同的项目到你的ListView的,你会需要成长和壮大你的 getIntentForString()方法。对于现在虽然,这应该足够了。如果你发现自己增加更多的选择,你的ListView,虽然他们会说我们需要采取更复杂的方法。

Note this approach has a downfall though. If yous start adding a bunch of different items to your ListView you're going to need to grow and grow your getIntentForString() method. For right now though, this should suffice. If you find yourself adding more options to your ListView though they'll be a more complicated approach that we'll need to take.

这篇关于Android的 - 调用方法,而不是类 - SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆