dimanche 1 mars 2015

Android SQLite Passing Item selected in gridview into another Activity for editing

Basically I have a Database with Accounts in them. I use GridView for viewing them, and from there you can select an Account and it will direct you to my Activity AccountManagement wherein you can modify, delete or just view all of the details of the selected Account from the GridView.


My problem is passing the data from the selection in GridView to AccountManagement.I'm not entirely sure how to use Intents for this properly. So far the code below doesn't work because I don't know how to pass the data into my AccountManagement and reference it properly from there.


Code is here, anything with Acc is problematic since Acc is where I'm supposed to put the data from GridView. I marked all those lines with a long ------ to make it easier to spot. Feel free to ask for any missing code.


AccountManagement snippet:



public class AccountDetails extends Activity {

DatabaseHelper dbHelper;
static public LinearLayout Linlay;
EditText txtName;
EditText txtAmt;
EditText txtPurpose;
TextView txtDate;
TextView txtEditDate;
Spinner spinStat;
Spinner spinTerm;

@Override
public void onCreate(Bundle savedInstanceState) {

Intent intent = getIntent();
Bundle bundle = intent.getExtras();

super.onCreate(savedInstanceState);
setContentView(R.layout.accdetails);

Linlay = (LinearLayout) findViewById(R.id.Linlay);
spinStat = (Spinner) findViewById(R.id.spinStat);
spinTerm = (Spinner) findViewById(R.id.spinTerm);
txtName = (EditText) findViewById(R.id.txtDelName);
txtAmt = (EditText) findViewById(R.id.txtDelAmt);
txtPurpose = (EditText) findViewById(R.id.txtDelPurpose);
spinTerm = (Spinner) findViewById(R.id.spinTerm);
txtDate = (TextView) findViewById(R.id.colDate);
txtEditDate = (TextView) findViewById(R.id.colEditDate);
spinStat = (Spinner) findViewById(R.id.spinStat);


Utilities.ManageTermSpinner(AccountDetails.this, 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(AccountDetails.this, 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()));-------------------------
txtPurpose.setText(Acc.getPurpose());---------------------------------



Button delete = (Button) findViewById(R.id.delete);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog diag = Alerts.DeleteConfirm(AccountDetails.this, Acc );--------------------------------
diag.show();
}
});
}


GridView snippet:


This is where I should be getting the data to put into Acc but I'm not sure how, so everything here can be changed since I'm not sure if this is the ideal way to do it.



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);

Intent myIntent = new Intent(AccountManager.this, AccountDetails.class);
Bundle extras = new Bundle();
myIntent.putExtras(extras);
startActivityForResult(myIntent, 0);


} catch (Exception ex) {
Alerts.CatchError(AccountManager.this, ex.toString());
}
}


});
} catch (Exception ex) {

}

Aucun commentaire:

Enregistrer un commentaire