2012-03-01 12 views
0

Listviewでアイテムの_idを取得する必要があります。私が今取得できるのはポジションだけです。どうやってやるの?。これは私が使用しているコードです。ListViewから_idを取得する方法

public class OSListActivity extends ListActivity { 
................ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
................ 
................ 
     MatrixCursor cursor; 
     cursor = datasource.getnameList(); 
     if (cursor.moveToFirst()) { 
      startManagingCursor(cursor); 
      String vdstatus = cursor.getString(6); 
      System.out.println("vdstatus : " + vdstatus); 
      String[] from = { "name", "desc", "status", "path", "folder", 
        BaseColumns._ID }; 
      int[] to = { R.id.name, R.id.desc, R.id.status, R.id.path }; 
      final VSsimplecursoradapter adapter = new VSsimplecursoradapter(
        this, R.layout.row, cursor, from, to); 
      setListAdapter(adapter); 
     } 
    } 

    @Override 
    public void onListItemClick(ListView parent, View view, int position, 
      long id) { 
     Intent intent = new Intent(this, VSDetailsActivity.class); 
     intent.putExtra("com.manager.boot.r1223."+BaseColumns._ID, position);//TODO put _id instead of position 
     startActivity(intent); 
    } 
} 

編集:ソリューション

public void onListItemClick(ListView parent, View view, int position, 
     long id) { 
    // Starts TestDetails activity on clicking a list item. 
    Cursor cursor = (Cursor) adapter.getItem(position); 
    Intent intent = new Intent(this, VSDetailsActivity.class); 
    intent.putExtra("com.manager.boot.r1223."+BaseColumns._ID, cursor.getInt(cursor 
      .getColumnIndex("_id"))); 
    startActivity(intent); 
} 
+0

_idはどうですか?あなたは精巧にする必要があります。 – straya

+0

@strayaこれは、アダプタが列名として使用する文字列 'BaseColumns._ID'です。 –

+0

質問をするときにあいまいにならない理由の良い例です! – straya

答えて

0

あなたはすでにそれを持っています。メソッドのパラメータを見てください。 「長いID」の部分を参照してください。それはあなたが探しているものです。

0

私はString s = ((Cursor) l.getItemAtPosition(position)).getString(0);を使用し、それを長文に解析します。

関連する問題