Tarasov
Tarasov

Reputation: 3683

How I can start a Activity if I click in A Item in a ListView

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?

Upvotes: 1

Views: 1723

Answers (5)

Nermeen
Nermeen

Reputation: 15973

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); 

        }

    });

Upvotes: 1

Anil Jadhav
Anil Jadhav

Reputation: 2158

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.

Upvotes: 0

SBJ
SBJ

Reputation: 4139

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

Upvotes: 1

topxebec
topxebec

Reputation: 1427

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

Upvotes: 1

Deepika
Deepika

Reputation: 591

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

Upvotes: 1

Related Questions