So I made this custom 2d array adapter(basically copied it from someone else, so I don't completely know what I am doing with it), because I am making a checkers board game, and I wanted the board to be a 2d array of imageviews. I'm just wondering: How do I actually access each individual item in this 2d array in my main activity. I know in the OnClick it gives me both the view that is selected and the position, but I was just wondering if there is any way that I can do something like array[row][column].setBackgroundResource(R.drawable.piecered2) in my main activity.
public class GridViewAdapter extends BaseAdapter {
private Context context;
ImageView[][] gridContent;
int rowPosition, columnPosition, count;
public GridViewAdapter(Context c, ImageView[][] content){
context = c;
count = 0;
gridContent = new ImageView[content.length][content[0].length];
for(int i = 0; i<gridContent.length; i++){
for(int j = 0; j<gridContent[i].length; j++){
gridContent[i][j] = content[i][j];
count++;
}
}
rowPosition = 0;
columnPosition = 0;
}
public int getCount() {
return count;
}
public int getRowCount(){
return gridContent.length;
}
public int getColumnCount(){
return gridContent[0].length;
}
public Object getItem(int rowNum, int columnNum) {
return gridContent[rowNum][columnNum];
}
public View getView(int position, View view, ViewGroup group) {
ImageView imageView;
if(view == null){
imageView = new ImageView(context);
}
else{
imageView = (ImageView) view;
}
columnPosition = position % gridContent[0].length;
rowPosition = (position - columnPosition)/gridContent[0].length;
imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//Just Setting Up the Initial State of the Board Here
if(columnPosition%2 != 0 && rowPosition == 6){
setRed(imageView);
} else if(columnPosition%2 == 0 && (rowPosition ==5 || rowPosition==7)){
setRed(imageView);
}
else if(columnPosition%2 != 0 && (rowPosition == 0||rowPosition==2)){
setBlack(imageView);
} else if(columnPosition%2 == 0 && rowPosition == 1){
setBlack(imageView);
}
return imageView;
}
public Object getItem(int position){
return null;
}
public long getItemId(int position) {
return 0;
}
}
Didn't you just post the solution yourself:
ImageView[][] gridContent;
// fill the array
// ...
gridContent[row][column].setImageResource(yourresource);
will set the image-resource for the ImageView, assuming "gridContent" is a 2D array of ImageViews.
Just make sure that the array is actually filled with ImageViews that are already initialized with "= new ImageView(this)". Otherwise you will run into a NullPointerException.
Furthermore, I would suggest that you change your getItem() method to the following:
/** use ImageView as the return type instead of Object */
public ImageView getItem(int rowNum, int columnNum) {
return gridContent[rowNum][columnNum];
}
This will enable you to do the following in your Activity:
GridViewAdapter adapter = new GridViewAdapter(this); // make sure the adapter contains a filled ImageView array
adapter.getItem(row, column).setImageResource(yourresource);
Related
I want to saveInstance when changing from portrait to landscape. But when I try to restoreInstance of my letter button's background and enable, it tells me this error.
The program went well when I comment out those codes.
This is letter class
public class Letter extends BaseAdapter {
private String[] letters;
private LayoutInflater letterInf;
public Letter(Context c){
letters = new String[26];
for(int a = 0; a < letters.length; a++){
letters[a] = ""+(char)(a+'A');
}
letterInf = LayoutInflater.from(c);
}
#Override
public int getCount() {
return letters.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Button btnLetter;
if(convertView == null){
btnLetter = (Button) letterInf.inflate(R.layout.letter, null, false);
}else{
btnLetter = (Button) convertView;
}
btnLetter.setText(letters[position]);
return btnLetter;
}
}
This is what I try to restore onRestoreInstance (the whole version)
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
currPart = savedInstanceState.getInt("currPart");
numChars = savedInstanceState.getInt("numChars");
numCorr = savedInstanceState.getInt("numCorr");
int[] savedBodyPartVisibility = savedInstanceState.getIntArray("bodyPartVisibility");
for(int i = 0; i<savedBodyPartVisibility.length; i++){
bodyParts[i].setVisibility(savedBodyPartVisibility[i]);
}
//saved word
currWord = savedInstanceState.getString("currWord");
hint = savedInstanceState.getString("hint");
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){//get orientation
tvHint.setText("Hint:"+hint);// if landscape, show hint
//Toast.makeText(getBaseContext(), "This is landscape!", Toast.LENGTH_SHORT).show();
}
charViews = new TextView[currWord.length()];
wordLayout.removeAllViews();
for(int c = 0; c<currWord.length(); c++){
charViews[c] = new TextView(this);
charViews[c].setText(""+currWord.charAt(c));
charViews[c].setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
charViews[c].setGravity(Gravity.CENTER);
charViews[c].setTextColor(Color.WHITE);
charViews[c].setBackgroundResource(R.drawable.letter_bg);
wordLayout.addView(charViews[c]);
}
//saved charView
int[] savedCharViewColor = savedInstanceState.getIntArray("charViewColor");
for(int i = 0; i< savedCharViewColor.length; i++){
charViews[i].setTextColor(savedCharViewColor[i]);
}
//int numLetters = savedInstanceState.getInt("numLetters");
//letter enable//letter button background color
boolean[] savedLetterEnable = savedInstanceState.getBooleanArray("letterEnable");
int[] savedLettersColor = savedInstanceState.getIntArray("lettersColor");
for(int i = 0; i<savedLetterEnable.length; i++){
letters.getChildAt(i).setEnabled(savedLetterEnable[i]);
//letters.getChildAt(i).setBackgroundColor(savedLettersColor[i]);
}
}
You cannot restore it this way because views are recycled in RecyclerView/ListView. It means that only some of them is rendered and when you scroll it reuses already rendered views.
So in most of the cases it will not have that many child views as items in datasource.
The proper approach is to store information about items' state inside adapter.
I have created simple example to give you an idea how could it look. Note that setOnSelectedListener(new OnSelectedListener(){...} is fake code and you should write proper listener (onClick, or if you want to use Checkboxes then onCheckedCHange or anything else based on your needs).
public class Letter extends BaseAdapter {
private String[] letters;
private LayoutInflater letterInf;
private Set<String> selectedLetters = new ArraySet();
public Letter(Context c){
letters = new String[26];
for(int a = 0; a < letters.length; a++){
letters[a] = ""+(char)(a+'A');
}
letterInf = LayoutInflater.from(c);
}
Set<String> getState() {
return selectedLetters;
}
void restoreState(Set<String> selectedLetters) {
this.selectedLetters = selectedLetters;
notifyDataSetInvalidated();
}
#Override
public int getCount() {
return letters.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Button btnLetter;
if(convertView == null){
btnLetter = (Button) letterInf.inflate(R.layout.letter, null, false);
}else{
btnLetter = (Button) convertView;
}
if(selectedLetters.contains(letters[position])) {
btnLetter.setSelected(true);
} else {
btnLetter.setSelected(false);
}
btnLetter.setOnSelectedListener(new OnSelectedListener() {
void onSelected(..., boolean isSelected) {
if(isSelected) {
selectedLetters.add(letters[position]);
} else {
selectedLetters.remove(letters[position]);
}
}
});
btnLetter.setText(letters[position]);
return btnLetter;
}
}
Then whenever you save state, you get it from adapter getState and put it in savedInstanceState.
Whenever you restore state you get it from savedState and put in adapter restoreState
I have a custom adapter MyAdapter which displays items from a dynamic list(list2).. The list shows Area, location and there corresponding total items. there can be n number of similar area and location. I want to show a row "subtotal" which adds the corresponding total items of Similar Areas. Till now I'm able to show sorted items and "Total" items at end.
Now I want to display "subTotal" row after items of similar Area.
I don't know how do I compare each row till I get different Areas, in my Custom Adapter. Here's the code for my Adapter:
private class MyAdapter extends BaseAdapter {
private LayoutInflater inflater = null;
private ViewHolder holder = null;
#Override
public int getCount() {
// TODO Auto-generated method stub
if (!list2.isEmpty())
return list2.size();
else
return countItems.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
if (!list2.isEmpty())
return list2.get(position);
else
return countItems.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(final int position, View convertView,
final ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
holder = new ViewHolder();
inflater = (LayoutInflater) mActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(
R.layout.activity_search_group_row, null);
holder.text_area = (TextView) convertView
.findViewById(R.id.edtTxtRef);
holder.text_loc = (TextView) convertView
.findViewById(R.id.edtTxtName);
holder.text_total = (TextView) convertView
.findViewById(R.id.edtTxtPrice);
holder.total_lly = (LinearLayout) convertView
.findViewById(R.id.total_lly);
holder.txtSubTotal = (TextView) convertView
.findViewById(R.id.txtSubTotal);
holder.txtCount = (TextView) convertView
.findViewById(R.id.txtCount);
holder.txtCountTotal = (TextView) convertView
.findViewById(R.id.txtCountTotal);
holder.sum_lly = (LinearLayout) convertView
.findViewById(R.id.sum_lly);
holder.top_lly = (LinearLayout) convertView
.findViewById(R.id.top_lly);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
System.out.println("=====*********adapter*********======");
int total1 = 0;
if (!locList.isEmpty() && !areaList.isEmpty()) {
if (!list2.isEmpty()) {
if (list2.size() - 1 == position) {
for (int k = 0; k < countItems.size(); k++) {
total1 = total1 + countItems.get(k);
}
lastRow = true;
}
}
holder.text_area.setText(list2.get(position).getArea());
holder.text_loc.setText(list2.get(position).getLocation());
holder.text_total.setText(String.valueOf(countItems.get(position)));
} else {
searchList.setDivider(new ColorDrawable(
android.R.color.transparent));
searchList.setDividerHeight(0);
// when area and location spinners are null
if (locList.isEmpty() && areaList.isEmpty()) {
if (countItems.size() - 1 == position) {
for (int k = 0; k < countItems.size(); k++) {
total1 = total1 + countItems.get(k);
}
lastRow = true;
}
holder.top_lly.setVisibility(View.GONE);
}
}
if (lastRow) {
holder.sum_lly.setVisibility(View.VISIBLE);
holder.txtCountTotal.setText(String.valueOf(total1));
holder.txtCountTotal.setVisibility(View.VISIBLE);
lastRow = false;
}
return convertView;
}
private class ViewHolder {
TextView text_area, text_loc, text_total, txtSubTotal, txtCount,
txtCountTotal;
LinearLayout total_lly, sum_lly, top_lly;
}
}
You can have a POJO with attributes like area, location, and total. Then create a map of <String,List<YourPojo>> and populate it with area as the key and the List of POJO as value. Here you will get the values separated by area.
As an example, it will look like:
area1 -> [{area1,loc1,20},{area1,loc2,10}]
area2 -> [{area2,loc1,1},{area2,loc2,10}]
and so on.
In this way you will get different Lists for different keys.
Then iterate the keySet of the map and for every key show the value in different table rows.
Once display of one key is complete, create a row for subtotal for that particular key and then continue with the next key.
I hope this helps!
You must sort your list by a custom comparator!
http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html
Create a class and implement the Comparator-Interface. Then you can define your own sorting-rules.
public class MyComparator implements Comparator<MyListObjectType> {
#Override
public int compare(MyListObjectType o1, MyListObjectType o2) {
if (o1.getTotal() > o2.getTotal()) {
return 1;
}
if (o1.getTotal() < o2.getTotal()) {
return -1;
}
if (o1.getTotal() == o2.getTotal()) {
return 0;
}
}
}
Use the comparator with:
Collections.sort( list, new MyComparator() );
An alternative is the implementation of the comparable-interface for your objects in your list.
You must have a list which contain unique Model object based on area and location so you must override equal and hashcode method
let it is your model
class Model {
String area;
String location;
int count;
Model(String area,String location) {
this.area = area;
this.location = location;
}
public String getLocation() {
return location;
}
public String getArea() {
return area;
}
#Override
public boolean equals(Object o) {
if (o == this) {
count = 1;
return true;
} else if (o instanceof Model) {
Model t = (Model) o;
if (t.getArea().equalsIgnoreCase(getArea()) && t.getLocation().equalsIgnoreCase(getLocation())) {
count++;
return true;
}
}
return false;
}
public int getCount() {
return count;
}
#Override
public int hashCode() {
return super.hashCode();
}
}
For Example modelList hold the data
use list to show data it will hold unique data based on area and location and also have total count pass it to adapter
ArrayList<Model> model = new ArrayList(); //you have all data with same area and location
ArrayList<Model> list = new ArrayList(); //it will hold total area with same area + location
for(int i=0;i<model.size()-1;i++){
Model t = model.get(i);
for(int j=i+1;j<model.size();j++){
Model m = model.get(j);
if(!t.equals(m)){
list.add(t);
}else{
if(!list.contains(t)){
list.add(t);
}
}
}
}
Situation
I have a muliple choice mode ListView with two view types- normal and header. I load the data from the contacts app by retrieving a cursor with the names and emails and make use of the AlphabetIndexer class. My adapter extends the SimpleCursorAdapter class and implements the SectionIndexer. Moreover I override the getCount() method of my adapter so that it returns the count of the cursor + the count of the sections. In addition, my layouts are correct and items highlight in the onListItemClickListener, according to the user actions.
Problem
However, I want to highlight all the items with a Check All Button but the code fails to do that.
for (int i = 0; i <= adapter.getCount() - 1; i++) {
adapter.getItem(i);
if (adapter.getItemViewType(i) == InviteContactListAdapter.TYPE_NORMAL) {
listView.setItemChecked(i, true);
}
}
It changes correctly the layout of the items with position smaller than the cursror.getCount() and then refuses to mark the rest with a bigger index.I log the ListView.getCheckedItemPositions()
and this list includes all the items, including the ones whose layout has not been checked.
Log.d("checkedItems:", listView.getCheckedItemPositions()):
So their state is changed but not their layout.
Example
I have a list with 55 contacts and 20 section headers. When i run the Select All Button items with position 0... 55 get highlighted. Items from 56 to 75 get only get checked, not highlighted.
Code
public class InviteContactListAdapter extends SimpleCursorAdapter implements
SectionIndexer {
public static final int TYPE_HEADER = 1;
public static final int TYPE_NORMAL = 0;
public static final int TYPE_COUNT = 2;
private AlphabetIndexer indexer;
private int[] usedSectionNumbers;
private Map<Integer, Integer> sectionToPosition;
private Context context;
private HashMap<Integer, Integer> sectionToOffset;
public InviteContactListAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to, 0);
ArrayList<String> stringCollection = new ArrayList<String>();
Character firstLetter;
String firstLetterAsString;
while (c.moveToNext()) {
firstLetter = c.getString(
c.getColumnIndex(ContactsContract.Data.DISPLAY_NAME))
.charAt(0);
firstLetter = Character.toUpperCase(firstLetter);
firstLetterAsString = firstLetter.toString();
if (!stringCollection.contains(firstLetterAsString)
&& Character.isLetter(firstLetter)) {
stringCollection.add(firstLetterAsString);
}
}
Collections.sort(stringCollection);
String alphabet = " ";
for (String s : stringCollection) {
alphabet = alphabet + s;
}
c.moveToFirst();
Log.d("length", "" + alphabet.length());
this.context = context;
indexer = new AlphabetIndexer(c,
c.getColumnIndexOrThrow(ContactsContract.Data.DISPLAY_NAME),
alphabet);
sectionToPosition = new TreeMap<Integer, Integer>();
sectionToOffset = new HashMap<Integer, Integer>();
final int count = super.getCount();
int i;
for (i = count - 1; i >= 0; i--) {
sectionToPosition.put(indexer.getSectionForPosition(i), i);
}
i = 0;
usedSectionNumbers = new int[sectionToPosition.keySet().size()];
for (Integer section : sectionToPosition.keySet()) {
sectionToOffset.put(section, i);
usedSectionNumbers[i] = section;
i++;
}
for (Integer section : sectionToPosition.keySet()) {
sectionToPosition.put(section, sectionToPosition.get(section)
+ sectionToOffset.get(section));
}
Log.d("", "");
}
#Override
public int getCount() {
if (super.getCount() != 0) {
return super.getCount() + usedSectionNumbers.length;
}
return 0;
}
#Override
public Object getItem(int position) {
if (getItemViewType(position) == TYPE_NORMAL) {
return super.getItem(position
- sectionToOffset.get(getSectionForPosition(position)) - 1);
}
return null;
}
#Override
public int getPositionForSection(int section) {
if (!sectionToOffset.containsKey(section)) {
int i = 0;
int maxLength = usedSectionNumbers.length;
while (i < maxLength && section > usedSectionNumbers[i]) {
i++;
}
if (i == maxLength)
return getCount();
return indexer.getPositionForSection(usedSectionNumbers[i])
+ sectionToOffset.get(usedSectionNumbers[i]);
}
return indexer.getPositionForSection(section)
+ sectionToOffset.get(section);
}
#Override
public int getSectionForPosition(int position) {
int i = 0;
int maxLength = usedSectionNumbers.length;
while (i < maxLength
&& position >= sectionToPosition.get(usedSectionNumbers[i])) {
i++;
}
return usedSectionNumbers[i - 1];
}
#Override
public Object[] getSections() {
return indexer.getSections();
}
// nothing much to this: headers have positions that the sectionIndexer
// manages.
#Override
public int getItemViewType(int position) {
if (position == getPositionForSection(getSectionForPosition(position))) {
return TYPE_HEADER;
}
return TYPE_NORMAL;
}
#Override
public int getViewTypeCount() {
return TYPE_COUNT;
}
// return the header view, if it's in a section header position
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final int type = getItemViewType(position);
LayoutInflater inflater = LayoutInflater.from(context);
if (type == TYPE_HEADER) {
if (convertView == null) {
convertView = inflater.inflate(
R.layout.list_item_alphabet_section_header, parent,
false);
}
((TextView) convertView.findViewById(R.id.letter_header))
.setText((String) getSections()[getSectionForPosition(position)]);
return convertView;
}
return super.getView(
position - sectionToOffset.get(getSectionForPosition(position))
- 1, convertView, parent);
}
// these two methods just disable the headers
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
if (getItemViewType(position) == TYPE_HEADER) {
return false;
}
return true;
}
EDIT
I think the issue might be that you are calling the super methods all over the place, which in my opinion means that the default behavior of the SimpleCursorAdapter has (which includes highlighting a selection) only applies to the first size of the cursor items.
In other words, highlighting a selected row is not a default "feature" of an Adapter, but rather something you must implement explicitly.
While the SimpleCursorAdapter has the highlighting "built-in", it only does so for a number of items equal to the Cursor's size.
I don't see how you can change this, other than managing the views yourself manually in the getView method (i.e. do the highlighting yourself by changing the background of the views).
In list-view while scrolling through List-view heap-size is continuously increasing, it reaches to 64MB on Galaxy S3 device and application gets crashed.
public class GAdapter extends BaseAdapter implements Filterable {
private List<G> list;
private GActivity mContext;
private Drawable attach , text;
private HttpImageManager mHttpImageManager;
private LayoutInflater mInfalotar;
public GAdapter(GActivity context , List<G> list){
this.list = list;
mContext = context;
mInfalotar = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mHttpImageManager = ((GApplication)context.getApplication()).getHttpImageManager();
attach = mContext.getResources().getDrawable(R.drawable.ic_attachment);
text = mContext.getResources().getDrawable(R.drawable.ic_text);
}
public void addContact(VamooseGroup entry){
list.add(entry);
notifyDataSetChanged();
}
public void setGroupList(List<G> g){
list.clear();
datavalues = null;
list.addAll(g);
notifyDataSetChanged();
}
#Override
public int getCount(){
return (list.size()+1);
}
#Override
public Object getItem(int index) {
if(index != list.size())
return list.get(index);
else
return null;
}
#Override
public long getItemId(int index) {
return index;
}
#Override
public int getViewTypeCount() {
return list.size()+1;
}
#Override
public int getItemViewType(int position) {
return position == list.size() ? 1 : 0;
}
#Override
public View getView(int index, View convertview, ViewGroup viewGroup) {
int type = getItemViewType(index);
switch(type){
case 0 :
convertview = mInfalotar.inflate(R.layout.data_listitem , viewGroup, false);
ImageView myimageView = (ImageView)convertview.findViewById(R.id.myImage);
TextView groupname = (TextView)convertview.findViewById(R.id.groupname);
TextView timespam = (TextView)convertview.findViewById(R.id.timeInfo);
TextView msgSender = (TextView)convertview.findViewById(R.id.msgowner);
TextView draft = (TextView)convertview.findViewById(R.id.draft);
ImageView watchIcon = (ImageView)convertview.findViewById(R.id.time);
ImageView attachment = (ImageView)convertview.findViewById(R.id.attachment);
final G g = list.get(index);
String grouppic = g.pic();
if(ImageValidator.validate(grouppic)){
Bitmap bitmap = mHttpImageManager.loadImage(new HttpImageManager.LoadRequest(Uri.parse(grouppic), myimageView));
if (bitmap != null && !bitmap.isRecycled()) {
myimageView.setImageBitmap(bitmap);
}
}
parent.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.select));
groupname.setText("FOO1");
msgbox.setText("FOO2");
groupname.setText("F003");
msgSender.setText("F004");
timespam.setText("F005");
attachment.setBackgroundDrawable(attach);
watchIcon.setBackgroundDrawable(text);
break;
}
return convertview;
}
}
above code is List-adapter used. i'm using the the following library for showing the images
android-http-image-manager
can someone tell me why bitmaps are not getting recycled? heap is continuously increasing while scrolling list-view and reaches to 64MB.please provide a brief solution.
A ListView is smart and there are many ways to recycle your views to increase the performance. One of these is to implement the ViewHolder-pattern.
But this will not solve your problem. The problem you are facing is that you have overriden the getViewTypeCount() method and returning a huge number. The ListView won't recycle any view because for each row, you have a different type.
You can solve your problem by not overriding this method, or returning the number of different row types. This means.. If you have 2 different layouts available for your list. You return 2.
#Override
public int getViewTypeCount() {
return 2;
}
I'm using a gallery in my project in which I have added four images and I want it to be infinite from both right side and left side. How do I accomplish this?
The main idea is that in your getView method, you have to use
position = position % imagesArray.length;
if (position < 0)
position = position + imagesArray.length;
imagesArray is the array that holds the images in your res folder. For example:
public class CircularGallery extends Activity {
/** Called when the activity is first created. */
private Integer[] imagesArray = { R.drawable.picture1, R.drawable.picture2, R.drawable.picture3, R.drawable.picture4, R.drawable.picture5, R.drawable.picture6 , R.drawable.picture7 };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
if (position >= imagesArray.length) {
position = position % imagesArray.length;
}
Toast.makeText(CircularGallery.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return Integer.MAX_VALUE;
}
public Object getItem(int position) {
if (position >= imagesArraylength) {
position = position % mImageIds.length;
}
return position;
}
public long getItemId(int position) {
if (position >= imagesArray.length) {
position = position % imagesArray.length;
}
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
if (position >= imagesArray.length) {
position = position % imagesArray.length;
}
i.setImageResource(imagesArray[position]);
i.setLayoutParams(new Gallery.LayoutParams(80, 80));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
public int checkPosition(int position) {
if (position >= imagesArray.length) {
position = position % imagesArray.length;
}
return position;
}
}}
Also, some developers have done such a functionality and you can find sources on their blogs
http://abhinavasblog.blogspot.com/2011/09/android-infinite-looping-gallery.html
http://blog.blundellapps.com/infinite-scrolling-gallery/
if you want to set image showing on right side, just set g.setSelection(image)
My first guess is to change adapter data, i.e. if you detect that you are on the "right edge", then get your first image and add it to the end, then take second image and so on...