java - Shorter code so fill arrays with same Value -
for "memory" game, i'm using gridview display 12 cards.
@ beginning, cards same.
in examples found, see similar code add picture ids imageadapter:
private integer[] mthumbids = { r.drawable.card_back, r.drawable.card_back, r.drawable.card_back, r.drawable.card_back, r.drawable.card_back, r.drawable.card_back, r.drawable.card_back, r.drawable.card_back, r.drawable.card_back, r.drawable.card_back, r.drawable.card_back, r.drawable.card_back };
is there shorter,cleaner, nicer way? this:
private integer[] mthumbids = new integer[11]; arrays.fill(mthumbids, r.drawable.card_back);
full class:
import android.content.context; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.gridview; import android.widget.imageview; import java.util.arrays; public class imageadapter extends baseadapter { private context mcontext; public imageadapter(context c) { mcontext = c; } public int getcount() { return mthumbids.length; } public object getitem(int position) { return null; } public long getitemid(int position) { return 0; } // create new imageview each item referenced adapter public view getview(int position, view convertview, viewgroup parent) { imageview imageview; if (convertview == null) { // if it's not recycled, initialize attributes imageview = new imageview(mcontext); imageview.setlayoutparams(new gridview.layoutparams(150, 150)); imageview.setscaletype(imageview.scaletype.fit_center); imageview.setpadding(8, 8, 8, 8); } else { imageview = (imageview) convertview; } imageview.setimageresource(mthumbids[position]); return imageview; } // references our images integer[] mthumbids = new integer[11]; arrays.fill(mthumbids, r.drawable.card_back); /* r.drawable.card_back, r.drawable.card_back, r.drawable.card_back, r.drawable.card_back, r.drawable.card_back, r.drawable.card_back, r.drawable.card_back, r.drawable.card_back, r.drawable.card_back, r.drawable.card_back, r.drawable.card_back, r.drawable.card_back };*/ }
actually, that's - can use java.util.arrays#fill
:
arrays.fill(mthumbids, r.drawable.card_back);
Comments
Post a Comment