SORU
27 Mart 2012, Salı


Android bağlam alma etkinliği

Bu beni afallattı.

Özel düzen bir sınıf içinde bir etkinlik yöntemi aramam gerek. Bu sorun, düzen içinde faaliyet erişmek için nasıl bilmiyorum.

ProfileView

public class ProfileView extends LinearLayout
{
    TextView profileTitleTextView;
    ImageView profileScreenImageButton;
    boolean isEmpty;
    ProfileData data;
    String name;

    public ProfileView(Context context, AttributeSet attrs, String name, final ProfileData profileData)
    {
        super(context, attrs);
        ......
        ......
    }

    //Heres where things get complicated
    public void onClick(View v)
    {
        //Need to get the parent activity and call its method.
        ProfileActivity x = (ProfileActivity) context;
        x.activityMethod();
    }
}

ProfileActivity

public class ProfileActivityActivity extends Activity
{
    //In here I am creating multiple ProfileViews and adding them to the activity dynamically.

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile_activity_main);
    }

    public void addProfilesToThisView()
    {
        ProfileData tempPd = new tempPd(.....)
        Context actvitiyContext = this.getApplicationContext();
        //Profile view needs context, null, name and a profileData
        ProfileView pv = new ProfileView(actvitiyContext, null, temp, tempPd);
        profileLayout.addView(pv);
    }
}

Yukarıda gördüğünüz gibi, profileView programlama yoluyla başlatmasını ve activityContext içinde taşıyorum. 2 soru:

  1. Bu Profileview içine doğru bağlam geçen mıyım?
  2. Nasıl içerik içeren etkinlik alabilirim?

CEVAP
27 Mart 2012, Salı


Sadece düzen için Context gibi bu da başarılı:

ProfileView pv = new ProfileView(this, null, temp, tempPd);

Daha sonra Düzeni bağlamında olacak, ama aslında etkinlik orada olduğunu bilecek ve böylece ne kullanılabilir gerek: çevirebilirsiniz

Activity activity = (Activity) context;

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • isupportthegosselins

    isupporttheg

    6 Aralık 2009
  • Maschine Tutorials

    Maschine Tut

    15 ŞUBAT 2011
  • Vsauce

    Vsauce

    30 Temmuz 2007