Question

hi I have a problem with the onItemClick in my android application. i try it but i get a error. I want that I get data from sqlite database in my listview and can click on a value in this list to start a other actitiy with this value. see my code for more information:

my actitiy:

public class ShowActivity extends ListActivity {

my method:

private void ladeDaten() {
        Cursor KlassenCursor = mDatenbank.rawQuery(KLASSEN_SELECT_ROW, null); 
        startManagingCursor(KlassenCursor); 

        android.widget.SimpleCursorAdapter KlassenAdapter = new android.widget.SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1, 
                KlassenCursor, 
                new String[] {"name"},
                new int[] {
                android.R.id.text1
                });

        setListAdapter(KlassenAdapter);

        ListView lv = (ListView)findViewById(android.R.id.list); 

        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent,View view,int position,long id){

                Cursor cursor = (Cursor)parent.getItemAtPosition(position);

                String item = cursor.getString(cursor.getColumnIndex("name"));

                Intent intent = new Intent(this,ShowActivity.class); //here is the error

                intent.putExtra("iteid", item); 

                startActivity(intent);
            }
        });

    }

my layout : ...

    </ListView>

...

my error:

The constructor Intent(new AdapterView.OnItemClickListener(){}, Class<ShowActivity>) is undefined

what can I do? :(

UPDATE:

public void onItemClick(AdapterView<?> parent,View view,int position,long id){

                Cursor cursor = (Cursor)parent.getItemAtPosition(position);

                String item = cursor.getString(cursor.getColumnIndex("name"));

                Intent intent = new Intent(ShowDayActitiy,ShowDayActivity.class); //here is the error

                intent.putExtra("iteid", item); 

                startActivity(intent);
            }
        });

i want do start a ther actitiy :(

Was it helpful?

Solution 2

Change this

Intent intent = new Intent(this,ShowActivity.class); 

to

Intent intent = new Intent(ShowActivity.this,ShowActivity.class); 

OTHER TIPS

This is happening because the in when you are creating intent the first parameter is the context . You are creating the intent from onClick() thus the context inside it is not of the outer activity. The intent should have the context of the present activity as the first parameter

Thus use ShowActivity.this or getApplicationContext() instead of using this

Intent intent = new Intent(getApplicationContext(), SecondActivity.class)

or

Intent intent = new Intent(FirstActivity.this, SecondActivity.class)

will do the trick.

Please feel free to ask any further doubts

use

Intent intent = new Intent(ShowActivity.this,ShowActivity.class);

or

Intent intent = new Intent(view.getContext(),ShowActivity.class);

instead of

Intent intent = new Intent(this,ShowActivity.class);

for starting Activity you will need to pass Activity context instead of view

there is a problem in your intent code

Intent intent = new Intent(getapplicationContext(),ShowActivity.class); 
intent.putExtra("iteid", item); 
startActivity(intent);

Check import for OnItemClickListener() in your code.You used adapter.onitemClickListener.

Remove that import and import ListView.onItemClickListener.

You can use

onItemClick(AdapterView adapterView, View view, int position, long value) {}

set Intent intent = new Intent(ShowActivity.this,ShowActivity.class); when you use "this" in nItemClickListener you didnt get the Activity context.

AdapterView parent

Use parent.getitematpostion(postion).toString().it will help you.

Please correct the code

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



        ListView list = getListView();
        list.setOnItemClickListener(new OnItemClickListener() {
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {




         if(dialog != null)  dialog.dismiss();
            Dialog_Sh_Adapter task = new Dialog_Sh_Adapter(GStore.this, data);
            setListAdapter(task); 


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