Skip to content

Instantly share code, notes, and snippets.

@kpgalligan
Last active November 16, 2018 13:15
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kpgalligan/8595c2522164ab316f9c to your computer and use it in GitHub Desktop.
Save kpgalligan/8595c2522164ab316f9c 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.
}
}
@dfreire770
Copy link

Hi, I just applied for the Android developer position. It seems like the mistake here is when you declare a list of EditText, there is no reference to which editText are you adding the string. When loading the UI, it just wont appear, because its a list of new empty objects. To fix this you could use findElementById and try to get the id of the object (editText) and then, add the data.
But I think that using an editText, you need the append method to add a string or text, not submit.
Another alternative could be that you are calling a submit method of a custom made editText, like an adapter.

@AvinashReddy9
Copy link

Hi, I just applied for Android developer position. The mistake is lack of reference for the edit text to which the string is being added.Due to empty objects it does appear. We can use findviewbyID to get the id of the edit text that we are placing on the manifest design.

@kennethokereke
Copy link

Hey, I applied for the android position it seems that the error is a lack of reference on the editText. I prefer as well to use findviewbyID within the XML layout.

@navczydev
Copy link

I applied for an Android Developer Position.
After reviewing this code I found that there are various issues.

  1. Accessing Database on main thread will cause to freeze the app.
    2)Before accessing List allRows in for-each need to validate because it has not initialized which might throw NPE when we try to access it
  2. Opening and closing of Databases happened many time which we can avoid by using other techniques like Dependency Injection.
  3. Major problem is Thread-pool with fix size of 4 threads.
    For each row we call to new thread but we fixed number of thread which are 4 and if all the 4 threads busy and we send new request that will be in waiting state until one of those 4 threads available which will cause to Application not responding(ANR) error.
    Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment