Java Android GridView将无法确定正确的位置

Java Android GridView将无法确定正确的位置,java,android,gridview,onclick,onclicklistener,Java,Android,Gridview,Onclick,Onclicklistener,我已经为gridView中的每个项目构建了一个带有onClick侦听器的gridView。我的问题是没有正确计算gridView中每个项目的位置 例如,如果我向gridView添加3个项目,那么所有3个项目的位置值都是3。如果我向gridView添加4个项目,则每个项目的位置值为4 我已经试着调试它,以找出到底是什么导致了这一问题-但我似乎无法准确指出问题的确切来源 Java代码段: public void onItemClick(AdapterView父视图、视图v、整型位置、长id){ I

我已经为gridView中的每个项目构建了一个带有onClick侦听器的gridView。我的问题是没有正确计算gridView中每个项目的位置

例如,如果我向gridView添加3个项目,那么所有3个项目的位置值都是3。如果我向gridView添加4个项目,则每个项目的位置值为4

我已经试着调试它,以找出到底是什么导致了这一问题-但我似乎无法准确指出问题的确切来源

Java代码段:
public void onItemClick(AdapterView父视图、视图v、整型位置、长id){
ImageCell i=(ImageCell)v;
跟踪(“在视图中单击:+i.mCellNumber”);
意向=无效;
开关(位置){
案例1:
Toast.makeText(DragActivity.this,“+位置,Toast.LENGTH\u SHORT)
.show();
intent=新的intent(DragActivity.this,FileChooser1.class);
打破
案例2:
Toast.makeText(DragActivity.this,“+位置,Toast.LENGTH\u SHORT)
.show();
intent=新的intent(DragActivity.this,FileChooser2.class);
打破
案例3:
Toast.makeText(DragActivity.this,“+位置,Toast.LENGTH\u SHORT)
.show();
intent=新的intent(DragActivity.this,FileChooser3.class);
打破
案例4:
Toast.makeText(DragActivity.this,“+位置,Toast.LENGTH\u SHORT)
.show();
intent=新的intent(DragActivity.this,FileChooser4.class);
打破
案例5:
Toast.makeText(DragActivity.this,“+位置,Toast.LENGTH\u SHORT)
.show();
intent=新的intent(DragActivity.this,FileChooser5.class);
打破
案例6:
Toast.makeText(DragActivity.this,“+位置,Toast.LENGTH\u SHORT)
.show();
intent=新的intent(DragActivity.this,FileChooser6.class);
打破
案例7:
Toast.makeText(DragActivity.this,“+位置,Toast.LENGTH\u SHORT)
.show();
intent=新的intent(DragActivity.this,FileChooser7.class);
打破
}
if(intent!=null){
星触觉(意向);
}
}
完整资料来源:
/**
*此活动显示一个带有网格的屏幕,在该屏幕上可以添加和编辑图像
*四处走动。它还定义了屏幕上可以拖动视图的区域
*被抛弃。在拖动对象时向用户提供反馈
*这些下降区。
* 
*
*与以前版本的DragView示例中的DragActivity类似
*应用程序,这里的代码是从Android启动器代码派生的。
* 
*
*原始启动程序代码需要长时间单击(按下)才能启动启动
*拖放序列。如果要查看该行为,请设置变量
*mlongclickstarts拖动到true。下面设置为false,这意味着
*触摸事件开始拖放。
* 
*@param
* 
*/
公共类DragActivity扩展活动实现View.OnLongClickListener,
View.OnClickListener,View.OnTouchListener{
/**
*/
//常数
私有静态final int HIDE\u TRASHCAN\u MENU\u ID=MENU.FIRST;
私有静态最终整数显示\u垃圾桶\u菜单\u ID=MENU.FIRST+1;
私有静态final int ADD_OBJECT_MENU_ID=MENU.FIRST+2;
私有静态最终整数更改\触摸\模式\菜单\ ID=MENU.FIRST+3;
私有布尔isErase=true;
私人编辑;
私有文本视图发送;
/**
*/
//变数
专用DragController mDragController;//处理拖放的对象
//序列。它与
//DragSource和DropTarget
//对象。
private DragLayer mDragLayer;//可以在其中创建对象的视图组
//拖。
private DeleteZone mDeleteZone;//用于删除的删除目标
//从屏幕上删除对象。
private int mImageCount=0;//已添加到的图像数
//屏幕。
私有ImageCell mLastNewCell=null;//添加到
//单击“添加图像”时的屏幕。
私有布尔值mLongClickStartsDrag=true;//如果为true,则需要较长时间
//单击以开始拖动
//手术。
//否则,任何触摸
//事件开始拖动。
public static final boolean debug=false;//使用此选项查看额外的
//祝酒词。
/**
*/
//方法
/**
*添加新图像,以便用户可以移动它。它显示在
*图像\源\屏幕的框架部分。
* 
*@param resourceId
*int-要添加的映像的资源id
*/
public void addNewImageToScreen(int resourceId){
if(mLastNewCell!=null)
mLastNewCell.setVisibility(View.GONE);
FrameLayout imageHolder=(FrameLayout)findViewById(R.id.image\u source\u frame);
if(imageHolder!=null){
FrameLayout.LayoutParams lp=新建FrameLayout.LayoutParams(
LayoutParams.FILL\u父级,LayoutParams.FILL\u父级,
重心);
ImageCell newView=新的ImageCell(此);
setImageResource(resourceId);
imageHolder.addView(newView,lp);
newView.mEmpty=false;
newView.mCellNumber=-1;
mLastNewCell=newView;
mImageCount++;
//让此活动收听触摸并单击视图的事件。
setOnClickListener(这个);
newView.setOnLongClickListener(这个);
setOnTouchListener(这个);
}
}
/**
*将其中一个图像添加到屏幕,以便用户可以移动新图像
*周围。请参阅addImageToScreen。
* 
*/
public void addNewImageToScreen(){
int resourceId=R.drawable.sqwhite;
添加NewTextToScreen();
int m=图像计数%3;
如果(m==1)
resourceId=R.drawable.sqdrk;
else如果(m==2)
resourceId=R.drawable.sqwhite;
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    ImageCell i = (ImageCell) v;
    trace("onItemClick in view: " + i.mCellNumber);

    Intent intent = null;
    switch (position) {

    case 1:
        Toast.makeText(DragActivity.this, "" + position, Toast.LENGTH_SHORT)
        .show();
        intent = new Intent(DragActivity.this, FileChooser1.class);
        break;
    case 2:
        Toast.makeText(DragActivity.this, "" + position, Toast.LENGTH_SHORT)
        .show();
        intent = new Intent(DragActivity.this, FileChooser2.class);
        break;
    case 3:
        Toast.makeText(DragActivity.this, "" + position, Toast.LENGTH_SHORT)
        .show();
        intent = new Intent(DragActivity.this, FileChooser3.class);
        break;
    case 4:
        Toast.makeText(DragActivity.this, "" + position, Toast.LENGTH_SHORT)
        .show();
        intent = new Intent(DragActivity.this, FileChooser4.class);
        break;
    case 5:
        Toast.makeText(DragActivity.this, "" + position, Toast.LENGTH_SHORT)
        .show();
        intent = new Intent(DragActivity.this, FileChooser5.class);
        break;
    case 6:
        Toast.makeText(DragActivity.this, "" + position, Toast.LENGTH_SHORT)
        .show();
        intent = new Intent(DragActivity.this, FileChooser6.class);
        break;
    case 7:
        Toast.makeText(DragActivity.this, "" + position, Toast.LENGTH_SHORT)
        .show();
        intent = new Intent(DragActivity.this, FileChooser7.class);
        break;

    }
    if (intent != null) {
        startActivity(intent);
    }
}
/**
 * This activity presents a screen with a grid on which images can be added and
 * moved around. It also defines areas on the screen where the dragged views can
 * be dropped. Feedback is provided to the user as the objects are dragged over
 * these drop zones.
 * 
 * <p>
 * Like the DragActivity in the previous version of the DragView example
 * application, the code here is derived from the Android Launcher code.
 * 
 * <p>
 * The original Launcher code required a long click (press) to initiate a
 * drag-drop sequence. If you want to see that behavior, set the variable
 * mLongClickStartsDrag to true. It is set to false below, which means that any
 * touch event starts a drag-drop.
 * 
 * @param <Intent>
 * 
 */

public class DragActivity extends Activity implements View.OnLongClickListener,
View.OnClickListener, View.OnTouchListener {

    /**
     */
    // Constants

    private static final int HIDE_TRASHCAN_MENU_ID = Menu.FIRST;
    private static final int SHOW_TRASHCAN_MENU_ID = Menu.FIRST + 1;
    private static final int ADD_OBJECT_MENU_ID = Menu.FIRST + 2;
    private static final int CHANGE_TOUCH_MODE_MENU_ID = Menu.FIRST + 3;
    private boolean isErase = true;
    private EditText et;
    private TextView tx;

    /**
     */
    // Variables

    private DragController mDragController; // Object that handles a drag-drop
    // sequence. It intersacts with
    // DragSource and DropTarget
    // objects.
    private DragLayer mDragLayer; // The ViewGroup within which an object can be
    // dragged.
    private DeleteZone mDeleteZone; // A drop target that is used to remove
    // objects from the screen.
    private int mImageCount = 0; // The number of images that have been added to
    // screen.
    private ImageCell mLastNewCell = null; // The last ImageCell added to the
    // screen when Add Image is clicked.
    private boolean mLongClickStartsDrag = true; // If true, it takes a long
    // click to start the drag
    // operation.
    // Otherwise, any touch
    // event starts a drag.

    public static final boolean Debugging = false; // Use this to see extra

    // toast messages.

    /**
     */
    // Methods

    /**
     * Add a new image so the user can move it around. It shows up in the
     * image_source_frame part of the screen.
     * 
     * @param resourceId
     *            int - the resource id of the image to be added
     */

    public void addNewImageToScreen(int resourceId) {
        if (mLastNewCell != null)
            mLastNewCell.setVisibility(View.GONE);

        FrameLayout imageHolder = (FrameLayout) findViewById(R.id.image_source_frame);
        if (imageHolder != null) {
            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
                    Gravity.CENTER);
            ImageCell newView = new ImageCell(this);
            newView.setImageResource(resourceId);
            imageHolder.addView(newView, lp);
            newView.mEmpty = false;
            newView.mCellNumber = -1;
            mLastNewCell = newView;
            mImageCount++;

            // Have this activity listen to touch and click events for the view.
            newView.setOnClickListener(this);
            newView.setOnLongClickListener(this);
            newView.setOnTouchListener(this);

        }
    }

    /**
     * Add one of the images to the screen so the user has a new image to move
     * around. See addImageToScreen.
     * 
     */

    public void addNewImageToScreen() {
        int resourceId = R.drawable.sqwhite;
        addNewTextToScreen();
        int m = mImageCount % 3;

        if (m == 1)
            resourceId = R.drawable.sqdrk;

        else if (m == 2)
            resourceId = R.drawable.sqwhite;
        addNewImageToScreen(resourceId);

    }

    private void addNewTextToScreen() {
        // TODO Auto-generated method stub

        if (isErase) {
            tx.setText(et.getText().toString());
        } else {
            tx.setText("");
            et.setVisibility(View.GONE);
        }
        isErase = !isErase;
    }

    /**
     * Handle a click on a view.
     * 
     */

    public void onClick(View v) {
        if (mLongClickStartsDrag) {
            positionListener();
            onItemClick(null, v, mImageCount, mImageCount);

        }
    }

    private void positionListener() {
        // TODO Auto-generated method stub

    }

    /**
     * Handle a click of the Add Image button
     * 
     */

    public void onClickAddImage(View v) {
        addNewImageToScreen();
    }

    /**
     * onCreate - called when the activity is first created.
     * 
     * Creates a drag controller and sets up three views so click and long click
     * on the views are sent to this activity. The onLongClick method starts a
     * drag sequence.
     * 
     */

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.demo);
        et = (EditText) findViewById(R.id.editText1);
        et.setVisibility(View.INVISIBLE);
        tx = (TextView) findViewById(R.id.textView1);

        GridView gridView = (GridView) findViewById(R.id.image_grid_view);

        if (gridView == null)
            toast("Unable to find GridView");
        else {
            gridView.setAdapter(new ImageCellAdapter(this));
            // gridView.setOnItemClickListener (this);
        }

        mDragController = new DragController(this);
        mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
        mDragLayer.setDragController(mDragController);
        mDragLayer.setGridView(gridView);

        mDragController.setDragListener(mDragLayer);
        // mDragController.addDropTarget (mDragLayer);

        mDeleteZone = (DeleteZone) findViewById(R.id.delete_zone_view);

        // Give the user a little guidance.
        Toast.makeText(getApplicationContext(),
                getResources().getString(R.string.instructions),
                Toast.LENGTH_LONG).show();
    }

    /**
     * Build a menu for the activity.
     * 
     */

    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(0, HIDE_TRASHCAN_MENU_ID, 0, "Hide Trashcan").setShortcut('1',
                'c');
        menu.add(0, SHOW_TRASHCAN_MENU_ID, 0, "Show Trashcan").setShortcut('2',
                'c');
        menu.add(0, ADD_OBJECT_MENU_ID, 0, "Add View").setShortcut('9', 'z');
        menu.add(0, CHANGE_TOUCH_MODE_MENU_ID, 0, "Change Touch Mode");

        return true;
    }

    /**
     * Handle a click of an item in the grid of cells.
     * 
     */

    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        ImageCell i = (ImageCell) v;
        trace("onItemClick in view: " + i.mCellNumber);

        Intent intent = null;
        switch (position) {

        case 1:
            Toast.makeText(DragActivity.this, "" + position, Toast.LENGTH_SHORT)
            .show();
            intent = new Intent(DragActivity.this, FileChooser1.class);
            break;
        case 2:
            Toast.makeText(DragActivity.this, "" + position, Toast.LENGTH_SHORT)
            .show();
            intent = new Intent(DragActivity.this, FileChooser2.class);
            break;
        case 3:
            Toast.makeText(DragActivity.this, "" + position, Toast.LENGTH_SHORT)
            .show();
            intent = new Intent(DragActivity.this, FileChooser3.class);
            break;
        case 4:
            Toast.makeText(DragActivity.this, "" + position, Toast.LENGTH_SHORT)
            .show();
            intent = new Intent(DragActivity.this, FileChooser4.class);
            break;
        case 5:
            Toast.makeText(DragActivity.this, "" + position, Toast.LENGTH_SHORT)
            .show();
            intent = new Intent(DragActivity.this, FileChooser5.class);
            break;
        case 6:
            Toast.makeText(DragActivity.this, "" + position, Toast.LENGTH_SHORT)
            .show();
            intent = new Intent(DragActivity.this, FileChooser6.class);
            break;
        case 7:
            Toast.makeText(DragActivity.this, "" + position, Toast.LENGTH_SHORT)
            .show();
            intent = new Intent(DragActivity.this, FileChooser7.class);
            break;

        }
        if (intent != null) {
            startActivity(intent);
        }
    }

    /**
     * Handle a long click. If mLongClick only is true, this will be the only
     * way to start a drag operation.
     * 
     * @param v
     *            View
     * @return boolean - true indicates that the event was handled
     */

    public boolean onLongClick(View v) {
        if (mLongClickStartsDrag) {

            // Make sure the drag was started by a long press as opposed to a
            // long click.
            // (Note: I got this from the Workspace object in the Android
            // Launcher code.
            // I think it is here to ensure that the device is still in touch
            // mode as we start the drag operation.)
            if (!v.isInTouchMode()) {
                toast("isInTouchMode returned false. Try touching the view again.");
                return false;
            }
            return startDrag(v);
        }

        // If we get here, return false to indicate that we have not taken care
        // of the event.
        return false;
    }

    /**
     * Perform an action in response to a menu item being clicked.
     * 
     */

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case HIDE_TRASHCAN_MENU_ID:
            if (mDeleteZone != null)
                mDeleteZone.setVisibility(View.INVISIBLE);
            return true;
        case SHOW_TRASHCAN_MENU_ID:
            if (mDeleteZone != null)
                mDeleteZone.setVisibility(View.VISIBLE);
            return true;
        case ADD_OBJECT_MENU_ID:
            // Add a new object to the screen;
            addNewImageToScreen();
            return true;
        case CHANGE_TOUCH_MODE_MENU_ID:
            mLongClickStartsDrag = !mLongClickStartsDrag;
            String message = mLongClickStartsDrag ? "Changed touch mode. Drag now starts on long touch (click)."
                    : "Changed touch mode. Drag now starts on touch (click).";
            Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG)
            .show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * This is the starting point for a drag operation if mLongClickStartsDrag
     * is false. It looks for the down event that gets generated when a user
     * touches the screen. Only that initiates the drag-drop sequence.
     * 
     */

    public boolean onTouch(View v, MotionEvent ev) {
        // If we are configured to start only on a long click, we are not going
        // to handle any events here.
        if (mLongClickStartsDrag)
            return false;

        boolean handledHere = false;

        final int action = ev.getAction();

        // In the situation where a long click is not needed to initiate a drag,
        // simply start on the down event.
        if (action == MotionEvent.ACTION_DOWN) {
            handledHere = startDrag(v);
        }

        return handledHere;
    }

    /**
     * Start dragging a view.
     * 
     */

    public boolean startDrag(View v) {
        DragSource dragSource = (DragSource) v;

        // We are starting a drag. Let the DragController handle it.
        mDragController.startDrag(v, dragSource, dragSource,
                DragController.DRAG_ACTION_MOVE);

        return true;
    }

    /**
     * Show a string on the screen via Toast.
     * 
     * @param msg
     *            String
     * @return void
     */

    public void toast(String msg) {
        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
    } // end toast

    /**
     * Send a message to the debug log. Also display it using Toast if Debugging
     * is true.
     */

    public void trace(String msg) {
        Log.d("DragActivity", msg);
        if (!Debugging)
            return;
        toast(msg);
    }

}
android:tag="my_tag"
view.setTag("my_tag");
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    ImageCell i = (ImageCell) v;
    trace("onItemClick in view: " + i.mCellNumber);

    Intent intent = null;
    String tag = View.getTag();
    if(tag.equals(Tag1)) {
      //do stuff for the view with Tag 1.
    } else if (tag.equals(Tag2) {
      // do stuff for the view with Tag 2
    }       
    //etc...