dimanche 22 février 2015

Android SQlite Using two spinners to act as search filters

So I have a simple DB and an AccountManager for that DB. In AccountManager I have a grid where all the accounts pop up when queried and spinners to serve as search filters since I only want to search it by a specific field. I started off with just one spinner to filter the accounts by STATUS. So now it would filter the accounts based on that STATUS. If I chose Status 1 on the spinner it would only show accounts with STATUS 1 and so on.


So like any newbie to programming, having a nice working code I went ahead and cloned it to make another spinner to use as another filter for searching. This time to search accounts by their TERMS. So now the plan was to use the two spinners either INDIVIDUALLY or TOGETHER to filter the accounts. The only problem being that once I cloned the necessary codes and tweaked it a bit now nothing shows on the grid whether I use just one of the spinners or both.


I'm not sure if my two spinners are conflicting each other or cancelling each other out, if anyone can see the problem or have their own recommendations on how to do the search filter thing better I'm all ears as well. Also feel free to ask for any other bits of the code for reference.


AccountManager:



public class AccountManager extends Activity {
DatabaseHelper dbHelper;
static public GridView grid;
TextView txtTest;
TextView txtTest2;
TextView txtAccounts;
Spinner spinStat;
Spinner spinTerm;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.gridview);
grid = (GridView) findViewById(R.id.grid);
txtTest = (TextView) findViewById(R.id.txtTest);
spinStat = (Spinner) findViewById(R.id.spinStat);
spinTerm = (Spinner) findViewById(R.id.spinTerm);

Button addbtn = (Button) findViewById(R.id.addbtn);
addbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent myIntent = new Intent(AccountManager.this, AddAccount.class);
startActivityForResult(myIntent, 0);
}
});

Button allacc = (Button) findViewById(R.id.allacc);
allacc.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
ShowGrid();

}
});
final DatabaseHelper db = new DatabaseHelper(this);

Utilities.ManageTermSpinner(this.getParent(), spinTerm);

try {

spinTerm.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
LoadGrid();

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {

}
});

} catch (Exception ex) {
txtTest.setText(ex.toString());
}


Utilities.ManageStatSpinner(this.getParent(), spinStat);
try {

spinStat.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
LoadGrid();
//sca.notifyDataSetChanged();

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {

}
});

} catch (Exception ex) {
txtTest2.setText(ex.toString());
}

try {
grid.setOnItemClickListener(new OnItemClickListener() {

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

SQLiteCursor cr = (SQLiteCursor) parent.getItemAtPosition(position);
String name = cr.getString(cr.getColumnIndex(DatabaseHelper.colName));
int amount = cr.getInt(cr.getColumnIndex(DatabaseHelper.colAmount));
String purpose = cr.getString(cr.getColumnIndex(DatabaseHelper.colPurpose));
String Terms = cr.getString(cr.getColumnIndex(DatabaseHelper.colTermsClass));
String Status = cr.getString(cr.getColumnIndex(DatabaseHelper.colStatClass));
String date = cr.getString(cr.getColumnIndex(DatabaseHelper.colDate));
String editdate = cr.getString(cr.getColumnIndex(DatabaseHelper.colEditDate));
Account acc = new Account(name, amount, purpose, db.GetTermsID(Terms),date,editdate,db.GetStatID(Status));
acc.SetID((int) id);
AlertDialog diag = Alerts.ShowEditDialog(AccountManager.this, acc);
diag.setOnDismissListener(new OnDismissListener() {

@Override
public void onDismiss(DialogInterface dialog) {
txtTest.setText("dismissed");
LoadGrid();
}
});
diag.show();
} catch (Exception ex) {
Alerts.CatchError(AccountManager.this, ex.toString());
}
}


}
);
} catch (Exception ex) {

}

}

@Override
public void onStart() {
super.onStart();
//txtAccounts.setText(txtAccounts.getText()+String.valueOf(dbHelper.getAccountCount()));
}


public void ShowGrid() {
dbHelper = new DatabaseHelper(this);
try {

Cursor a = dbHelper.getAllAccounts();
startManagingCursor(a);

String[] from = new String[]{DatabaseHelper.colName, DatabaseHelper.colAmount, DatabaseHelper.colTermsClass, DatabaseHelper.colStatClass};
int[] to = new int[]{R.id.colName, R.id.colAmount, R.id.colTerms, R.id.colStat};


SimpleCursorAdapter saa = new SimpleCursorAdapter(this, R.layout.gridrow, a, from, to);
grid.setAdapter(saa);


} catch (Exception ex) {
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setMessage(ex.toString());
b.show();
}
}

