Delete selected row in list in android

I have a listview with a custom row that has a text view and a clickable image to delete the row. Listview gets data from sqlite database. I have successfully loaded data from database to list. I want to delete this row from the database when the delete button is clicked. I know it requires binding an onclick event handler to the delete button and getting the object or sqlite data associated with the string. How can I get the sqlite data associated with the row the user clicked on the delete button on? Here is my adapter in 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;

}
}

      

Here is my CommentsDataSource class

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;
}

}

      

+3


source to share


4 answers


Change the getView method as shown below:



@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;
}

      

+2


source


You need to install onlistitemclicklistener

to advanced ListActivity

. It will be something like this.

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

      



Now make a handler to delete the selected row. You have listview

, current view

and indexed id

. Hope it works

0


source


write a listener for button click and in onClick () method, use this code:

@Override
protected void onClick(View view){
    view.getParent().setVisibility(View.GONE);
}

      

I hope this works for you. Alternatively, since I don't have time to test the above code, if that doesn't work, in the method getView()

you can keep view

and the button two separate ArrayList

, then get the button's index in onClick()

, using the button, ArrayList

and set the visibility of the same index in the view ArrayList

as GONE

. This method is messy, so I prefer the above method.

0


source


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

      

-1


source







All Articles