2013-01-23 19 views
0

您好我的android應用程序中的onItemClick有問題。我嘗試,但我得到一個錯誤。我希望我能從我的列表視圖中獲取來自sqlite數據庫的數據,並且可以單擊此列表中的一個值來啓動具有此值的其他actitiy。看到我的代碼以獲取更多信息:如何在android應用程序中使用onItemClick?

我actitiy:

public class ShowActivity extends ListActivity { 

我的方法:

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

    } 

我的佈局: ...

</ListView> 

...

我的錯誤:

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

我該怎麼辦? :(

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

我想你開始療法actitiy :(

+0

爲什麼哦爲什麼告訴我們你得到一個錯誤,然後不告訴我們錯誤是什麼?你認爲這些錯誤是隨機的還是不重要的? – Simon

+0

用新的Intent(ShowActitiy.this,ShowDayActivity.class)替換'new Intent(this,ShowActivity.class);'' – njzk2

+0

@ njzk2:這可能是答案。已經有7個答案,但是你全部擊敗了他們,因爲歐普試圖打開** ShowDayActivity **,但沒有提到它呢。 –

回答

1

更改此

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

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

使用

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

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

,而不是

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

啓動活動中,你將需要通過活動情境,而不是觀點

1

有在你的意圖的一個問題代碼

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

看到我的更新..... – Tarasov

1

在您的代碼中檢查OnItemClickListener()的導入。您使用的是adapter.onitemClickListener。

刪除導入和導入ListView.onItemClickListener

您可以使用

onItemClick(適配器視圖適配器視圖,查看視圖, INT位置,長值){}

1

設置Intent intent = new Intent(ShowActivity.this,ShowActivity.class);當您使用 「本」 在nItemClickListener你沒得到活動場景。

2

發生這種情況是因爲在創建意圖時第一個參數是上下文。您正在通過onClick()創建意圖,因此其內部的上下文不屬於外部活動。這樣做的目的應該有本活動作爲第一個參數

的情況下使用這樣或ShowActivity.this代替getApplicationContext()使用此

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

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

會做的伎倆。

請隨意問任何進一步的懷疑

1

適配器視圖父

使用parent.getitematpostion(現在的位置)的ToString()。它會幫助你。

+0

不,因爲它是一個遊標,而cursor.toString()不會有太大的意義。 「它會幫助你」並不意味着什麼 – njzk2

相關問題