2017-04-01 1 views
0

Javaの知識がなくても、Androidを使ってコーディングするのは非常に新しいものです。私は他の質問を投稿して一緒にまとまってきましたが、私は苦労しています。 私はOnCreateメソッドの最上部に 'images2'という配列を作成し、それにdrawableの色を一つずつ入れました。その部分はうまくいきますが、私はonClickメソッドを持つonClickListnerを作成しようとしていましたが、OnClickメソッドを使用すると、それを別の色にクリックするとdrawableが変更されます。私はそれらすべてを同じ色に変えて欲しい。私はセレクタをプログラムで設定できることを知っています:gridview2.setSelector(new ColorDrawable(Color.BLACK));セレクタを変更すると、バックエンドでもXMlコードでも新しいドロウアブルをスティックするのに役立ちません。だから私はアダプタで別のlistArrayを作成し、私はそれをメインで呼び出すことができます。私は25個の可能なアイテムのすべての位置を通過するonClickのインデックスを持っていて、配列内の各位置に描画可能オブジェクトを変更するようにしたいと思います。また、配列myarray.add(position)にクリックされた現在の位置を追加します。しかし、私は価値が救われていないかもしれないことに気づいた。 arrayListはpublicとして宣言されていますが、アダプタ内でpublicとして宣言されているので、OnCreateの変更は効果がありませんか?ごめんなさい。それは間違いなくnoobinoob-noobの質問です。GridViewで複数の項目を選択できるようにする

/** 
 
* Created by Jordan on 3/25/2017. 
 
*/ 
 

 
public class Welcome extends AppCompatActivity implements AdapterView.OnItemClickListener { 
 

 
    public void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.welcome); 
 

 
     final List<Integer> images2 = new ArrayList<Integer>(); 
 
     images2.add(R.drawable.grid1);images2.add(R.drawable.grid6); 
 
     images2.add(R.drawable.grid2);images2.add(R.drawable.grid2); 
 
     images2.add(R.drawable.grid3);images2.add(R.drawable.grid1); 
 
     images2.add(R.drawable.grid4);images2.add(R.drawable.grid3); 
 
     images2.add(R.drawable.grid5);images2.add(R.drawable.grid2); 
 
     images2.add(R.drawable.grid6);images2.add(R.drawable.grid6); 
 
     images2.add(R.drawable.open);images2.add(R.drawable.grid4); 
 
     images2.add(R.drawable.grid1);images2.add(R.drawable.grid1); 
 
     images2.add(R.drawable.grid6);images2.add(R.drawable.grid4); 
 
     images2.add(R.drawable.grid4);images2.add(R.drawable.grid2); 
 
     images2.add(R.drawable.grid3);images2.add(R.drawable.grid3); 
 
     images2.add(R.drawable.grid6);images2.add(R.drawable.grid1); 
 
     images2.add(R.drawable.grid5); 
 

 
     this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
 
     final GridView gridview2 = (GridView) findViewById(R.id.welcomeGrid); 
 

 
     final Welcome.ImageAdapter mAdapter = new Welcome.ImageAdapter(this, images2); 
 
     gridview2.setAdapter(mAdapter); 
 
     gridview2.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE); 
 
     gridview2.setItemChecked(2, true); 
 
     gridview2.setOnItemClickListener(this); 
 

 
     gridview2.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
 
      public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
 
       // ArrayList<Integer> clickedStatus = new ArrayList<Integer>(); 
 

 
       ArrayList<Integer> clickedStatus = new ArrayList<>(25); 
 
       clickedStatus.add(position); 
 
       if (position == 12){ 
 
        setContentView(R.layout.login); 
 
       } 
 
       else if(position != 12) { 
 
        // no matter, whether the item has already been clicked or not I want this toast to pop up to promp the user to actually enter the app and sign in 
 
        Toast.makeText(Welcome.this, "You selected " + position+ ".. Please select middle tile to enter!", 
 
          Toast.LENGTH_SHORT).show(); 
 

 
        if(clickedStatus.contains(position)){ 
 
         // as soon as it's noticed that the an integer matching the current position is currently stored in the 
 
         // array list (clickedStatus), I want it removed right below 
 
         // was passing item in place of position into the remove(). Don't know why Integer item = (Integer) parent.getItemAtPosition(position); 
 

 
         //I want to go through 25 numbers and 
 
         for(int i=0; i<25;i++){ 
 
           // compare those ints to what is in contained in the clickedstatus 
 
          if(clickedStatus.contains(i)){ 
 
           // Drawable Marker = getResources().getDrawable(R.drawable.celeb1); 
 
           images2.set(i, R.drawable.celeb1); 
 
           //for every int that is stored in click status I want 
 
           // to call the item and change the background of that item to a specified drawable file. 
 

 
          } 
 
         } 
 
        } 
 
        else { 
 
         clickedStatus.add(position); 
 
        // images2.set(position,); 
 
         for(int i=1; i<25;i++){ 
 
          // compare those ints to what is in contained in the clickedstatus 
 
          if(clickedStatus.contains(i)){ 
 
           // Drawable Marker = getResources().getDrawable(R.drawable.celeb1); 
 
           images2.set(i, R.drawable.celeb1); 
 
           //for every int that is stored in click status I want 
 
           // to call the item and change the background of that item to a specified drawable file. 
 

 
          } 
 
         } 
 

 
        } 
 
        // gridview2.setSelector(new ColorDrawable(Color.BLACK)); 
 
        // mAdapter.notifyDataSetChanged(); 
 
       } 
 
      } 
 
     } 
 
     ); 
 
    } 
 
