如何从android中的arrayList检索网格项数据

时间:2015-07-02 09:16:53

标签: android gridview arraylist android-arrayadapter

我从json获取数据并保存到自定义类类型的arrayList中。在这里,

class ShowInfo{
    public String title;
    public String image;
    public String id;
}
ArrayList<ShowInfo> imgs = new ArrayList<ShowInfo>();

我的gridView适配器类是 -

class CustomAdapter extends ArrayAdapter<ShowInfo> {

    CustomAdapter() {
        super(getActivity(), android.R.layout.simple_list_item_1, imgs);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if(convertView == null){
            LayoutInflater inflater = getActivity().getLayoutInflater();
            convertView = inflater.inflate(R.layout.single_image, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        else{
            holder = (ViewHolder)convertView.getTag();
        }
        holder.populateFrom(imgs.get(position));


        return convertView;
    }
}
class ViewHolder{
    // public TextView payment = null;
    public ImageView image = null;

    ViewHolder(View view){
        image = new RoundedImageView(getActivity().getApplicationContext());
        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(width / rowLimit, width / rowLimit);
        params2.setMargins(5, 0, 5, 0);

        //content.setLayoutParams(params);

    }

    void populateFrom(GalleryInfo info){

        // image.setImageBitmap(info.logo);
        Picasso.with(getActivity())
                .load(info.image)
                .placeholder(R.drawable.dittyicon)
                .resize(200, 200)
                .into(image);
    }
}

当点击gridview的任何项目时,我想获得相应网格图像的标题和id。我能得到那个吗? 我被困在这里,任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

使用OnItemClickListener,您可以按照以下方式执行此操作

yourGridView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            ShowInfo info=imgs.get(position);
            //By using info you can get title and id
        }
    });

希望这会对你有所帮助。

答案 1 :(得分:1)

你可以在onItemClick()上找到

    imgs.get(position).getImageid(); imgs.get(position).getImageTitle();