2
public class DataViewActivity extends Activity{
SQLiteDatabase db;
SimpleCursorAdapter adapter;
String dbTable = "users";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_layout);
ListView listView = (ListView) findViewById(R.id.dbPop);

DBHelper dbhelper = new DBHelper(DataViewActivity.this);
db = dbhelper.getWritableDatabase();

Cursor cursor = db.query(dbTable, null, null, null, null, null, null);
startManagingCursor(cursor);

String[] from = new String[] { "name","_id"};
int[] to = new int[] { android.R.id.text1 };
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, from,
        to);

    listView.setAdapter(adapter);
  }
}

How to implement onClick listener to this code to delete selected database row. I don't know much about Android so for me its a learning curve.

1
  • 1
    Your question helped me a lot... :) Commented Apr 20, 2015 at 9:05

3 Answers 3

7

set setOnItemClickListener to your listview...

listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

            database.remove(id);//create removemethod in database class

        }
    });

and in remove method

    public void remove(long id){
        String string =String.valueOf(id);
        database.execSQL("DELETE FROM favorite WHERE _id = '" + string + "'");
    }
Sign up to request clarification or add additional context in comments.

3 Comments

I have a little question. How to store id in Db to make your solution work? Should I put integer primary key or integer primary key autoincrement or integer primary key autoincrement not null?
integer primary key autoincrement
Thanks for the reply, after questioning you I had read in sqlite documentation that autoincrement decreases the speed of the db and it doesn't make any difference whether you write integer primary key or integer primary key autoincrement.
3
    listview.setOnItemClickListener(new OnItemClickListener(){

    public void onItemClick(AdapterView<?> arg0, View v, int pos,long id) {
            // TODO Auto-generated method stub
      {
             TextView tv=(TextView)v;
     String s1=tv.getText().toString();
            //delete row
            String delgrp="DELETE FROM (tablename) WHERE  (row)='"+s1+"'";
    sdb.execSQL(delgrp);
      }

Comments

1

ListView.onListItemClick, as third parameter has position. Position is the entry returned by Adpater.getItem(int). So when you click on a row of your ListView, the ListView.onListItemClick is fired. There you can retrieve your Adapter entry and use the info you need to delete the entry from the database.

public void onListItemClick(ListView parent, View v, int position, long id) {
      // do something with the cursor
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.