通过按钮传递ListView的rowId

时间:2010-11-10 13:25:33

标签: android

我正在尝试为ListView上的每一行添加一个按钮。然后,该按钮将从表中复制行(已单击)并将该行插入另一个表中。我需要知道的是如何将单击的行rowId分配给我的按钮方法,因为我需要将其传递给copyData方法。这是我的主要代码,按钮方法位于底部。

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

}

}

1 个答案:

答案 0 :(得分: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。