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

点击的项目是listview 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);

在新活动中,我想在数据库中的给定类别(我点击的那个)中收集一些“问题”......我如何将点击的类别传递给新活动?

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 回答