1

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?

2
  • Use onItemClickListener() see this answer Commented Oct 11, 2012 at 10:05
  • Do you have any buttons or ImageButton on the List Items? Commented Oct 11, 2012 at 10:11

5 Answers 5

1

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); } }); 
Sign up to request clarification or add additional context in comments.

1 Comment

Override has to be present, since the function redefine the one from the super class.
1

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

Comments

1

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

Comments

1

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.