Android:单击Button时获取id

时间:2013-08-09 16:36:50

标签: android listview button sqlite

我正在创建一个包含书签的词典,我希望有一个活动,我可以删除书签中的个别数据..这是我想要做的样本布局

enter image description here

在每个数据的左下角有一个按钮,可以删除项目是否有一个按钮的方法,可以获取项目的_id这是我如何在列表视图中输出我的数据

ourCursor = dbDictionaryHelper.getBookmarked(); startManagingCursor(ourCursor); adapter = new DeleteBookmarkAdapter(ourCursor); lvDeleteBookmark.setAdapter(adapter);

    class DeleteBookmarkAdapter extends CursorAdapter {
    DeleteBookmarkAdapter(Cursor c) {
        super(DeleteBookmarkActivity.this, c);
    }

    public void bindView(View row, Context ctxt, Cursor c) {
        DictionaryHolder holder = (DictionaryHolder) row.getTag();
        holder.populateFrom(c, dbDictionaryHelper);
    }

    public View newView(Context ctxt, Cursor c, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.deletebookmarkdata, parent,
                false);
        DictionaryHolder holder = new DictionaryHolder(row);
        row.setTag(holder);
        return (row);
    }
}

static class DictionaryHolder implements OnClickListener {
    private TextView word = null;
    private Button btnRemove = null;

    DictionaryHolder(View row) {
        word = (TextView) row.findViewById(R.id.tvDeleteBookmark);
    }

    void populateFrom(Cursor c, DictionaryHelper r) {
        word.setText(r.getName(c));
    }

希望你能帮助我......谢谢

2 个答案:

答案 0 :(得分:0)

您可以使用ListView。活动显示所需数据的列表视图。在此列表视图中,项目上的触摸操作将其删除 你会有更好的设计。用户不仅可以点击一个小按钮,还可以点击元素的所有行来删除它 在列表视图中使用此方法实现删除操作:

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // Do something when a list item is clicked
}

Here the reference of ListView

答案 1 :(得分:0)

您可以找到按钮并设置自定义侦听器:

public void bindView(View view, Context context, Cursor cursor) {
        String name = cursor.getString(itemNameIndex);
        boolean checked = cursor.getInt(itemCheckIndex) > 0;
        TextView nameTxt = (TextView) view.findViewById(R.id.item_name);
        Button removeBtn= (Button) view.findViewById(R.id.item_remove);
        checkChk.setOnClickListener(new OnRemoveItemClickListener(name));
        nameTxt.setText(name);
    }
@Override
    public View newView(Context context, Cursor c, ViewGroup parent) {
        View view = inflater.inflate(R.layout.row_item, null);
        return view;
    }