2012-12-19 31 views
0

我有這個事件處理時收集對象的信息:如何使用onListItemClick啓動一個新的活動

@Override 
     protected void onListItemClick(ListView l, View v, int position, long id) { 
     // TODO Auto-generated method stub 
     //super.onListItemClick(l, v, position, id); 
     String selection = l.getItemAtPosition(position).toString(); 

     Intent myIntent = new Intent(MainActivity.this, sondaggioActivity.class); 
     MainActivity.this.startActivity(myIntent); 
     //Toast.makeText(this, selection, Toast.LENGTH_LONG).show(); 
     } 

單擊的項目是列表視圖rappresentation:

public class categorie { 
     private long id; 
     private String nome; 
     private long preferita; 

     public long getId() { 
     return id; 
     } 

     public void setId(long id) { 
     this.id = id; 
     } 

     public String getNome() { 
     return nome; 
     } 

     public void setNome(String nome) { 
     this.nome = nome; 
     } 



     public long getPreferita() { 
     return preferita; 
    } 

    public void setPreferita(long preferita) { 
     this.preferita = preferita; 
    } 

    // Will be used by the ArrayAdapter in the ListView 
     @Override 
     public String toString() { 
     return nome; 
     } 
    } 

和我列出他們通過:

datasource = new pollDataSource(this); 
     datasource.open(); 

     Cursor values = datasource.getAllCategorie(); 

     String[] categorieColumns = 
      { 
       MySQLiteHelper.COLUMN_NOME // Contract class constant containing the word column name 

      }; 


      int[] mWordListItems = { R.id.categoria_label }; 


     SimpleCursorAdapter adapter = new SimpleCursorAdapter(
       getApplicationContext(),    // The application's Context object 
       R.layout.single_list_item,    // A layout in XML for one row in the ListView 
       values,        // The result from the query 
       categorieColumns,      // A string array of column names in the cursor 
       mWordListItems,      // An integer array of view IDs in the row layout 
       0);         // Flags (usually none are needed) 


     setListAdapter(adapter); 

在新的活動,我wou ld喜歡在給定的類別(我點擊過的)中收集數據庫中的一些「問題」......我怎樣才能將點擊的類別傳遞給新的活動?

回答

1

你可能想這樣的:

Cursor data = (Cursor)l.getItemAtPosition(position); 
String cat = data.getString(cursor.getIndexColumn(MySQLiteHelper.COLUMN_NOME)); 
Intent myIntent = new Intent(MainActivity.this, sondaggioActivity.class); 
myIntent.putExtra("categorieName", cat); 
MainActivity.this.startActivity(myIntent); 

然後在sondaggioActivity活動,您可以使用getIntent()讓啓動該活動的Intent和檢索分類名稱。

+0

是cursor = Cursor?或者光標是別的東西? –

+1

@peorthyr這是一個錯誤,請參閱我編輯的答案。 – Luksprog

1

你知道意圖嗎?您可以將簡單的原語,字符串和更多內容放入將開始下一個活動的意圖中。它具有鍵值關係,如HashMap。這裏有一個簡單的例子。

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    int selection = position; 
    Intent myIntent = new Intent(MainActivity.this, sondaggioActivity.class); 
    myIntent.putExtra("SELECTION", selection); // <--- put the value (KEY, VALUE) 
    MainActivity.this.startActivity(myIntent); 
} 

然後在您的新活動:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    int category = getIntent().getExtras().getInt("SELECTION"); // <-- get the value, with the same KEY 
    switch (category) { 
    case 1: 
     // do something 
     break; 
    case 2: 
     // do something else 
     break; 
    } 
} 
相關問題