2012-10-31 92 views
3

這是AccountListView,它檢索並顯示我已經添加到數據庫中的數據在列表視圖中,我添加了現金和銀行帳戶,當我點擊列表視圖中的現金時它打開交易意圖,其中有一個微調現金和銀行已被添加,我希望它顯示我點擊列表視圖的數據。請注意,現金和銀行顯示餘額只能用於微調。在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); 

} 

當交易的意圖是開放它採取使用以下代碼紡絲器的 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); 

} 
+2

首先了解微調http://developer.android.com/guide/topics/ui/controls/spinner.html –

+0

紗廠是由處理數據的列表/顯示什麼當前被選中。這與僅僅設置文本不同。 – Shellum

+0

在陣列的第一個「第0個」位置添加您的「標題」,並將該陣列設置爲您的微調器。 – MKJParekh

回答

0

首先SetAdapter的值。

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

現在使用下面的代碼顯示作爲選定項目的微調框的第一項。

mSpnTermsCode.setSelection(0); 
+0

當我把sSpnTermsCode.setSelection(0)它顯示第一個。等待!我有一個列表視圖中的數據顯示,我想要的是當我點擊列表視圖中的第一個數據時,意圖打開並顯示在微調框中的值。 對於編輯文本(餘額),其工作方式如上所示。只適用於微調。 –

+0

@SheilaPerumal將索引值保存在一個變量中並傳入意圖,然後寫入mSpnTermsCode.setSelection(indexvalue);如果你有疑問,然後告訴我。 –

+0

我不知道該怎麼做。我認爲這很簡單,就像editText:/我已經從列表視圖中獲取值並將其傳遞給intent,唯一的麻煩是我無法在spinner中顯示它。我試過你的代碼sSpnTermsCode.setSelection(1)並運行第二個出現。 –

0

對於微調,你必須設置適配器綁定您的數據

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

你必須設置侑列表適配器。

+0

總是試圖給出完整的答案..什麼是mContext,productaddapter,productname ??? –

+0

我已經設置adpater綁定數據 –

+0

@SheilaPerumal然後你想要做什麼與微調? –

11

不久前用包含字符串列表的微調器進行了測試,似乎工作正常。可能幫助某人。

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

} 

:)