Reputation: 217
I a problem with my OnItemClickListener. It may be useful to note that I have written this following the guidelines supplied by the android developer website: http://developer.android.com/resources/tutorials/views/hello-listview.html
When setting the listener:
lv.setOnItemClickListener(new OnItemClickListener() {
I have the following error shown for the new "OnItemClickListener"
"The type new AdapterView.OnItemClickListener(){} must implement the inherited abstract method AdapterView.OnItemClickListener.onItemClick(AdapterView, View, int, long)"
Ive tried what eclipse suggests and added the apparently unimplemented method but it just generates a method that is almost identical apart from the actual specifics in the parameters e.g where I have
"int position"
the auto generated code has
"int arg2"
and by having this method in place it makes my method redundant as it wont get called (I think...)
I have attempted to alter various parts of the code such as the contents of the parameters etc but I have had no luck. So im basically looking for some help to solve this problem please, any advice/direction will be appreciated. My full code is shown below:
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class View extends ListActivity{
static final String[] entriesArray = new String[]{
"One", "Two", "Three", "GO"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.dbrowviewer, entriesArray));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//When clicked show toast
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
}
And the layout just in case.. (R.layout.dbrowviewer)
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
Also if anyone has a spare second to give advice..I have an error with "(TextView) view.getText()" in the toast which states that I cannot cast from View to TextView. Have had a quick look into it with no luck but Im currently trying to solve the first and more important problem!
Thanks in advance,
Josh
Upvotes: 2
Views: 5341
Reputation: 351
View is already a class that exists, try renaming it to something else....
Upvotes: 0
Reputation: 2281
First, this is the code I modified and it worked.
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parnet, android.view.View view,
int position, long id) {
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
The problem here is your "class" name, you've chosen "View" as your class name, which happens to be the same as android.view.View, so if you use View as the type of the second parameter of your listener, the compiler gets confused.
My suggestion is that, you'd better choose another name for your activity class. I usually use the functionality of the activity plus a "Activity" postfix as activity class name, for example, if you want to display a list of users, you can choose UserListActivity, here I choose the "ListActivity" as the postfix to indicate that the activity actually derives from ListActivity.
Upvotes: 3
Reputation: 1621
Can you try adding:
@Override
above onItemClick():
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//When clicked show toast
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
Upvotes: 0