2011-05-26 157 views
0

好吧,我對Android和編程一般都很陌生。我遵循了Android開發者頁面上的教程來創建列表視圖。現在我真的想用它做點什麼。我有它的工作去一個視圖,但我想列表中的兩個項目,每個去一個不同的活動。這是我已經有的代碼。onClick ListView幫助

package com.pais.convert; 

import android.app.ListActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class list extends ListActivity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     String[] choose = getResources().getStringArray(R.array.list_chooser); 
     setListAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, choose)); 

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

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     Intent intent = new Intent(this, tempConvert.class); 
     intent.putExtra("KEY_SELECTED_INDEX", position); 
     startActivity(intent); 
    } 



    } 

我把它去一個不同的活動,但我怎麼能有兩個獨立的項目在ListView每個去不同的活動。但仍然從同一個名單啓動?

感謝所有的幫助。

回答

1

對於ListActivity,最簡單的方法是在活動本身中重寫onListItemClick

編輯:

這裏就是我的意思是:

public class test extends ListActivity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     String[] choose = getResources().getStringArray(R.array.list_chooser); 
     setListAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, choose)); 

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

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     // Start another activity to do something with the selected item. 
     // I'll assume the other activity is defined in the class 
     // AnotherActivity: 
     Intent intent = new Intent(this, AnotherActivity.class); 
     // now you can add additional information to the intent for the 
     // other activity to use. For instance, to pass just the index of the 
     // selected item, you could code: 
     intent.putExtra("KEY_SELECTED_INDEX", position); 
     // (The string "KEY_SELECTED_INDEX" is an arbitrary string you choose 
     // to name this piece of data. AnotherActivity will use the same name 
     // to retrieve it. Other extras would be added under different names.) 
     startActivity(intent); 
    } 
} 

那麼你就必須定義一個單獨的活動,顯示了第二種觀點:

public class AnotherActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     Intent intent = getIntent(); 
     int position = intent.getIntExtra("KEY_SELECTED_INDEX", -1); 
     if (position == -1) { 
      Toast.makeText(this, "No selection to show!", Toast.DURATION_LONG) 
       .show(); 
     } 

     // continue with setting up the activity 
    } 
} 

您還需要添加這是應用程序清單文件的第二個活動。

+0

所以我添加\t lv.setOnItemClickListener(新OnItemClickListener(){ \t \t公共無效onItemClick(適配器視圖父母,視圖V, \t \t INT位置,長的id){到的代碼,但我如何得到這個指向要查看的活動「視圖」並打開它 – sipjca 2011-05-26 21:03:38

+0

首先,您不需要調用'setOnItemClickListener',只需重寫'onListItemClick'。要打開另一個視圖,通常需要啓動一個新的活動(如[這裏](http://developer.android.com/guide/topics/fundamentals.html#ActivatingComponents)所述):你創建一個'new Intent(this,NewViewActivity.class);'並用它來調用'startActivity '當用戶按下該活動的後退按鈕時, p將返回到您的列表活動。 (如果你不需要,調用'startActivity'後調用'finish')。 – 2011-05-26 21:25:59

+0

我做了這樣的事情。 '@Override OnItemClickListener'的第一行。在它的下面有'new Intent(this,view.class);'和'startActivity(view.class);',下面我插入了一個Auto-Generated方法存根。 @Override和OnItemClickListener給我一個錯誤。我有一種感覺,我仍然做了一些大的錯誤......正如你可以告訴我對編程非常陌生。 – sipjca 2011-05-26 21:36:56