片段

时间:2017-03-15 17:53:58

标签: android android-fragments gridview adapter oncreate

我使用GridView适配器创建了一个音板,但我遇到了两个问题。

  1. 它说
  2.   

    "无法应用CustomGridAdapter"

    myGridView.setAdapter(new CustomGridAdapter(getActivity(), items));

    1. 它说
    2.   

      "无法解决方法itemClicked(int)"

                    ` @Override
                      public void onClick(View v) {
                          context.itemClicked(position);
                      }
                  });`
      

      这是我的mFragment.java文件:

      package com.aaron.waller.girdlayoutmanagertest.mFragments;
      
      import android.content.Context;
      import android.media.MediaPlayer;
      import android.os.Bundle;
      import android.support.annotation.Nullable;
      import android.support.v4.app.Fragment;
      import android.support.v7.widget.LinearLayoutCompat;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.BaseAdapter;
      import android.widget.Button;
      import android.widget.GridView;
      
      import com.aaron.waller.girdlayoutmanagertest.MainActivity;
      import com.aaron.waller.girdlayoutmanagertest.R;
      
      public class erstesFragment extends Fragment {
      
          GridView gridView;
          MediaPlayer mp;
          public String[] items ={"Button1","Button2","Button3","Button4","Button5","Button6"};
      
          @Nullable
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
              View rootView=inflater.inflate(R.layout.erstes_fragment,container,false);
      
      
              gridView= (GridView)rootView.findViewById(R.id.gridView1);
              gridView.setAdapter(new CustomGridAdapter(getActivity(), items));
      
              return rootView;
          }
          public void itemClicked(int position) {
      
              if(position == 0){
                  mp = MediaPlayer.create(getActivity(), R.raw.sound1);
                  mp.start();
              }else if(position == 1){
                  mp = MediaPlayer.create(getActivity(), R.raw.sound2);
                  mp.start();
      
              }else if(position == 2) {
                  mp = MediaPlayer.create(getActivity(), R.raw.sound3);
                  mp.start();
              }
              else if(position == 3) {
                  mp = MediaPlayer.create(getActivity(), R.raw.sound4);
                  mp.start();
              }
              else if(position == 4) {
                  mp = MediaPlayer.create(getActivity(), R.raw.sound5);
                  mp.start();
              }
              else if(position == 5) {
                  mp = MediaPlayer.create(getActivity(), R.raw.sound6);
                  mp.start();
              }
      
          }
      
          public class CustomGridAdapter extends BaseAdapter {
      
              private MainActivity context;
              private String[] items;
              LayoutInflater inflater;
      
              public CustomGridAdapter(MainActivity context, String[] items) {
                  this.context = context;
                  this.items = items;
                  inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              }
      
      
              @Override
              public int getCount() {
                  return items.length;
              }
      
              @Override
              public Object getItem(int position) {
                  return items[position];
              }
      
              @Override
              public long getItemId(int position) {
                  return position;
              }
      
              @Override
              public View getView(final int position, View convertView, ViewGroup parent) {
      
                  if (convertView == null) {
                      convertView = inflater.inflate(R.layout.single_item, null);
                  }
                  Button button = (Button) convertView.findViewById(R.id.button);
                  button.setText(items[position]);
                  button.setOnClickListener(new View.OnClickListener() {
      
                      @Override
                      public void onClick(View v) {
                          context.itemClicked(position);
                      }
                  });
      
                  return convertView;
              }
      
          }
      }
      

      如何解决这两个问题?

2 个答案:

答案 0 :(得分:2)

1)替换它:

gridView.setAdapter(new CustomGridAdapter(getActivity(), items));

使用:

gridView.setAdapter(new CustomGridAdapter(this, items));

2)在CustomAdapter中修改如下:

    private Context context;
    private String[] items;
    LayoutInflater inflater;

    public CustomGridAdapter(Context context, String[] items) {
        this.context = context;
        this.items = items;
        inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

希望这有帮助。

答案 1 :(得分:1)

试试这个:

 gridView.setAdapter(new CustomGridAdapter(getActivity(), items));
CustomGridAdapter

中的

public class CustomGridAdapter extends BaseAdapter{

    private Context context;
    private String[] items;
    LayoutInflater inflater;

    public CustomGridAdapter(Context c, String[] items) {
        this.context = c;
        this.items = items;
        inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

现在使用点击监听器:

button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(context instanceof MainActivity) {
                    ((MainActivity) context).itemClicked(position);
                }
            }
        });