0
我不知道如何從圖像適配器類中獲取每個圖像的id並以連續的順序將其存儲在數組中。 我必須做的是交換圖像,並在swappin圖像後,我想保存所有圖像的ID並將它們存儲在數組中。在gridview中獲取圖像的id並按順序存儲在數組中
ImageAdapter.Java
package com.example.dragdropimagepart;
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 android.widget.Toast;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
ImageView iView;
public ImageAdapter(Context c) {
this.mContext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
System.out.println("Item Is :-" + mThumbIds[position].toString());
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
System.out.println("Geting Id of Item " + mThumbIds[position]);
if (iView != null) {
//iView.setImageResource(mThumbIds[position]);
position = iView.getId();
int[] array ={position};
Toast.makeText(mContext, "Call"+ array, Toast.LENGTH_SHORT).show();
}
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
iView = new ImageView(mContext);
iView.setLayoutParams(new GridView.LayoutParams(85, 85));
iView.setScaleType(ImageView.ScaleType.CENTER_CROP);
iView.setPadding(8, 8, 8, 8);
} else {
iView = (ImageView) convertView;
}
iView.setImageResource(mThumbIds[position]);
return iView;
}
public Integer[] mThumbIds = { R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4, R.drawable.sms1,
R.drawable.call, R.drawable.android1, R.drawable.android,
R.drawable.collection };
}
如果你可以提供一些代碼sniplet或sample,這可能有所幫助。 – JackDev
Farhan,檢查並閱讀你的答案,它需要用正確的語法用英語更新! –