Jun Kuan
Jun Kuan

Reputation: 49

Deleting data from SQLite using listview by clicking Delete Button

Okay, so my supervisor wants me to do this:

Please refer to this link to see what I'm talking about since I'm not good in expressing myself in English.

screen shot

I've already made a way to show all the data that are saved in the database, but I really have no idea on how to automatically put an Delete button beside the data and delete it by clicking it.

Here's my code: (If there are more simple codes that you can suggest, then I'll be more glad.)

public class CheckData extends ListActivity implements OnClickListener  {     
    TextView selection;
    public int idToModify; 
    DataManipulator dm;

    List<String[]> list = new ArrayList<String[]>();
    List<String[]> names2 =null ;
    String[] stg1;
    protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.check);
    dm = new DataManipulator(this);
    names2 = dm.selectAll();
    stg1=new String[names2.size()]; 
    int x=0;
    String stg;
    for (String[] name : names2) {
        stg = name[1];
        stg1[x]=stg;
        x++;
//ONCLICK
        View homeonviewall = findViewById(R.id.homeonviewall);
        homeonviewall.setOnClickListener(this);
        View newdataonviewall = findViewById(R.id.newdataonviewall);
        newdataonviewall.setOnClickListener(this);     

    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,stg1);
        this.setListAdapter(adapter);
    selection=(TextView)findViewById(R.id.selection);
   }      
@Override
    public void onClick(View v) {
                // TODO Auto-generated method stub
        switch(v.getId()){
            case R.id.homeonviewall:
                    Intent a = new Intent(this, Dbsample.class);  
                    startActivity(a);
                    break;
                    case R.id.newdataonviewall:
                    Intent b = new Intent(this, SaveData.class);  
                    startActivity(b);
                    break;

                        }
            }  
}

Upvotes: 0

Views: 1401

Answers (1)

Sam
Sam

Reputation: 86948

1) Make a layout to display the name and the Delete button. Pass the layout to your adapter, where you currently use android.R.layout.simple_list_item_1, for example

new ArrayAdapter<String>(this, R.layout.row_layout_delete, stg1);

2) Extend your Adapter and override getView() to add an OnClickListener to your button. Inside onClick() simply delete the current row.

I recommend using a CursorAdapter, like SimpleCursorAdapter, they are specifically designed to link database information to a ListView.


This answer covers an extra topic, but I provide details on how to extend an Adapter and implement an OnClickListener: How to attach multiple touch actions to a single list item?, the "quick and dirty" answer should help you. You should also watch the Google I/O presentation Turbo-Charge Your UI to get the most from your Adapters.

Upvotes: 1

Related Questions