开发者

Passing data from an Activity to its View

开发者 https://www.devze.com 2023-03-05 16:29 出处:网络
This is a newbie question. What is the best way to pass an object from an Activity to its View? I know the View has access to its context. Are Intents still the way to go? Or is there something else?

This is a newbie question. What is the best way to pass an object from an Activity to its View? I know the View has access to its context. Are Intents still the way to go? Or is there something else?

EDIT: Did not know the details about the data mattered :)

The data is user information obtained from a server in the Activity stored in an object. It contains userid, name and other details (mix of longs and strings).

The idea is to use/show the data in the view and possibl开发者_如何学Goy modify some of the fields and pass it back to the Activity which will send it to the server. Also I create and launch the View after I have the User information object with me. So after the View is launched it has to somehow get the object when it wants to use it.

P


You can use setTag: http://developer.android.com/reference/android/view/View.html#setTag%28int,%20java.lang.Object%29


I don't know what you exactly mean with passing data form an activity to a view as a view is normally part of an activity.

So I would suggest you start an AsyncTask in the onCreate() method of your activity which downloads the data from the server. When the AsyncTask finished you pass the data pack to a method of you activity which takes the data and fills the corresponding view elements. To get a reference to the views like TextView, EditText and so on you can use the method findViewById().

When the finished editing the data he must press a Button which triggers an OnClickListener that fetches the edited data from the views and starts another AsyncTask to upload the data back to the server.

0

精彩评论

暂无评论...
验证码 换一张
取 消