Search code examples
androidlistviewgetstring

Getting String Name from an Item in a ListView when clicked. Android


For some reason this only returns the name of the first item in the list no matter which item I click. I am not sure why it isn't returning the proper name. Basically I just want to find the name so I can search the list for that name so I can find its location in the array.

    myTask.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // When clicked, show a toast with the TextView text
            String name = ((TextView)view).getText().toString();
            for (int i = 0; i < ToDoActivity.myUser.taskCount; i++){
                if(name == ToDoActivity.myUser.tasks[i].getTaskName())
                    clickTask = i;
            }
            Intent myIntent = new Intent(view.getContext(), TaskEdit.class);
            view.getContext().startActivity(myIntent);
        }

Thank You Dlong


Solution

  • If I understand what you're trying to do, you can simply use the position argument of onItemClick:

    clickTask = position;