Answer the question
In order to leave comments, you need to log in
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();
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question