允许用户在GridView

时间:2017-04-01 19:32:02

标签: arrays xml android-arrayadapter android-adapter

通过Android编码非常新,不了解Java的观点。我已经通过发布的其他问题整理了一些内容,但我还在挣扎。 我创建了一个名为' images2'在我的OnCreate方法的顶部,并将一堆颜色从我的drawable中逐个放入其中。这部分工作得很好但我一直在尝试创建一个onClickListner,它有一个OnClick方法,当你点击它到另一种颜色时,它会改变drawable。我希望他们都变成相同的颜色。我知道我可以以编程方式设置选择器:gridview2.setSelector(new ColorDrawable(Color.BLACK));但是改变选择器,无论是在后端还是XMl代码都没有帮助我让新的drawable坚持下去。所以我在适配器中创建了另一个listArray,我可以在main中调用它。我在onClick中有一个索引,它将遍历所有25个可能的项目位置,我想使它能够将drawable更改为数组中的每个位置。它还将当前位置添加到数组myarray.add(position)中;但我刚才意识到价值可能没有得到拯救。 arrayList被声明为public,但它在适配器中声明为public,因此OnCreate中的更改没有效果吗?对不起这绝对是一个noobinoob-noob问题。



/**
 * 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;
        }
        ;}
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我创建并最终确定了一组不同的数组,用于保存每个可能导致获胜的五种选择组合。标题为对角线(1,2,3,4,5),水平(1,2,3,4,5),垂直(1,2,3,4,5)等。点击后,一个值存储在ListArray的适当位置,我有相对检查(if语句),每次单击按钮时都会循环。不会大幅减慢应用程序速度,在纸面上看起来很难看,但在没有使用复选框的情况下非常实用。如果有人有兴趣看到他们可能评论的代码。