// I think this got generated automatically at some point somehow. Might of been with the multichoicemode line above^
 
    @Override 
 
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
 

 
    } 
 

 
    public class ImageAdapter extends BaseAdapter { 
 
     private Context mContext; 
 
     //IMAGES ARE REFERENCED HERE V--- 
 
     private List<Integer> mImages2; 
 
     public ArrayList<Integer> clickedStatus = new ArrayList<Integer>(); 
 
     //create array to store whether an item has been clicked or not. needs to be public!! 
 

 
     //check to determine the case in the below method 
 

 
     // IGNORE THIS 
 
     // private int selectedPosition = -1; 
 
     //public void setSelectedPosition(int position) { 
 
     // selectedPosition = position; 
 
     //} 
 

 
      public ImageAdapter(final Context c, final List<Integer> images2){ 
 
      mContext = c; 
 
      mImages2 = images2; 
 

 

 
     } 
 

 
     public int getCount() { 
 
      return mImages2.size(); 
 
     } 
 

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

 
     public long getItemId(int position) { 
 
      return 1; 
 
     } 
 
     // 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(140, 120)); 
 
       imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
 
       imageView.setPadding(0, 0, 0, 0); 
 
      } else { 
 
       imageView = (ImageView) convertView; 
 
      } 
 
      if(clickedStatus.contains(position)){ 
 
       convertView.setSelected(true); 
 
       convertView.setPressed(true); 
 
       convertView.setBackgroundColor(Color.GREEN); 
 
      } 
 

 
      imageView.setImageResource(mImages2.get(position)); 
 
      return imageView; 
 
     } 
 
     ;} 
 
}

+0

申し訳ありません。私がそこに持っているコードは絶対的な混乱であると知っていますが、私は一度にいくつかのことをやろうとするマルチタスクのようなものです。一度に開くために15以上のタブが開いていて、自分の顔の前に物事を維持するのが簡単です。しかし助けてください。これは私のMSIS学士号のための私の最終的なプロジェクトです。私のグループは基本的に頭からつま先まで、私のところまですべてを残しました!! –

+0

私は本当にできるだけ多くのことをできるだけ早く残してみたい(何もしないことを明らかにする)が、直接の強い答えのために物事を見直す必要があるなら、私はそれもすべてダウンしている。私はまだそれに取り組んでいるので、私はそれを神様の過剰なものに残しているとは思わない!私はたくさん働いていますが、今週はおそらく本当に助かるカップルの日になるでしょう。 –

答えて

0

私は勝利につながる選択のあらゆる可能な5つの組み合わせを保持するための配列の異なるセットを作成し、完成。対角線(1,2,3,4,5)、水平(1,2,3,4,5)、垂直(1,2,3,4,5)などがあります。 ListArrayの適切な位置と私はボタンがクリックされるたびにループする相対的なチェック(if文)を持っています。アプリを大幅にダウンさせることなく、紙に見えないように見えますが、チェックボックスを使用しなければ機能的です。もし誰かがコードを見ることに興味があるなら、彼らはコメントするかもしれません。

関連する問題