I put some value to ArrayList<"Object">
ArrayList<Object> mData = new ArrayList<Object>();
AdListData data = new AdListData();
data.Id = json_data.getInt("ad_uid");
data.User_id = json_data.getInt("user_id");
mData.add(data);
And after I need to convert ArrayList<"Object"> to ArrayList<"AdListData"> How can I do this? Example:
ArrayList<AdListData> array = new ArrayList<AdListData>();
array = mData;
Why I need to do this? I use AsyncTask and on onPostExecute a get data like this
protected void onPostExecute(ArrayList<Object> result) {
// Pass the result data back to the main activity
mDownloadCompleteListener.getDownloadCompleteState(result);
}
this example show that a get only on type of arraylist
ArrayList<Object>
OR
ArrayList<AdListData>
But in my case I want to use it for different Objects like this
ArrayList<Object> mData = new ArrayList<Object>();
if(mType == "get_ad_data")
{
AdListData data = new AdListData();
data.Id = json_data.getInt("ad_uid");
data.User_id = json_data.getInt("user_id");
mData.add(data);
}
else
{
AnotherClass data = new AnotherClass();
data.Id = json_data.getInt("ad_uid");
data.User_id = json_data.getInt("user_id");
mData.add(data);
}
return mData;
` And when listener call I want to convert it in needed Array of objects
@Override
public void getDownloadCompleteState(ArrayList<Object> ad_list) {
// TODO Auto-generated method stub
ArrayList<AdListData> array = new ArrayList<AdListData>();
array = ad_list;
}
Check out java generics. When you pass in your array list, define it as ArrayList<T extends Object>
or ArrayList<? extends Object>
ArrayList<? extends Object>
. Then when you're passing or getting values you can define T as the class of your choice or check if the object is an instance of the class you want. There's a ton of information and examples on java generics so you can find what fits your needs w/o me posting a bunch of examples.
I don't think it's possible to just automatically assign ArrayList<Object>
to ArrayList<AdListData>
since there is no guarantee that all of the data is of type AdListData
.
Why you are making life too complicated..
Ever heard about Moduler Code
, Interfaces
, Abstract class
es???
I ll suggest you to write one Interface
or Abstract class
over your AdListData
and AnotherClass
.. (I am assuming they share IS A
relationship..)
If not.. write two diff AsyncTask
s for those.. and if they share common code you can use functions like utility functions..
doing this your code will be much more readable and simple..
now, to ans your question I ll just write some snippet..
public class Test {
int data;
public static <T extends Object> void foo(ArrayList<T> l) {
T firstObj = l.get(0);
if (firstObj != null) {
if (firstObj instanceof Test) {
System.out.println("yay.. I am list of test obj " + l);
} else {
System.out.println("m just another list.. :( " + l);
}
}
@Override
public String toString() {
return data + "";
}
public static void main(String[] args) {
ArrayList<Test> l = new ArrayList<Test>();
Test t1 = new Test();
t1.data = 1;
Test t2 = new Test();
t2.data = 2;
Test t3 = new Test();
t3.data = 3;
Test t4 = new Test();
t4.data = 4;
l.add(t1);
l.add(t2);
l.add(t3);
l.add(t4);
foo(l);
ArrayList<Integer> l2 = new ArrayList<Integer>();
l2.add(1);
l2.add(2);
l2.add(3);
l2.add(4);
foo(l2);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.