在微调器中设置文本(Set text on Spinner)

2019-08-02 03:32发布

这是AccountListView,它检索,我已经在数据库中添加列表视图显示数据,我添加了一些现金和银行账户,当我在名单上的现金点击查看打开事务的意图,其上有一个微调其中现金和银行已经加入,我希望它显示,我点击了列表视图中的数据。 请注意,对于现金和银行显示成功地只为微调的平衡。

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

}

当交易目的是打开它取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);

}

Answer 1:

测试这只是前一阵子与包含字符串列表的微调,似乎很好地工作。 可以帮助别人。

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

}

:)



Answer 2:

使用下面的代码微调的第一SetAdapter。

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

而现在显示微调的第一个项目如使用下面的代码选定的项目。

mSpnTermsCode.setSelection(0);


Answer 3:

对于微调,你必须设置适配器绑定您的数据

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

适配器必须设置侑列表。



Answer 4:

如果您使用的科特林,这是一个扩展温控功能设置微调的文本基础上user3786552的答案

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


文章来源: Set text on Spinner