2012-04-09 63 views
0

這是我的代碼。我在這裏用一些數組數據來製作列表視圖。當我點擊列表時,我可以通過使用Toast來檢測它。但是我不能能夠打電話給另一項活動。 代替吐司事件。
公共類ListView的擴展ListActivity {在列表視圖中沒有啓動另一個活動點擊

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setListAdapter(new ArrayAdapter<String>(this, R.layout.foodjoint, RESTAURANTS)); 

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


    lv.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      // When clicked, show a toast with the TextView text 
      Toast.makeText(getApplicationContext(), ((TextView) view).getText(), 
        Toast.LENGTH_SHORT).show(); 
      myClickHandler(); 
     } 
    }); 

    //  ListView restuList=ListView(); 

} 

static final String[] RESTAURANTS = new String[] { 
    "Restaurant 1", "Restaurant 2", "Restaurant 3", "Restaurant 4", "Restaurant 5", 
    "Restaurant 6", "Restaurant 7", "Restaurant 8", "Restaurant 9", "Restaurant 10", 
    "Restaurant 11", "Restaurant 12", "Restaurant 13", "Restaurant 14", "Restaurant 15" 
}; 
public void myClickHandler() { 
    finish(); 
    Intent gotoLIst=new Intent(ListView.this,MenuActivity.class); 
    startActivity(gotoLIst); 
} 

,這裏是我的XML文件

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/AliceBlue " 
    android:padding="10dp" 
    android:textSize="16sp" 
    android:focusableInTouchMode="false" 
    android:clickable="false" 
    android:focusable="false" > 

我需要調用另一個活動@的ListView onitemClick。

其實我已經使用了另一個活動,它也使用了一些list-view.and這個活動包含了一些錯誤。這就是爲什麼它不起作用。 thnx Luksprog幫助我找到答案。

回答

1

你可能會想finish()當前Activity你開始新的一個後:

public void myClickHandler() { 
    Intent gotoLIst=new Intent(ListView.this,MenuActivity.class); 
    startActivity(gotoLIst); 
    finish(); 
} 
+0

還是你想的意思是謂如果我只是把這個methof以下烏爾編輯後話,我可以能夠調用另一方法?ok lemme嘗試 – kaushikSuman 2012-04-09 11:59:08

+0

04-09 17:00:10.168:E/AndroidRuntime(271):java.lang.IllegalStateException:ArrayAdapter需要資源ID爲TextView,這裏是例外 – kaushikSuman 2012-04-09 12:01:25

+0

@kaushik新活動MenuActivity '有一個'ListView',你可以在其上設置一個'ArrayAdapter'? – Luksprog 2012-04-09 12:07:00

相關問題