M
M
Maybe_V2016-03-24 21:58:15
Android
Maybe_V, 2016-03-24 21:58:15

How to fix error when using onTouch and onClick?

I ran into a problem when using the onTouch() and onClick() methods. The fact is that if these both methods are defined in the same class, then onTouch () does not work!

public class GridViewAdapter extends ArrayAdapter<TaskName> implements  View.OnTouchListener, View.OnClickListener{

    public GridViewAdapter(Context context, ArrayList<TaskName> taskNames){
        super(context, 0 , taskNames);
        this.context = (MainGridActivity) context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView = context.getLayoutInflater().inflate(R.layout.grid_view_item, null);
        }
        convertView.setTag(position);

        convertView.setOnTouchListener(this);
        convertView.setOnClickListener(this);

        taskName = getItem(position);
        nameView = (TextView) convertView.findViewById(R.id.task_name);
        nameView.setText(taskName.getTaskName());
        countView = (TextView) convertView.findViewById(R.id.count_task);
        countView.setText(String.valueOf(taskName.getTaskNameId()));


      return convertView;
    }


    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            int pos = Integer.valueOf(view.getTag().toString());
            Log.d(LOG, getItem(pos).getTaskName());
            Log.d(LOG, String.valueOf(getItem(pos).getTaskNameId()));
            selectedItemText = getItem(pos).getTaskName();
            selectedItemId = getItem(pos).getTaskNameId();
        }
        return false;
    }



    @Override
    public void onClick(View view) {
        Toast.makeText(context, "Click", Toast.LENGTH_SHORT).show();
    }
}

This is an adapter class for GridView!
In the fragment, I set the GridView context menu to remove the element ! It is for determining which element to delete that onTouch () serves. The value of the element on which the touch was made is transferred to the fragment, respectively, if the context menu is called on it for deletion - it is deleted!
I need to make a transition to another activity after touching the GridView element - from for this I use onClick().
I can't figure out where I've gone wrong and what's the problem! How can I fix this problem?
This is my first android practice and maybe I'm doing something wrong, I will accept any constructive criticism and advice !!
Thanks!!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2016-03-24
@prokopov-vi

If I understood correctly, then you need to get rid of onTouch (), this is not the way to get the element's position.
In the fragment, you need to do (probably, this already exists):
and, further, override the method:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    ...
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
    int position = info.position; // Это и есть нужная тебе позиция, сохрани её куда-нибудь и потом воспользуйся для удаления.
    ...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question