0

问题与描述的完全一样。我正在使用 SQL 数据库来保存两个字符串和一个 int。int 是微调器的位置。它似乎可以很好地保存和更新,但是当我返回页面查看它时,它会重置微调器。我知道它保存了正确的位置,因为我的列表视图显示了保存的位置。

{*旁注(可能不重要)有趣地更改一个特定的代码行会更改微调器重置的内容。如果我使用该方法

     trket.setSelection(codeIndex);

它将微调器设置在位置 3

但是,如果我使用代码

trket.setId(codeIndex);

它将微调器设置在位置 0}

现在我的代码。

这第一类是您查看/编辑/保存/删除条目的地方有很多额外的代码。对不起。它已经尽可能地修剪了。

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

    }

}

我确实相信这是唯一重要的课程,但是如果您可能还想看其他内容,请告诉我。我会一直在这里查看。

谢谢!

4

2 回答 2

1

内部saveContact方法

代替

trket.getLastVisiblePosition());

trket.getSelectedItemPosition();
于 2013-10-30T03:38:50.590 回答
0

因此,感谢来自 Google+ 的 Rob Myers,他帮助我弄清楚我没有得到结果并正确设置了 int。我在重新进入我正在调用的活动时设置字段的位置

 trket.setSelection(codeIndex);

当我需要打电话时

 trket.setSelection(result.getInt(codeIndex));
于 2013-10-30T20:02:31.430 回答