2010-11-10 35 views
2

我想在ListView上的每一行添加一個按鈕。該按鈕然後將從表中複製已經被點擊的行,並將該行插入到另一個表中。我需要知道的是如何將點擊行rowId分配給我的按鈕方法,因爲我需要將它傳遞給copyData方法。這是我的主代碼,按鈕方法在底部。通過一個按鈕傳遞ListView的rowId

public class Favourites extends ListActivity { 
private static final int ACTIVITY_CREATE=0; 
private static final int ACTIVITY_EDIT=1; 

private static final int INSERT_ID = Menu.FIRST; 
private static final int DELETE_ID = Menu.FIRST + 1; 

private FavouritesDbAdapter mDbHelper; 



/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.favourites_list); 
    mDbHelper = new FavouritesDbAdapter(this); 
    mDbHelper.open(); 
    fillData(); 
    registerForContextMenu(getListView()); 
} 

private void fillData() { 
    Cursor notesCursor = mDbHelper.fetchAllNotes(); 
    startManagingCursor(notesCursor); 

    // Create an array to specify the fields we want to display in the list 
    String[] from = new String[]{FavouritesDbAdapter.KEY_TITLE,FavouritesDbAdapter.KEY_ROWID}; 

    // and an array of the fields we want to bind those fields to 
    int[] to = new int[]{R.id.text1,R.id.text2}; 

    // Now create a simple cursor adapter and set it to display 
    SimpleCursorAdapter notes = 
     new SimpleCursorAdapter(this, R.layout.favourites_row, notesCursor, from, to); 
    setListAdapter(notes); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    super.onCreateOptionsMenu(menu); 
    menu.add(0, INSERT_ID, 0, R.string.menu_insert); 
    return true; 
} 

@Override 
public boolean onMenuItemSelected(int featureId, MenuItem item) { 
    switch(item.getItemId()) { 
     case INSERT_ID: 
      createNote(); 
      return true; 
    } 

    return super.onMenuItemSelected(featureId, item); 
} 

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, 
     ContextMenuInfo menuInfo) { 
    super.onCreateContextMenu(menu, v, menuInfo); 
    menu.add(0, DELETE_ID, 0, R.string.menu_delete); 
} 

@Override 
public boolean onContextItemSelected(MenuItem item) { 
    switch(item.getItemId()) { 
     case DELETE_ID: 
      AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
      mDbHelper.deleteNote(info.id); 
      fillData(); 
      return true; 
    } 
    return super.onContextItemSelected(item); 
} 

private void createNote() { 
    Intent i = new Intent(this, FavouritesEdit.class); 
    startActivityForResult(i, ACTIVITY_CREATE); 
} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    Intent i = new Intent(this, Favourites.class); 
    Intent i1 = new Intent(this, FavouritesEdit.class); 
    i1.putExtra(FavouritesDbAdapter.KEY_ROWID, id); 
    i.putExtra(FavouritesDbAdapter.KEY_ROWID, id); 
    startActivityForResult(i1, ACTIVITY_EDIT); 

} 


@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    super.onActivityResult(requestCode, resultCode, intent); 
    fillData(); 
} 


public void button(View view) { 

    mDbHelper.copyData(#ROWID of CLICKED BUTTON TO BE PASSED OVER#); 
    fillData(); 

} 

}

+0

使用選定的行ID設置局部變量會更容易,您不覺得嗎? – ykatchou 2010-11-10 13:40:51

+0

我不知道,爲什麼我問:) – Sazzle 2010-11-10 14:02:53

回答

0

我已經做到了前是這樣的:

 ListView list_Builds = (ListView)findViewById(R.id.list_Builds); 
     list_Builds.setAdapter(adapter); 

     list_Builds.setOnItemClickListener(new OnItemClickListener(){ 
      @Override 
      public void onItemClick(AdapterView parent, View view, int position, long id) 
      { 
        // BuildID is my primary key in the table 

        TextView lbl_BuildID = (TextView)view.findViewById(R.id.lbl_BuildID); 
        String buildID = (String)lbl_BuildID.getText(); 

        TextView lbl_BuildName = (TextView)view.findViewById(R.id.lbl_BuildName); 
        String buildName = (String)lbl_BuildName.getText(); 

        TextView lbl_BuildDescription = (TextView)view.findViewById(R.id.lbl_BuildDescription); 
        String buildDescription = (String)lbl_BuildDescription.getText(); 

        // I passed it on to a new intent. But here you could call your database 
        Intent i = new Intent(getBaseContext(), ViewBuild.class); 

        i.putExtra("BuildID", buildID); 
        i.putExtra("BuildName", buildName); 
        i.putExtra("BuildDescription", buildDescription); 
        startActivity(i); 
       } 
       catch(Exception ex) 
       { 
        Log.println(1, "item-click-event", ex.getMessage()); 
       } 
      } 
     }); 

基本上,當我綁定到列表視圖中,我有一個包含一個隱藏的 「BuildID」 的TextView數據庫中該行的主鍵ID。

相關問題