public void LoadGrid() {
dbHelper = new DatabaseHelper(this);
try {



View v = spinTerm.getSelectedView();
TextView txt = (TextView) v.findViewById(R.id.txtTermClass);
String Terms = String.valueOf(txt.getText());
Cursor c = dbHelper.getAccByTerms(Terms);
startManagingCursor(c);

View x = spinStat.getSelectedView();
TextView txt2 = (TextView) x.findViewById(R.id.txtStatID);
String Status = String.valueOf(txt2.getText());
Cursor b = dbHelper.getAccByStatus(Status);
startManagingCursor(b);

String[] from = new String[]{DatabaseHelper.colName, DatabaseHelper.colAmount, DatabaseHelper.colTermsClass, DatabaseHelper.colDate, DatabaseHelper.colStatClass};
int[] to = new int[]{R.id.colName, R.id.colAmount, R.id.colTerms, R.id.colDate, R.id.colStat};

SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.gridrow, c, from, to);
grid.setAdapter(sca);

SimpleCursorAdapter sba = new SimpleCursorAdapter(this, R.layout.gridrow, b, from, to);
grid.setAdapter(sba);


} catch (Exception ex) {
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setMessage(ex.toString());
b.show();
}
}
}


Alerts:



public class Alerts {

public static void ShowAccAddedAlert(Context con)
{
AlertDialog.Builder builder=new AlertDialog.Builder(con);
builder.setTitle("Add new Account");
builder.setIcon(android.R.drawable.ic_dialog_info);
DialogListener listener=new DialogListener();
builder.setMessage("Account Added successfully");
builder.setPositiveButton("ok", listener);

AlertDialog diag=builder.create();
diag.show();
}

public static AlertDialog ShowEditDialog(final Context con,final Account Acc)
{
AlertDialog.Builder b=new AlertDialog.Builder(con);
b.setTitle("Account Details");
LayoutInflater li=LayoutInflater.from(con);
View v=li.inflate(R.layout.editdialog, null);

b.setIcon(android.R.drawable.ic_input_get);

b.setView(v);
final TextView txtName=(TextView)v.findViewById(R.id.txtDelName);
final TextView txtAmt=(TextView)v.findViewById(R.id.txtDelAmt);
final TextView txtPurpose=(TextView)v.findViewById(R.id.txtDelPurpose);
final Spinner spinTerm=(Spinner)v.findViewById(R.id.spinTerm);
final Spinner spinStat=(Spinner)v.findViewById(R.id.spinStat);



Utilities.ManageTermSpinner(con, spinTerm);
for(int i=0;i<spinTerm.getCount();i++)
{
long id=spinTerm.getItemIdAtPosition(i);
if(id==Acc.getTerms())
{
spinTerm.setSelection(i, true);
break;
}
}

Utilities.ManageStatSpinner(con, spinStat);
for(int j=0;j<spinStat.getCount();j++)
{
long id=spinStat.getItemIdAtPosition(j);
if(id==Acc.getStatus())
{
spinStat.setSelection(j, true);
break;
}
}


txtName.setText(Acc.getName());
txtAmt.setText(String.valueOf(Acc.getAmt()));

b.setPositiveButton("Modify", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Acc.setName(txtName.getText().toString());
Acc.setAmt(Integer.valueOf(txtAmt.getText().toString()));
Acc.setPurpose(txtPurpose.getText().toString());
Acc.setTerms((int) spinTerm.getItemIdAtPosition(spinTerm.getSelectedItemPosition()));
Acc.setStatus((int) spinStat.getItemIdAtPosition(spinStat.getSelectedItemPosition()));
try
{
DatabaseHelper db=new DatabaseHelper(con);
db.UpdateAcc(Acc);

}
catch(Exception ex)
{
CatchError(con, ex.toString());
}
}
});

b.setNeutralButton("Delete", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
DatabaseHelper db=new DatabaseHelper(con);
db.DeleteAcc(Acc);
}
});
b.setNegativeButton("Cancel", null);

return b.create();

}

static public void CatchError(Context con, String Exception)
{
Dialog diag=new Dialog(con);
diag.setTitle("Error");
TextView txt=new TextView(con);
txt.setText(Exception);
diag.setContentView(txt);
diag.show();
}
}


Utilities:



public class Utilities {
static public void ManageTermSpinner(Context context, Spinner view) {
DatabaseHelper dbHelper = new DatabaseHelper(context);

Cursor c = dbHelper.getAllTerms();

SimpleCursorAdapter ca = new SimpleCursorAdapter(context, R.layout.termspinnerrow, c, new String[]{DatabaseHelper.colTermsClass, "_id"}, new int[]{R.id.txtTermClass});

view.setAdapter(ca);


}

static public void ManageStatSpinner(Context context, Spinner view) {
DatabaseHelper dbHelper = new DatabaseHelper(context);

Cursor d = dbHelper.getAllStatus();

SimpleCursorAdapter da = new SimpleCursorAdapter(context,R.layout.statspinnerrow, d, new String[]{DatabaseHelper.colStatClass,"_id"}, new int[]{R.id.txtStatClass});

view.setAdapter(da);
}
}

Aucun commentaire:

Enregistrer un commentaire