访问gridView项的数据android

时间:2015-09-16 08:51:42

标签: java android gridview

所以我有一个gridview,显示数据库中的图像。当用户点击一个项目时,它必须打开显示图像的新活动和一些与图像相关的信息。

现在我想从gridview项中获取数据,例如imageId,并通过intent将其传递给新活动。

如何访问gridview的数据。

    public class MyPostsGridFragment extends Fragment {

    private ParseQueryAdapter<ParseObject> mainAdapter;
    private MyPostsGridAdapter imgAdapter;
    private GridView gridview;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.gridview_fragment, container, false);


        // Initialize main ParseQueryAdapter
        mainAdapter = new ParseQueryAdapter<ParseObject>(getActivity().getApplicationContext(), "Test");


        // Initialize the subclass of ParseQueryAdapter
        imgAdapter = new MyPostsGridAdapter(getActivity().getApplicationContext());


        // Initialize gridView and set initial view to mainAdapter
        gridview = (GridView) v.findViewById(R.id.gridview);
        gridview.setAdapter(mainAdapter);
        mainAdapter.loadObjects();


        if (gridview.getAdapter() == mainAdapter) {
            gridview.setAdapter(imgAdapter);
            imgAdapter.loadObjects();
        } else {
            gridview.setAdapter(mainAdapter);
            mainAdapter.loadObjects();
        }

        gridview.setAdapter(new MyPostsGridAdapter(getActivity()));


        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Intent intent = new Intent(getActivity(), SinglePostActivity.class);
                //intent.putExtra("")
                Log.i("position", "the position is : "+position);
                startActivity(intent);


            }
        });


        return v;
    }


}

这是我的适配器

  public class MyPostsGridAdapter extends ParseQueryAdapter<ParseObject> {
    //MediaPlayer mediaPlayer;

    private LayoutInflater mInflater = null;
    ImageButton playPause;
    private boolean isPlaying = false;


    public MyPostsGridAdapter(Context context) {


        super(context, new QueryFactory<ParseObject>() {
            public ParseQuery create() {
                ParseQuery query = new ParseQuery(Images.class);
                query.whereEqualTo("createdBy", ParseUser.getCurrentUser());
                query.include("createdBy");
                query.orderByDescending("createdAt");
                return query;
            }
        });
    }

    // Customize the layout by overriding getItemView
    @Override
    public View getItemView(final ParseObject object, View v, ViewGroup parent) {
        final ViewHolder holder;


        if (v == null) {
            v = View.inflate(getContext(), R.layout.gridview_item, null);
            holder = new ViewHolder();
            holder.btnOne = (ImageView) v.findViewById(R.id.playPause);

            v.setTag(holder);
        }

        super.getItemView(object, v, parent);

        // Add and download the image
        ParseImageView image = (ParseImageView) v.findViewById(R.id.voice);
        ParseFile imageFile = object.getParseFile("img");
        if (imageFile != null) {
            //Get singleton instance of ImageLoader
            ImageLoader imageLoader = ImageLoader.getInstance();
            //Load the image from the url into the ImageView.
            imageLoader.displayImage(imageFile.getUrl(), image);

        }//end if



        String name = object.getObjectId();
        //final int objId= Integer.parseInt(name);
        String test = object.getString("createdBy");

        // nameTextView.setText(test);

        return v;
    }//end getItem View

    class ViewHolder {
        ImageView btnOne;

    }


}

2 个答案:

答案 0 :(得分:0)

您可以从OnItemClickListener获取数据,如下所示

OnItemClickListener listener = new OnItemClickListener (){

  @Override
  onItemClick(AdapterView<?> parent, View view, int position, long id){
      String name = ((TextView) view.findViewById(R.id.txtText)).getText();
      Intent intent = new Intent(context,newActivityName.class);
      intent.putExtra("username",name);
      startActivity(intent);
  }

}

答案 1 :(得分:0)

    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        Toast.makeText(getActivity(), "Item Clicked: " + position, Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(getActivity(), DisplayImage.class);


        intent.putExtra("position", position);
        startActivity(intent);

    }

将位置或ID传递给下一个活动并显示带有图像的信息