2013-05-20 81 views
0

我有一個問題,我的代碼iv一直試圖解決這個錯誤,但仍然沒有運氣。我想一個新的活動啓動時,在列表視圖中的項目被點擊我找了一段時間發現這個代碼listview點擊多個活動

public void onItemClick(AdapterView<?> parent, View view, 
    int position, long id) { 
    switch(position) 
    { 
    case 0: Intent newActivity = new Intent(this, superleague.class);  
      startActivity(newActivity); 
      break; 
    case 1: Intent newActivity = new Intent(this, youtube.class);  
      startActivity(newActivity); 
      break; 
    case 2: Intent newActivity = new Intent(this, olympiakos.class);  
      startActivity(newActivity); 
      break; 
    case 3: Intent newActivity = new Intent(this, karaiskaki.class);  
      startActivity(newActivity); 
      break; 
    case 4: Intent newActivity = new Intent(this, reservetickets.class);  
      startActivity(newActivity); 
      break; 
    } 
    } 

現在我把這個在我的項目,它是哪裏的問題,但因爲我新的這需要一些時間來解決小事希望你能幫助?

package com.mybasicapp; 

    import android.app.Dialog; 
    import android.app.ListActivity; 
    import android.content.DialogInterface; 
    import android.content.DialogInterface.OnCancelListener; 
    import android.content.DialogInterface.OnDismissListener; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.AdapterView; 
    import android.widget.AdapterView.OnItemClickListener; 
    import android.widget.ArrayAdapter; 
    import android.widget.Button; 
    import android.widget.ImageButton; 
    import android.widget.ListView; 
    import android.widget.Toast; 

    public class menu extends ListActivity implements onListItemClickListner{ 


    ImageButton ImagebuttonOpenDialog; 

    String KEY_TEXTPSS = "TEXTPSS"; 
    static final int CUSTOM_DIALOG_ID = 0; 

    ListView dialog_ListView; 

    String[] listContent = { 
"FORD", "BMW", "AUDI", "JAGUAR", 
"MERCEDES", "RENAULT", "ROVER", "SAAB", "BENTLEY", 
"VOLVO", "VAUXHALL", "SEAT"}; 


    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 

    lv.setOnListItemClickListener(new OnListItemClickListener() { 
    public void onListItemClick(AdapterView<?> parent, View view, 
      int position, long id) { 
     switch(position) 
     { 
      case 0: Intent newActivity = new Intent(this, 
     Pickmodel.class);  
        startActivity(newActivity); 
        break; 
      case 1: Intent newActivity = new Intent(this, 
     Pickmodel.class);  
        startActivity(newActivity); 
        break; 
      case 2: Intent newActivity = new Intent(this, 
     Pickmodel.class);  
        startActivity(newActivity); 
        break; 
      case 3: Intent newActivity = new Intent(this, 
     Pickmodel.class);  
        startActivity(newActivity); 
        break; 
      case 4: Intent newActivity = new Intent(this, 
     Pickmodel.class);  
        startActivity(newActivity); 
        break; 
     } 
     } 




    ImagebuttonOpenDialog = (ImageButton)findViewById(R.id.imgbtn1); 
    ImagebuttonOpenDialog.setOnClickListener(new Button.OnClickListener(){ 

    public void onClick(View arg0) { 
     showDialog(CUSTOM_DIALOG_ID); 
     }}); 

     } 

    @Override 
    protected Dialog onCreateDialog(int id) { 

     Dialog dialog = null; 

     switch(id) { 
     case CUSTOM_DIALOG_ID: 
      dialog = new Dialog(menu.this); 
      dialog.setContentView(R.layout.custom); 
      dialog.setTitle("Custom Dialog"); 

      dialog.setCancelable(true); 
      dialog.setCanceledOnTouchOutside(true); 

      dialog.setOnCancelListener(new OnCancelListener(){ 

     public void onCancel(DialogInterface dialog) { 
     // TODO Auto-generated method stub 
     Toast.makeText(menu.this, 
      "OnCancelListener", 
      Toast.LENGTH_LONG).show(); 
     }}); 

      dialog.setOnDismissListener(new OnDismissListener(){ 

     public void onDismiss(DialogInterface dialog) { 
     // TODO Auto-generated method stub 
     Toast.makeText(menu.this, 
      "OnDismissListener", 
      Toast.LENGTH_LONG).show(); 
     }}); 

      //Prepare ListView in dialog 
      dialog_ListView = (ListView)dialog.findViewById(R.id.dialoglist); 
      ArrayAdapter<String> adapter 
      = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, listContent); 
      dialog_ListView.setAdapter(adapter); 
      dialog_ListView.setOnItemClickListener(new OnItemClickListener(){ 

     public void onItemClick(AdapterView<?> parent, View view, 
      int position, long id) { 
     // TODO Auto-generated method stub 
     Toast.makeText(menu.this, 
      parent.getItemAtPosition(position).toString() + " clicked", 
      Toast.LENGTH_LONG).show(); 
     dismissDialog(CUSTOM_DIALOG_ID); 
     }}); 

      break; 
     } 

     return dialog; 
    } 

    @Override 
    protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) { 
     // TODO Auto-generated method stub 
     super.onPrepareDialog(id, dialog, bundle); 

     switch(id) { 
     case CUSTOM_DIALOG_ID: 
      // 
      break; 
     } 

    } 

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
      long arg3) { 
     // TODO Auto-generated method stub 

    } 

    } 


    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
    // TODO Auto-generated method stub 

     } 

請讓我知道如果我失去了一些東西也感謝 P.S我不能運行這個項目,所以我不能打印日誌CAT

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/customdialog" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="20dp" 
    android:minWidth="300dp"> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/popcar" /> 

<ListView 
    android:id="@+id/dialoglist" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 

<ImageButton 
    android:id="@+id/imageButton1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/kj" /> 

</LinearLayout> 
+0

什麼問題?即使項目沒有運行,Logcat也會說出一些東西。你爲什麼不能運行它? –

+0

我如何複製logcat? – coolcat

+0

你有什麼錯誤?什麼是日食給你的信息。 – Gustek

回答

0

既然你正在擴展ListActivity時,onItemClickListener將無法正常工作。 你必須使用onListItemClickListner然後它將工作。

+0

我上面編輯了我的代碼..它不讓我導入onlistitemclicklistener希望你能幫忙嗎? – coolcat

+1

只需要注意,擴展ListActivity時無需爲點擊事件設置偵聽器。 只需重寫此方法在您的類 保護無效onListItemClick() 欲瞭解更多信息請參閱: http://developer.android.com/reference/android/app/ListActivity.html –

0

您正在使用ListActivity,但您的視圖中沒有任何列表。

你應該有一個元素

<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 
</ListView> 

在你的佈局能夠使用ListActivity。

但它看起來像你已經削減了一些XML,因爲我沒有看到關閉RelativeLayout標記

+0

有錯誤的XML ..我有改變了上面的XML。我以爲我確實有這個問題。這個問題頂部的代碼是問題是? – coolcat

+0

當使用ListActivity ListView必須有一個id @android:id/list – Gustek

+0

我有那個android:id =「@ + id/dialoglist」 – coolcat