我想将像id
这样的图像资源或任何名称设置为动态创建的图像,但我无法做到这一点。这是我的代码:
public class ImageAdapter extends BaseAdapter {
public Context mContext;
public String id ;
public String [] arr;
public String[] mThumbIds;
public ImageAdapter(Context c,String id) {
mContext = c;
this.id = id;
arr = this.id.split(",");
populateArray();
}
public void populateArray(){
mThumbIds = new String[arr.length];
for(int i= 0; i<arr.length; i++){
mThumbIds[i] = "http://www.mysiteurl.com?r=index.php"+arr[i];
}
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
Picasso.with(mContext).load(mThumbIds[position]).into(imageView);
//imageView.setImageResource(Integer.parseInt(mThumbIds[position]));
return imageView;
}
}
我有一个id数组,我在这个类的构造函数中传递。我想要的是将这些ID设置为由这段代码创建的图像,以便我可以在其中一个图像点击监听器上获取该ID。
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
Picasso.with(mContext).load(mThumbIds[position]).into(imageView);
//imageView.setImageResource(Integer.parseInt(mThumbIds[position]));
return imageView;
}
这是我的片段,我从中调用ImageAdapter
类并设置图像的点击监听器。
GridView gridview = (GridView) view.findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(getActivity(),id));
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
ChartPageFragment.fromSearch = true;
ChartPageFragment newFragment = new ChartPageFragment();
Bundle args = new Bundle();
//String arr=v.getTag();
args.putString("id","34778");
newFragment.setArguments(args);
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(newFragment.getClass().getName());
// Commit the transaction
transaction.commit();
}
});
我坚持认为如何做到这一点。 提前致谢
答案 0 :(得分:1)
使用您在适配器中使用的相同逻辑。拆分字符串id
并使用position
的{{1}}中的onItemClick
参数获取相应的ID。
试试这个,
GridView
在private String getClickedItemId(int position){
// id is the variable containing all your ids
String [] arr = id.split(",");
if(position < arr.length)
return arr[poistion];
else
return null;
}
中调用此功能,如下所示。
onItemClick