I have an adapter and a touch handler in it, which, when it touches, displays text from the element on which the action was performed.

This is what my adapter looks like:

public class GridViewAdapter extends ArrayAdapter<TaskName>{ private final String LOG = "ADAPTER_LOG"; private AppCompatActivity context; private TextView nameView, countView; private TaskName taskName; public GridViewAdapter(Context context, ArrayList<TaskName> taskNames){ super(context, 0 , taskNames); this.context = (AppCompatActivity) context; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null){ convertView = context.getLayoutInflater().inflate(R.layout.grid_view_item, null); } 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())); convertView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { Log.d(LOG, nameView.getText().toString()); return true; } return false; } }); return convertView; } } 

The problem is that the output is always the first entry. Why is this happening and how can I fix it?

  • Those. Is the text in paragraphs different, but the same when clicked? .. - Yuriy SPb
  • @Yuriy SPb, yes that way! ( - Maybe_V

1 answer 1

The reaction is just the right. You must refer to the data on the position.

 Log.d(LOG, getItem(position).getTaskName()); 
  • Thank! I did not know how to handle the position! - Maybe_V