5

category_idと の 2 つの列を持つカテゴリ テーブルがありますname。という名前のデータ ヘルパー クラスを作成しましたCategoryDataHelpergetCategoryCursor()カテゴリ テーブルから ID と名前を取得し、カーソルを返すヘルパー クラスの名前のメソッドがあります。そのカーソルを使用SimpleCursorAdapterして、カテゴリのリストを表示していました。正常に動作しています。

public class Categories extends ListActivity  {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        categoryDataHelper = new CategoryDataHelper(getApplicationContext());
        Cursor categoryCursor  = categoryDataHelper.getCategoryCursor();
        ListAdapter adapter = new SimpleCursorAdapter (
                this,  
                android.R.layout.simple_list_item_1,
                categoryCursor,                                              
                new String[] { CategoryDataHelper.NAME },           
                new int[] {android.R.id.text1});  

        // Bind to our new adapter.
        setListAdapter(adapter);

        list = getListView();
        list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Here I want the category_id  
            }
        });
    }    
}

ここで、選択したカテゴリのOnItemClickListenerインテントを実装して送信したいと思います。メソッドcategory_idでIDを取得するにはどうすればよいですか?onItemClick()

4

4 に答える 4

17

おそらく、アダプターからカーソルを取得する必要があります。このようにして、カーソルが置き換えられても、まだ有効なカーソルを取得しています。

Cursor cursor = ((SimpleCursorAdapter) adapterView).getCursor();
cursor.moveToPosition(position);
long categoryId = cursor.getLong(cursor.getColumnIndex(CategoryDataHelper.ID));

または、"category_id"またはの代わりに列の名前を使用しますCategoryDataHelper.ID

于 2011-04-05T14:22:55.657 に答える
3

ありがとうザック、私はあなたの投稿で解決できました...素晴らしい!!! ...アクティビティから別のアクティビティにパラメーターを送信します。

Intent myIntent = new Intent(Clientes.this, Edc.class);
Cursor cursor = (Cursor) adapter.getItem(position);
myIntent.putExtra("CLIENTE_ID", cursor.getInt(cursor.getColumnIndex("_id")));
startActivity(myIntent);

他のアクティビティ(EDC)で....私はパラメータを取得します:

int _clienteId = getIntent().getIntExtra("CLIENTE_ID", 0);
于 2011-08-13T17:12:10.200 に答える
1

onItemclickではどうですか:

categoryCursor.moveToPosition(position);

次に、返されたカーソルからヘルパーからIDを取得しますか?

于 2011-04-05T14:13:28.910 に答える
1

を使用するSimpleCursorAdapterと、onItemClick関数は選択した項目のデータベース ID を渡します。したがって、解決策は簡単です

long category_id = id
于 2016-01-20T01:58:04.940 に答える