我有一个自定义行的列表视图,其中包含textview和用于删除行的可单击图像。 Listview从sqlite数据库获取数据。我已成功将数据库中的数据加载到listview中。我想要的是在按下删除按钮时从数据库中删除该行。我知道这需要将onclick事件处理程序绑定到删除按钮并获取与该行绑定的对象或sqlite数据。如何获取与用户单击删除按钮的行绑定的sqlite数据? 这是我对listview的适配器
public class CommentAdapter extends ArrayAdapter<Comment> {
// private objects
private List<Comment> mListComment;
private LayoutInflater mInflater;
public CommentAdapter(Context c,int textViewResourceId, List<Comment> list) {
super(c, textViewResourceId, list);
mListComment = list;
// create layout inflater
mInflater = LayoutInflater.from(c);
}
@Override
public int getCount() {
return mListComment.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// get view reference
View view = convertView;
// if null
if(view == null) {
// inflate new layout
view = mInflater.inflate(R.layout.comment_row, null);
// create a holder
ViewHolder holder = new ViewHolder();
// find controls
holder.txtComment = (TextView)view.findViewById(R.id.txtComment);
holder.btnDelete = (ImageView)view.findViewById(R.id.btnDelete);
// set data structure to view
view.setTag(holder);
}
Comment cmt = mListComment.get(position);
// if not null
if(cmt != null) {
// query data structure
ViewHolder holder = (ViewHolder)view.getTag();
// set data to display
holder.txtComment.setText(cmt.getComment());
holder.btnDelete.setClickable(true);
holder.btnDelete.setImageResource(android.R.drawable.ic_delete);
}
// return view
return view;
}
/*
* @class ViewHolder
* to hold data structure on view with comment info
*/
static class ViewHolder {
private TextView txtComment;
private ImageView btnDelete;
}
}
这是我的CommentsDataSource类
public class CommentsDataSource {
private SQLiteDatabase database;
private DatabaseHelper dbHelper;
private String[] allColumns = { DatabaseHelper.COLUMN_ID, DatabaseHelper.COLUMN_COMMENT };
public CommentsDataSource(Context context) {
dbHelper = new DatabaseHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public Comment createComment(String comment) {
//insert into databasae
return newComment;
}
private Comment cursorToComment(Cursor cursor) {
//convert cursor to comment
return comment;
}
public void deleteComment(Comment comment) {
//delete row from datbase
}
public List<Comment> getAllComments() {
//get all rows
return comments;
}
}
答案 0 :(得分:2)
按以下方式移动getView方法:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// get view reference
View view = convertView;
// if null
if(view == null) {
// inflate new layout
view = mInflater.inflate(R.layout.comment_row, null);
// create a holder
ViewHolder holder = new ViewHolder();
// find controls
holder.txtComment = (TextView)view.findViewById(R.id.txtComment);
holder.btnDelete = (ImageView)view.findViewById(R.id.btnDelete);
// set data structure to view
view.setTag(holder);
}
final Comment cmt = mListComment.get(position);
// if not null
if(cmt != null) {
// query data structure
ViewHolder holder = (ViewHolder)view.getTag();
// set data to display
holder.txtComment.setText(cmt.getComment());
holder.btnDelete.setClickable(true);
holder.btnDelete.setImageResource(android.R.drawable.ic_delete);
holder.btnDelete.setOnClickListener(new View.OnClickListener(){
public void onClick(View view)
{
commentsDataSource.deleteComment(cmt);
//Requery DB to get Updated results
notifyDataSetChanged();
}
})
}
// return view
return view;
}
答案 1 :(得分:0)
您需要在展开的onlistitemclicklistener
中设置ListActivity
。它会是这样的。
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
ViewHolder item = (ViewHolder ) getListAdapter().getItem(position);
Toast.makeText(this, item.txtComment + " selected", Toast.LENGTH_LONG).show();
}
现在,让您的处理程序删除所选行。您有listview
,current view
和indexed id
。希望,它会起作用
答案 2 :(得分:0)
为按钮单击编写一个监听器,在onClick()方法中,使用以下代码:
@Override
protected void onClick(View view){
view.getParent().setVisibility(View.GONE);
}
我希望这对你有用。
或者,由于我没有时间检查上面的代码,如果这不起作用,在getView()
方法中,您可以将view
和按钮存储到两个单独的{{1}中然后,从按钮ArrayList
获取onClick()
中的按钮索引,并在视图ArrayList
中将相同索引的可见性设置为ArrayList
。这个方法有点脏,所以我更喜欢上面的方法。
答案 3 :(得分:-1)
Listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
List.remove(position);
notifyDataSetChanged();
}
});