在gridView中交换项目

时间:2016-01-31 02:21:48

标签: java android gridview

在以下方法中,我尝试通过单击一个来交换网格视图中的两个项目。我有号码0-9并点击了0以外的一个项目,该项目会将其置于0的位置,而0将设置在0的位置点击项目。我想我错过了将gridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Item clickedItem = mList.get(position); int g = clickedItem.getValue(); String c = clickedItem.getStr(); Item spare = mList.get(8); Toast.makeText(MainActivity.this, "val is "+g+",", Toast.LENGTH_SHORT).show(); if (clickedItem.getValue() == 0) { Toast.makeText(MainActivity.this, "unable to find pic", Toast.LENGTH_SHORT).show(); } if (clickedItem.getValue() != 0) { Item temp = getItem(position); // need clicked position to get spare; spare = temp; } } public Item getItem(int position) { return mList.get(position); } }); 插入其位置的路线。请参阅以下代码:

Dim a as String = "whatever your string is"
for i as integer = 1 to len(a)
    Dim curLetter = Mid(a, i, 1) ' this will give you the current character.
    ' do your encrypting here.
Next

1 个答案:

答案 0 :(得分:0)

gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Item clickedItem = mList.get(position);
            int g = clickedItem.getValue();
            String c = clickedItem.getStr();
            Item spare = mList.get(8);


            Toast.makeText(MainActivity.this, "val is "+g+",", Toast.LENGTH_SHORT).show();

            if (clickedItem.getValue()== 0) {
                Toast.makeText(MainActivity.this, "unable to find pic", Toast.LENGTH_SHORT).show();
                return
            }

            if (clickedItem.getValue()!= 0) {
                Item temp = getItem(position);
                Item temp0 = getItem(0);
                tempPosition = position;
                mList.remove(tempPosition );
                mList.remove(0);
                mList.add(0,temp );
                if(tempPosition > mList.size() - 1){
                    mList.add(temp0);
                }else{
                    mList.add(tempPosition ,temp0);
                }

                mAdapter.notifDataSetChanged();
                // need clicked position to get spare;
                spare=temp;
            }

        }

        public Item getItem(int position) {
            return mList.get(position);
        }


    });