10

This is the AccountListView, it retrieve and displays data that i have been added in database in a list view, i have added cash and bank account, when i clicked on cash in the list view it open the transaction intent, which has a spinner on which cash and bank has been added, i want it to display the data that i have clicked on the list view. Note that for the balance of cash and bank display succesfully only for the spinner.

public class AccountListActivity extends Activity implements OnClickListener, OnItemClickListener {

private ListView AccountListView;
private Button addNewAccountButton;

private ListAdapter AccountListAdapter;


private ArrayList<AccountDetails> pojoArrayList;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);

    AccountListView = (ListView) findViewById(R.id.AccountListView);
    AccountListView.setOnItemClickListener(this);
    registerForContextMenu(AccountListView);
    addNewAccountButton = (Button) findViewById(R.id.namesListViewAddButton);
    addNewAccountButton.setOnClickListener(this);

    pojoArrayList = new ArrayList<AccountDetails>();


    AccountListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, populateList());

    AccountListView.setAdapter(AccountListAdapter);

}
@Override  
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
super.onCreateContextMenu(menu, v, menuInfo);  
    menu.setHeaderTitle("Menu");  
    menu.add(0, v.getId(), 0, "Update");  
    menu.add(0, v.getId(), 0, "Delete");
    menu.add(0, v.getId(), 0, "Cancel");

}  

public List<String> populateList(){


    List<String> AccountList = new ArrayList<String>();


    DatabaseAdapter openHelperClass = new DatabaseAdapter(this);


    SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();


    Cursor cursor = sqliteDatabase.query(DatabaseAdapter.TABLE_ACCOUNT, null, null, null, null, null, null);


    startManagingCursor(cursor);


    while (cursor.moveToNext()) {


        String aBNAME = cursor.getString(cursor.getColumnIndex(DatabaseAdapter.KEY_BANKNAME));
        String aBTYPE = cursor.getString(cursor.getColumnIndex(DatabaseAdapter.KEY_TYPE));
        String aAccNum = cursor.getString(cursor.getColumnIndex(DatabaseAdapter.KEY_ACCNUM));
        String aBal = cursor.getString(cursor.getColumnIndex(DatabaseAdapter.KEY_BALANCE));
        String aEDate = cursor.getString(cursor.getColumnIndex(DatabaseAdapter.KEY_EXPIRYDATE));


        AccountDetails ugPojoClass = new AccountDetails();
        ugPojoClass.setaBankName(aBNAME);
        ugPojoClass.setaAccountType(aBTYPE);
        ugPojoClass.setaAccountNumber(aAccNum);
        ugPojoClass.setaBalance(aBal);
        ugPojoClass.setaDate(aEDate);

        pojoArrayList.add(ugPojoClass);

        AccountList.add(aBNAME);    
    }

    sqliteDatabase.close();

    return AccountList;
}

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

    Toast.makeText(getApplicationContext(), "Clicked on :" + arg2, Toast.LENGTH_SHORT).show();

    Intent updateDeleteAccountIntent = new Intent(this, Transaction.class);

    AccountDetails clickedObject =  pojoArrayList.get(arg2);

    Bundle dataBundle = new Bundle();
    dataBundle.putString("clickedBankName", clickedObject.getaBankName());
    dataBundle.putString("clickedBankType", clickedObject.getaAccountType());
    dataBundle.putString("clickedBankNumber", clickedObject.getaAccountNumber());
    dataBundle.putString("clickedBankBalance", clickedObject.getaBalance());
    dataBundle.putString("clickedExpiryDate", clickedObject.getaDate());


    updateDeleteAccountIntent.putExtras(dataBundle);

    startActivity(updateDeleteAccountIntent);

}

When the transaction intent is open it take value of the Transaction.java

public class Transaction extends Activity implements OnClickListener{

 private Spinner Category, Account, typerp;
 private TextView tvSaveNew, tvDisplayDate;
 private EditText ItemName, Amount, Notes;
 private EditText Balance, Result;
 private ImageButton TransDate, ImageButton1;
 private Button save, newt;

private String bundledBankName;
private String bundledBankType;
private String bundledBankNumber;
private String bundledBankBalance;
private String bundledBankDate;
private String BankNameValue;
private String NewBankBalanceValue;
private String BankTypeValue;
private String BankNumberValue;
private String BankBalanceValue;
private String BankDateValue;

 private int year;
 private int month;
 private int day;
 static final int DATE_DIALOG_ID = 999;

 private ArrayList<TransactionDetails> TransactionDetailsObjArrayList;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.transaction);

    save = (Button) findViewById(R.id.TbtnSave);
    newt = (Button) findViewById(R.id.btnNewTran);
    TransDate = (ImageButton) findViewById(R.id.transDate);
    Category = (Spinner) findViewById(R.id.Tcategory);
    Account = (Spinner) findViewById(R.id.TAccount);
    typerp = (Spinner) findViewById(R.id.TypeR);
    ItemName = (EditText) findViewById(R.id.TransItemName);
    Amount = (EditText) findViewById(R.id.TransAmount);
    Notes = (EditText) findViewById(R.id.tranNote);
    Balance = (EditText) findViewById(R.id.RetrieveBalance);
    Result = (EditText) findViewById(R.id.ResultBalance);
    tvDisplayDate = (TextView) findViewById(R.id.ttvDisplayDate);

    save.setOnClickListener(this);
    newt.setOnClickListener(this);
    setCurrentDateOnView();
    TransDate.setOnClickListener(this);

    TransactionDetailsObjArrayList = new ArrayList<TransactionDetails>();
    loadSpinnerData();

    Bundle takeBundledData = getIntent().getExtras();


    bundledBankName = takeBundledData.getString("clickedBankName");

    bundledBankBalance = takeBundledData.getString("clickedBankBalance");



    Account.setSelection(0);
    Balance.setText(bundledBankBalance);

}
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
Sheila Perumal
  • 115
  • 1
  • 2
  • 7

