Error with OnItemClickListen / AdapterView.OnItemClickListen

1

I can not fix the following code:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] atividades = new String[]{"Atividade 1", "Atividade 2"};

        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, atividades);

        ListView listView = (ListView) findViewById(R.id.lv);
        listView.setAdapter(adapter);

        listView.setOnClickListener(chamaAtividade());
    }

    public OnItemClickListener chamaAtividade() {
        return (new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent;
                switch (position) {
                    case 0:
                        intent = new Intent(getBaseContext(), Activity1.class);
                        startActivity(intent);
                        break;
                    case 1:
                        intent = new Intent(getBaseContext(), Activity2.class);
                        startActivity(intent);
                        break;
                }
            }
        });
    }
    public void sair(View view) {
        finish();
    }
}

The following error messages appear:

  

Error: (30, 12) error: can not find symbol class OnItemClickListener

     

Error: (31, 21) error: can not find symbol class OnItemClickListener

     

Error: Execution failed for task ': app: compileDebugJavaWithJavac'.   Compilation failed; see the compiler error output for details.

When we change the code as it appears in the suggestion, it changes where it has OnItemClickListener to AdapterView.OnItemClickListener , after the error in the line that calls the function chamaAtividade() , and obviously suggests changing the line

listView.setOnClickListener(chamaAtividade());

for

listView.setOnClickListener((View.OnClickListener) chamaAtividade());

Apparently it was supposed to work, but when I use the phone to run the code, it does not even open, it says the application has stopped. What can it be?

The 'Activity1' and 'Activity' activities were just blank activity creations for testing and the activity_main.xml file has the following code:

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Sair"
    android:onClick="sair"/>

<ListView
    android:id="@+id/lv"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">
</ListView>

    
asked by anonymous 08.08.2017 / 04:21

1 answer

1

The error is when you click Click Listener.

In onCreate change the line

listView.setOnClickListener(chamaAtividade());

By

listView.setOnItemClickListener(chamaAtividade());

In the Activity method, change the header to:

public AdapterView.OnItemClickListener chamaAtividade(){ ... }
    
08.08.2017 / 07:34