0

私はこのイベントハンドラを持っています:

@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();
        }

クリックされたアイテムは、次のリストビューのラププレゼンテーションです。

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);

新しいアクティビティで、データベース内の特定のカテゴリ (クリックしたもの) でいくつかの「質問」を収集したいと思います... クリックしたカテゴリを新しいアクティビティに渡すにはどうすればよいですか?

4

2 に答える 2

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;
    }
}
于 2012-12-19T15:12:27.033 に答える
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

于 2012-12-19T15:16:02.887 に答える