2011-11-02 50 views

回答

3
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setListAdapter(new ArrayAdapter<String>(this,R.layout.new_service_request,LIST)); 

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


    lv.setOnItemClickListener(new OnItemClickListener(){ 



     @Override 
     public void onItemClick(AdapterView<?> parent, View v, 
       int position, long id) { 
      // When clicked, show a toast with the TextView text 
      Toast.makeText(getApplicationContext(), ((TextView) v).getText(), 
       Toast.LENGTH_SHORT).show(); 
     // int ITEM_CLICKED = (int)getSelectedItemId(); 
      switch(position){ 
      case 0: 
       Intent intent1 = new Intent(New.this, Next.class); 
       startActivity(intent1); 
       break; 
      case 1: 
       Intent intent2 = new Intent(New.this, List.class); 
       startActivity(intent2); 
       break; 
      case 2: 
       Intent intent3= new Intent(New.this, HotCard.class); 
       startActivity(intent3); 
       break; 
3

看看這有助於

dialog1.setItems(array_of_items, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
      //which is the item number in the list which you can use 
      //to do things accordingly 
      } 
     }); 
+0

還在活動中實現了OnItemClickListener –

1

不知道這是否會工作,但,你可以在對話框中嘗試:

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

    } 
}); 
0

我會建議有一個走在從http://www.vogella.de/articles/AndroidListView/article.html再現例子,然後嘗試適應它以適應您的目的(即在您的對話中)。我通常會發現,在試圖將代碼放入代碼之前,在一個簡單的用例中更容易掌握問題。因此,在您ListActivity,你會在例如調用

setListAdapter(new ArrayAdapter<String>(this, 
    android.R.layout.simple_list_item_1, yourThreeStringArray)); 

然後,而不是調用到Toast,只需啓動你的活動是這樣的:

Intent myIntent = new Intent(this, MyIntent.class); 
startActivityForResult(myIntent, ACTIVITY_CREATE); 

(帶班的的打算更換MyIntent行動當然。)

祝你好運!

相關問題