2017-10-29 9 views
0

だから私は単純なボードゲームを作成する必要があります。これは、フラグメント内のGridview(10x10)に基づいています。アクティビティは、コントロール付きの別のフラグメントも表示します(左下)。 コントロールフラグメントのボタンが押されたときに、GridViewの内部でイメージを移動する必要があります。これまでのところ、私はGridViewにデータを取り込むことができましたが、イメージを内部に移動する方法を理解することはできません。GridViewの内部でImageViewを動的に置き換える方法(Android)?

このグリッドフラグメント

 public class Grid extends Fragment{ 
     GridView gridView; 
     private View rootView; 
     public PhotoImageAdapter mAdapter; 
     Integer[] image = { 
       R.drawable.pucman, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, 
       R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, 
       R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, 
       R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, 
       R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, 
       R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, 
       R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, 
       R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, 
       R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, 
       R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, R.drawable.ground, 
     }; 


     @Override 


     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 

      rootView = inflater.inflate(R.layout.fragment_grid, container, false); 
      gridView = rootView.findViewById(R.id.GridView); 
      gridView.setAdapter(new PhotoImageAdapter(this.getActivity(), image)); 
      return rootView; 


     } 


    public void goRight()// this function is called by the control fragment. 
    { 
    // here is my not so fructuous attempt to replace the image in the 
    //second cell with the image of my character 

    image[1]=R.drawable.pucman; 
    image[0]=R.drawable.ground; 

    } 

が、これは私のアダプタです。

public class PhotoImageAdapter extends BaseAdapter { 
    private Context mContext; 
    public Integer[] mThumbIds; 



    public PhotoImageAdapter(Context c, Integer[] image) { 
     mContext = c; 
     mThumbIds = image; 


    } 

    public int getCount() { 
     return mThumbIds.length; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return 0; 
    } 

    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(80, 80)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setPadding(8, 8, 8, 8); 
     } else { 
      imageView = (ImageView) convertView; 
     } 


     imageView.setImageResource(mThumbIds[position]); 
     return imageView; 
    } 

} 

すべてのヘルプは大歓迎されます!ありがとう:)

+0

パズルゲームをしたいですか? –

+0

実際には、それを越えて動くことができるキャラクターを持ったボードゲームです。 – Eric

答えて

-1

私はついにそれを考え出しました!私はgoRight()を呼び出すときにフラグメントを置き換えてトランザクションをコミットする必要がありました。それが誰かを助けることができるといいですね。

0

私は更新されたイメージアレイと画像配列を変更した後、この行を追加しようと、あなたはそれぞれの動きにアダプターを更新する必要があると思う:

gridView.setAdapter(new PhotoImageAdapter(this.getActivity(), image)); 
+0

こんにちは、試しましたか?それは助けましたか? – batsheva

+0

はい、残念ながら、私には "java.lang.NullPointerException:仮想メソッド 'void android.widget.GridView.setAdapter(android.widget.ListAdapter)'をnullオブジェクト参照に呼び出そうとしています。 – Eric

+0

あなたが同じウィンドウにいる場合、どのようにあなたのグリッドビューはnullですか?あなたは最終的にそれを宣言しなければなりません – batsheva

関連する問題