Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cloudbank/d64f8d264e8995d4a8280bee3abef8b7 to your computer and use it in GitHub Desktop.
Save cloudbank/d64f8d264e8995d4a8280bee3abef8b7 to your computer and use it in GitHub Desktop.
Bad Activity
public class MistakesActivity extends ActionBarActivity
{
public static final String TABLE_NAME = "names";
ExecutorService executorService = Executors.newFixedThreadPool(4);
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mistakes);
findViewById(R.id.saveRows).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
SQLiteDatabase db = new MyDatabaseOpenHelper(MistakesActivity.this)
.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, new String[] {"fullName"},
null, null, null, null, null, null);
List<String> allVals = new ArrayList<String>();
while(cursor.moveToNext())
{
allVals.add(cursor.getString(0));
}
List<EditText> allRows = //Magically load a bunch of entered EditText values
for(EditText row : allRows)
{
executorService.submit(new InsertValThread(allVals, row));
}
db.close();
}
});
}
private class InsertValThread implements Runnable
{
List<String> allVals;
EditText enteredValue;
private InsertValThread(List<String> allVals, EditText enteredValue)
{
this.allVals = allVals;
this.enteredValue = enteredValue;
}
@Override
public void run()
{
String theVal = enteredValue.getText().toString();
if(!allVals.contains(theVal))
{
SQLiteDatabase db = new MyDatabaseOpenHelper(MistakesActivity.this)
.getWritableDatabase();
db.execSQL("insert into names(fullName) values('"+ theVal +"')");
allVals.add(theVal);
db.close();
}
}
}
public class MyDatabaseOpenHelper extends SQLiteOpenHelper
{
//Doesn't matter. Assume its perfect.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment