Question

I write an Android Application and I use a ListView and it works fine, but if I want to click on an Item then I want to start a second Activity with the selected Item. For this I want to use OnItemClick but it doesn't work :(

MainActivity.java

package de.linde.listview;

import java.util.ArrayList;
import java.util.List;

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.ListAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity<T> extends Activity {

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

        List valueList = new ArrayList<String>(); 

        for(int i=0;i<10;i++)
        {
            valueList.add("value" + i); 
        }

        ListAdapter adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,valueList); 

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

        lv.setOnItemClickListener(new OnItemClickListener() {   //<-- Error1

            @Override
            public void OnItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){  //<-- Error 2

                Intent intent = new Intent();
                intent.setClassName(getPackageName(), getPackageName() + ".Show_Activity"); 
                intent.putExtra("selected",lv.getAdapter().getItem(arg2).toString()); 
                startActivity(intent); 

            }

        });


    }

}

Here my Show_Activity.java

package de.linde.listview;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.widget.TextView;

public class Show_Activity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.anzeige);

        Intent intent = getIntent();

        ((TextView)(findViewById(R.id.textView1))).setText("Es wurde" + intent.getStringExtra("selected") + " gewählt!"); 
    }

}

I get the Error 1:

The type new AdapterView.OnItemClickListener(){} must implement the inherited abstract method AdapterView.OnItemClickListener.onItemClick(AdapterView, View, int, long)

I get the Error 2:

The method OnItemClick(AdapterView, View, int, long) of type new AdapterView.OnItemClickListener(){} must override a superclass method

What did I make wrong?

Was it helpful?

Solution

Try to remove @override before onItemClick method:

lv.setOnItemClickListener(new OnItemClickListener() {   //<-- Error1

       // @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){  //<-- Error 2

            Intent intent = new Intent();
            intent.setClassName(getPackageName(), getPackageName() + ".Show_Activity"); 
            intent.putExtra("selected",lv.getAdapter().getItem(arg2).toString()); 
            startActivity(intent); 

        }

    });

OTHER TIPS

Just use lv.setOnItemClickListener(new AdapterView.OnItemClickListener()

You should change public void OnItemClick to public void onItemClick 'o' in Lower case.

lv.setOnItemClickListener(new OnItemClickListener() { //<-- Error1

Click on this error and suggestion comes to add unimplement method click on tag unimplement method so automatic onItemClick method create put your code on this function and remove your onItemClick method and try....

Thanks

OnItemClick method name is wrong in your code.

You must have use name onItemClick (Note 'o' should be in small case) .Since OnItemClick is not method of onItemClickListener.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top