Java strange problem
I am bit poor at java or better to say OOP's I have a strange problem, I wonder if thats possible. I have a class
private ArrayList<?> data;
public A(Activity a, ArrayList<?> mStatus)
{
activity = a;
data = mStatus;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
later i used
public View getView(int position, View convertView, ViewGroup parent)
{
View vi = convertView;
ViewHolder holder;
if (convertView == null)
{
vi = inflater.inflate(R.layout.custom_gallery, null);
holder = new ViewHolder();
holder.text = (TextView) vi.findViewById(R.id.text);
holder.image = (ImageView) vi.findViewById(R.id.image);
holder.root = (LinearLayout) vi.findViewById(R.id.root);
vi.setTag(holder);
}
else
holder = (ViewHolder) vi.getTag();
//I used this that solves my problem
if(data.get(position) instanceof GalleryTestParser)
{
GalleryTestParser mParser = (GalleryTestParser)data.get(position);
holder.text.setText(mParser.Name);
holder.image.setTag(mParser.galleryThumbiPad);
holder.root.setTag(mParser);
imageLoader.DisplayImage(mParser.galleryThumbiPad, activity, holder.image);
}
return vi;
}
Edit: Above I want if data is of type GalleryTestParser or if some other than do som开发者_运维技巧ething else.
earlier I was trying ArrayList<GalleryTestParser> mStatus
in constructor but that is class specific. What I want is that from object of arraylist somehow I must know what type of it is so that I type cast arraylist object to that type.
Is that possible ?
Try this
public A(Activity a, List<? extends GenericParser> mStatus)
and then subclass the different types of parser from the generic base.
Careful though, if you need to use instanceof to tell them apart you are probably doing it wrong, really only the parser should need to know what type it is!
To expand on StevieB's answer (and adding a twist): You can use an interface along with a unique key to identify which concrete class you are dealing with:
public abstract interface GenericParser {
public String getType();
}
public class GalleryTestParser implements GenericParser {
public String getType() {
return "gallery";
}
}
public A(Activity a, List<? extends GenericParser> mStatus) {
GenericParser item = mStatus.get(0);
if (item.getType().equals("gallery")) {
item = (GalleryTestParser)item;
// Do stuff with item
}
}
This avoids "instanceof", giving you more control over the handling. For example, getType() could return something more complicated in future, like a list of capabilities.
Ideally though, you might want to move the execution into the GalleryTestParser, which will make the code cleaner:
public abstract interface GenericParser {
public void execute();
}
public class GalleryTestParser implements GenericParser {
public void execute() {
// Do stuff with item
}
}
public A(Activity a, List<? extends GenericParser> mStatus) {
GenericParser item = mStatus.get(0);
item.execute();
}
Do you need an instanceof
operator?
if (mStatus.get(i) instanceof GalleryTestParser){
GalleryTestParser parser = (GalleryTestParser) mStatus.get(i)
}
This practice isn't good though.
Change your signature to
public A(Activity a, List<GalleryTestParser> mStatus)
精彩评论