2013-10-30 92 views
0

問題與上述內容完全相同。我正在使用SQL數據庫來保存兩個字符串和一個int。整數是微調位置。它似乎保存並更新得很好,但當我返回頁面查看它時,它會重置微調器。我知道這是保存正確的位置,因爲我有我的列表視圖顯示保存的位置。似乎無法正確獲取SQL數據庫以正確設置Spinner

{*附註(可能不重要)有趣的是改變編碼一個特定的行更改什麼微調重置。如果我使用的方法

 trket.setSelection(codeIndex); 

它在位置設置旋轉器3

然而如果我使用

trket.setId(codeIndex); 

它在位置0}

設置微調現在的代碼我的代碼。

這第一類是查看/編輯/保存/刪除條目 的Theres很多額外的代碼。抱歉。它儘可能多地被修飾。

public class TestEdit extends Activity implements OnClickListener,OnItemSelectedListener{ 

private long rowID; 
private EditText caret,lapet; 
private Spinner trket; 
String a,b,c,message,pps,tys,rh1s,rh2s,sr1s,sr2s,de1s,de2s,dc1s,dc2s,ar1s,ar2s,c1s,c2s,t1s,t2s,bb1s,bb2s,li1s,li2s,la1s,la2s,lb1s,lb2s,df1s,df2s,g1s,g2s,g3s,g4s,g5s,g6s,g7s,gfs,tss,hps,tqs,lbs,mils,rpms,partss,commentss; 
String[] paths = { "six","five", "four", "three", "two", "one"}; 
Button paste,sa,sh,cb; 
int position; 


    @Override 
    public void onCreate(Bundle savedInstanceState) 


    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.test_tunepage); 
     caret = (EditText) findViewById(R.id.editText1); 
     lapet = (EditText) findViewById(R.id.editText2); 
     trket = (Spinner) findViewById(R.id.spinner1); 


     sa = (Button)findViewById(R.id.delete); 

     sa.setOnClickListener(this); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, paths 
       ); 
     trket.setAdapter(adapter); 
     trket.setOnItemSelectedListener(this); 





     Bundle extras = getIntent().getExtras(); 


       if (extras != null) 
     { 
     rowID = extras.getLong("row_id"); 
     caret.setText(extras.getString("car")); 
     lapet.setText(extras.getString("lap")); 
     trket.setSelection(position); 


     } 

     Button saveButton =(Button) findViewById(R.id.save); 
     saveButton.setOnClickListener(new OnClickListener() { 

      @Override 
     public void onClick(View v) 
      { 
       if (caret.getText().length() != 0) 
       { 
        AsyncTask<Object, Object, Object> saveContactTask = 
         new AsyncTask<Object, Object, Object>() 
         { 
          @Override 
          protected Object doInBackground(Object... params) 
          { 
          saveContact(); 
          return null; 
          } 

          @Override 
          protected void onPostExecute(Object result) 
          { 
          finish(); 
          } 
         }; 

        saveContactTask.execute((Object[]) null); 
       } 

       else 
       { 
        AlertDialog.Builder alert = new AlertDialog.Builder(TestEdit.this); 
        alert.setTitle(R.string.errorTitle); 
        alert.setMessage(R.string.errorMessage); 
        alert.setPositiveButton(R.string.errorButton, null); 
        alert.show(); 
       } 
       } 
     }); 
     } 
    private void deleteContact() 
    { 

     AlertDialog.Builder alertt = new AlertDialog.Builder(TestEdit.this); 

     alertt.setTitle(R.string.confirmTitle); 
     alertt.setMessage(R.string.confirmMessage); 

     alertt.setPositiveButton(R.string.delete_btn, 
     new DialogInterface.OnClickListener() 
     { 
      @Override 
      public void onClick(DialogInterface dialog, int button) 
      { 
       final TestDbCon dbConnector = 
        new TestDbCon(TestEdit.this); 

       AsyncTask<Long, Object, Object> deleteTask = 
        new AsyncTask<Long, Object, Object>() 
        { 
        @Override 
        protected Object doInBackground(Long... params) 
        { 
         dbConnector.deleteContact(params[0]); 
         return null; 
        } 

        @Override 
        protected void onPostExecute(Object result) 
        { 
         finish(); 
        } 
        }; 

       deleteTask.execute(new Long[] { rowID });    
      } 
     } 
    ); 
     alertt.setNegativeButton(R.string.cancel_btn, null).show(); 

    } 

    @Override 
    protected void onResume() 
    { 
     super.onResume(); 
     new LoadContacts().execute(rowID); 
    } 

    private class LoadContacts extends AsyncTask<Long, Object, Cursor> 
    { 
     TestDbCon dbConnector = new TestDbCon(TestEdit.this); 

     @Override 
     protected Cursor doInBackground(Long... params) 
     { 
     try { 
      dbConnector.open(); 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return dbConnector.getOneContact(params[0]); 
     } 

     @Override 
     protected void onPostExecute(Cursor result) 
     { 
     super.onPostExecute(result); 
     if (result != null && result.moveToFirst()) { 

     // get the column index for each data item 
     int nameIndex = result.getColumnIndex("car"); 
     int capIndex = result.getColumnIndex("lap"); 
     int codeIndex = result.getColumnIndex("trk"); 




     lapet.setText(result.getString(capIndex)); 
     //trket.setId(codeIndex); 
     trket.setSelection(codeIndex); 

     caret.setText(result.getString(nameIndex)); 
     } 

     result.close(); 
     dbConnector.close(); 
     } 
    } 

    private void saveContact() 
     { 
      TestDbCon dbConnector = new TestDbCon(this); 

      if (getIntent().getExtras() == null) 
      { 
       dbConnector.insertContact(
         caret.getText().toString(),//1 
         lapet.getText().toString(), 
         trket.getLastVisiblePosition()); 

      } 
      else 
      { 
      dbConnector.updateContact(rowID, 
       caret.getText().toString(), 
       lapet.getText().toString(), 
       trket.getLastVisiblePosition()); 

    } 


    } 

    @Override 
    public void onClick(View v) { 
     switch(v.getId()){ 

     case R.id.delete: 
       deleteContact(); 
       break;} 


      } 



    private void getstrings() { 
     // TODO Auto-generated method stub 
     a=caret.getText().toString(); 
     b=lapet.getText().toString(); 

    } 
    @Override 
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
     position = trket.getSelectedItemPosition(); 

    } 
    @Override 
    public void onNothingSelected(AdapterView<?> arg0) { 
     // TODO Auto-generated method stub 

    } 

}

我相信這是唯一重要的一類,但是如果那裏有別的東西,你可能想看到的只是讓我知道。我會一直在這裏檢查。

謝謝!

回答

1

saveContact方法

更換

trket.getLastVisiblePosition()); 

trket.getSelectedItemPosition(); 
+0

謝謝你的回覆!一旦我更改該代碼,沒有任何更改。它仍然重置 – TheRedStig

0

所以感謝Rob邁爾斯來自Google+誰幫我弄清楚,我沒有得到的結果和設置int正確。我在那裏時再入設置字段到活動我打電話

trket.setSelection(codeIndex); 

,當我需要被調用

trket.setSelection(result.getInt(codeIndex));