I'm building an app where I've to create multiple tables on a SQLite database.
The best example I can give is of one store that I'll have to manage, so I'm creating one table for each category (wines, beers, milk, etc...) of products where I have to manage the brands that I'm selling but, if I quit from selling one of this categories I would like to be able to drop this category table.
But I tried many examples from the web and nothing works. I'm always getting the same error.
Because I've many items on my tables I built a custom list view and a custom adapter and both are working perfectly, I can delete or add new items without problems, build new tables, but the same approach doesn't work to drop my built tables.
I intentionally let my database opened until my activity is destroyed so I don't have to open it trough my adapter.
Maybe someone can help me how to drop a table with the name passed from a String, using one button or in this case, would be really helpful using my adapter button, because my idea is to add buttons on my list view.
Just to make clear, I can drop the tables if I create one editText to type my tables names and use a button to run this code:
dataBase.execSQL("DROP TABLE IF EXISTS '" + tableName + "'");
or if I run this code during the execution of the method onCreate
and I give a name from a table that really exists.
dataBase.execSQL("DROP TABLE IF EXISTS 'test 1'");
So my question is not how to handle NullPointerException
but how to manipulate my SQLite database from my custom adapter.
Thank you!
this is my adapter code:
public class MyCustomAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
private SQLiteDatabase dataBase;
private String tableName;
public MyCustomAdapter(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int pos) {
return list.get(pos);
}
@Override
public long getItemId(int pos) {
//return list.get(pos).getId();
//just return 0 if your list items do not have an Id variable.
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item_clickable, null);
}
//Handle TextView and display string from your list
TextView listItemText = (TextView)view.findViewById(R.id.list_item_textview);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
Button deleteBtn = (Button)view.findViewById(R.id.delete_btn);
deleteBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//criar alertDialog
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle("Delete!");
dialog.setMessage("Do you wish to delete this ToDo list?");
dialog.setCancelable(false);
dialog.setIcon(android.R.drawable.ic_delete);
dialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(context, "No!", Toast.LENGTH_SHORT).show();
}
});
dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
final String tableName= list.get( position ).toString();
try {
dataBase.execSQL("DROP TABLE IF EXISTS '" + tableName + "'");
//dataBase.execSQL("DROP TABLE IF EXISTS 'test 1'");
list.remove(position); //or some other task
Toast.makeText(context, "Tabela " + tableName + " removida com sucesso!", Toast.LENGTH_SHORT).show();
notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
}
});
dialog.create();
dialog.show();
}
});
listItemText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Toast.makeText(context, "Item position is: " + list.get(position), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, MainActivity.class);
String tabelaNome = list.get( position ).toString();
intent.putExtra("tableName", tableName);
context.startActivity(intent);
}
});
return view;
}
Note the difference between your two SQL statements:
dataBase.execSQL("DROP TABLE IF EXISTS '" + tableName + "'");
dataBase.execSQL("DROP TABLE IF EXISTS teste 1");
The one you say works does not have quotes around the table name.