4 Answers4

24

Tested this just a while ago with a spinner that contains a list of strings, seems to work fine. Might help someone.

public void setSpinText(Spinner spin, String text)
{
    for(int i= 0; i < spin.getAdapter().getCount(); i++)
    {
        if(spin.getAdapter().getItem(i).toString().contains(text))
        {
            spin.setSelection(i);
        }
    }

}

:)

user3786552
  • 241
  • 2
  • 3
3

First SetAdapter of spinner using below code.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(PaymentTerms_NewInvoice_Activity.this, android.R.layout.simple_spinner_item, mTempArray);
mSpnTermsCode.setAdapter(adapter);

And Now Display First Item of Spinner as a Selected item using below code.

mSpnTermsCode.setSelection(0);
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
  • When i put sSpnTermsCode.setSelection(0) it display the first one. Wait! I have the data display in a list view,what i want is when i clicked on the first data on the list view, an intent open and it displays the value in a spinner. For the edit text(Balance) it works as shown above. Only for the spinner. – Sheila Perumal Oct 31 '12 at 05:53
  • @SheilaPerumal Save index value in one variable and pass into intent and then write mSpnTermsCode.setSelection(indexvalue); and if you have query regarding that then tell me. – Dipak Keshariya Oct 31 '12 at 05:55
  • I dont know how to do it. I thought it was simple like the editText :/ I have already take the value from the list view and pass it to the intent the only trouble is that im not able to display it in the spinner. I tried your code sSpnTermsCode.setSelection(1) and run the second one appears. – Sheila Perumal Oct 31 '12 at 06:06
  • @SheilaPerumal pass index on onitemclick event and pass that index instead of 1 and then try. – Dipak Keshariya Oct 31 '12 at 06:12
  • No, still having some trouble – Sheila Perumal Oct 31 '12 at 09:25
  • @SheilaPerumal what trouble tell me. – Dipak Keshariya Oct 31 '12 at 09:25
  • I dont know how to do it. @IGP Please Help public void onItemClick( AdapterView> arg0, View arg1, int arg2, long arg3) { Toast.makeText(getApplicationContext(), "Clicked on :" + arg2, Toast.LENGTH_SHORT).show(); Account.setSelection(arg2); – Sheila Perumal Oct 31 '12 at 23:59
  • `TransactionDetailsObjArrayList = new ArrayList(); loadSpinnerData(); Bundle takeBundledData = getIntent().getExtras(); bundledBankName = takeBundledData.getString("clickedBankName"); bundledBankBalance = takeBundledData.getString("clickedBankBalance"); Account.setSelection(0); Balance.setText(bundledBankBalance);` – Sheila Perumal Nov 02 '12 at 07:44
  • I have edit the code above. Im sorry, im still new on StackOverflow. The code i have posted is Transaction.java. I have AccountList.java which contain list view with Cash and Bank which i have input to the database and have been displayed in the listview.. When i clicked on BankAccount in the listview, trasaction intent open but the value selected on the Spinner is CASH. Its because : Account.setSelection(0); – Sheila Perumal Nov 02 '12 at 07:54
  • @SheilaPerumal Please post your full java code so i can understandyou problem. – Dipak Keshariya Nov 02 '12 at 08:12
  • @SheilaPerumal I don't understand, please give more information. – Dipak Keshariya Nov 03 '12 at 04:26
1

If you are using Kotlin, this is an extension fucntion to set the text of spinner based on user3786552's answer

fun Spinner.setSpinnerText(text: String) {
        for (i in 0 until this.adapter.count) {
            if (this.adapter.getItem(i).toString().contains(text)) {
                this.setSelection(i)
            }
        }
    }
Bolaji
  • 556
  • 1
  • 6
  • 11
0

For Spinner you have to set Adapter to bind your data

     Spinner Account;
     ArrayAdapter<String> productaddapter;
     List<String> productname=new ArrayList<String>();
     //Fill your data in productname arraylist..          

     productaddapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_dropdown_item, productname);

     Account.setAdapter(productaddapter);

in adapter you have to set yor list.

J.D.
  • 1,401
  • 1
  • 12
  • 21
  • Always try to give full answer.. what is mContext, productaddapter, productname??? – Niranj Patel Oct 31 '12 at 05:28
  • @SheilaPerumal then what you want to do with spinner?? – J.D. Oct 31 '12 at 05:41
  • @JigneshDer I have the data display in a list view,what i want is when i clicked on the first data on the list view, an intent open and it displays the value in a spinner. For the edit text(Balance) it works as shown above. Only for the spinner. I have edited my code above. Thanks – Sheila Perumal Oct 31 '12 at 05:55