0

我正在尝试构建一个类似于计算自行车齿轮(iOS 版本)的 Android 应用程序,将磁带存储在数据库中。当用户用每个链环的齿数填写 editText 时,我想在下一个进行计算的活动中写入这些值,但我得到的只是一个奇怪的行为: - 如果用户选择两个链环,第二个value 没有写在信息 textView 上,但它出现在列上的 textview 中,以显示对应于第三个链环的结果, - 如果用户选择三个链环,则第二个和第三个值以相反的顺序放置,都在信息 textView 和标题中。

一些图片来说明

用户填空

两个链轮值,第二个未显示

三、最后两个倒置位置

我的一段代码:

活动一

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();
    fillSpinner();

        button1.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v){


chainring1editText.setBackgroundColor(getResources().getColor(android.R.color.background_light));
chainring2editText.setBackgroundColor(getResources().getColor(android.R.color.black));
chainring3editText.setBackgroundColor(getResources().getColor(android.R.color.black));
        chainringCount = "1";
    }
});
button2.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v){

chainring1editText.setBackgroundColor(getResources().getColor(android.R.color.background_light));   
chainring2editText.setBackgroundColor(getResources().getColor(android.R.color.background_light));       
chainring3editText.setBackgroundColor(getResources().getColor(android.R.color.black));
        chainringCount = "2";
            }
    });
    button3.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v){


chainring1editText.setBackgroundColor(getResources().getColor(android.R.color.background_light));
chainring2editText.setBackgroundColor(getResources().getColor(android.R.color.background_light));
chainring3editText.setBackgroundColor(getResources().getColor(android.R.color.background_light));
        chainringCount= "3";
    }

});

//Check if database is empty
if (spinner.getAdapter().getCount()==0){
    Intent intent =new Intent (this,AddCassettes.class);
    startActivity(intent);
   }

}
private void init() {
    chainring1editText = (EditText)findViewById(R.id.chainring1editText);
    chainring2editText = (EditText)findViewById(R.id.chainring2editText);
    chainring3editText = (EditText)findViewById(R.id.chainring3editText);

    //force user to select number of chainrings


chainring1editText.setBackgroundColor(getResources().getColor(android.R.color.black));


chainring2editText.setBackgroundColor(getResources().getColor(android.R.color.black));


chainring3editText.setBackgroundColor(getResources().getColor(android.R.color.black));

    button1 = (Button)findViewById(R.id.btnBack);
    button2 = (Button)findViewById(R.id.btnReset);
    button3 = (Button)findViewById(R.id.btnSave);
    spinner = (Spinner)findViewById(R.id.picker);
    gearInches = (ToggleButton)findViewById(R.id.inches);
    rgWheelSize = (RadioGroup)findViewById(R.id.rgWheelsize);
    tvWheelsize = (TextView)findViewById(R.id.tvWheelsize);
}
@Override
protected void onResume(){
    super.onResume();
    fillSpinner();
}
@SuppressWarnings("deprecation")
private void fillSpinner(){
    DataBaseHelper databasehelper = new DataBaseHelper(this);
    SQLiteDatabase db = databasehelper.getReadableDatabase();

    Spinner picker = (Spinner)this.findViewById(R.id.picker);
    Cursor cursor = db.rawQuery ("SELECT _id AS _id, dmodel FROM cass",null);
    String[] from = new String[] {"dmodel"};
    int[] to = new int[] {android.R.id.text1};
    mAdapter = new 
    SimpleCursorAdapter(this,android.R.layout.simple_spinner_dropdown_item,cursor,from,to);
    picker.setAdapter(mAdapter);
    mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    picker.setAdapter(mAdapter);
    db.close();

}


//private String[] getItems(){
public void getData(){

    // retrieve selected cassette to calculate gears

    Spinner spinner = (Spinner)this.findViewById(R.id.picker);
    selectedCassette = ((Cursor) spinner.getSelectedItem()).getString(1);


    //retrieve teeth 
    teethOne= this.chainring1editText.getText().toString();
    teethTwo =this.chainring2editText.getText().toString();
    teethThree =this.chainring3editText.getText().toString();

    //retrieve wheelsize from radio button
    switch (rgWheelSize.getCheckedRadioButtonId())
    {
    case R.id.r26inches:
        wheelsize = "2055";
        tvWheelsize.setText(wheelsize + "mm");
        break;
    case R.id.r275inches:
        wheelsize= "2170";
        tvWheelsize.setText(wheelsize + "mm");
        break;
    case R.id.r29inches:
        wheelsize = "2285";
        tvWheelsize.setText(wheelsize + "mm");
        break;
    case R.id.rRoad:
        wheelsize = "2097";
        tvWheelsize.setText(wheelsize + "mm");
        break;
    }
    //get the state from the toggle button
    if (gearInches.isChecked()){
        inches = "yes";
        }else{
            inches = "no";
        }
}

//MENU
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {

        case R.id.addCassette:
           Intent intent =new Intent (this,AddCassettes.class);
           startActivity(intent);
            return true;

        case R.id.deleteCassette:
            Intent intentTwo = new Intent (this,EditCassettes.class);
            startActivity(intentTwo);
            return true;

        case R.id.calculateCombinations:
            Intent intentThree = new Intent (this,Calculate.class);
            getData();


            Bundle b = new Bundle();
            b.putString("C",selectedCassette);
            b.putString("CC",chainringCount);
            b.putString("TO", teethOne);
            b.putString("TT", teethThree);
            b.putString("TH", teethTwo);
            b.putString("WH", wheelsize);
            b.putString("IN", inches);
            b.putString("CC", chainringCount);

            intentThree.putExtras(b);
            startActivity(intentThree);
                return true;

             default:
                return super.onOptionsItemSelected(item);
        }
    }

Calculate.java(活动 2)

public class Calculate extends Activity{

private TextView banner;
String bannerText, selCassette,chainringOne, chainringTwo, chainringThree,chainringCount;
int chainrings = 0;
...
TextView[] tvChainringArray = new TextView[3];



    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calculate);
    init();

    Bundle bundle = getIntent().getExtras();
    selCassette = bundle.getString("C");
    chainringOne = bundle.getString("TO");
    chainringTwo = bundle.getString("TT");
    chainringThree = bundle.getString("TH");
    chainringCount = bundle.getString("CC");
    wheelsize = bundle.getString("WH");
    gearInches = bundle.getString("IN");

    if(Integer.parseInt(chainringCount) == 1){
        bannerText = selCassette + " * " + chainringOne;
        banner.setText(bannerText);
    }else if (Integer.parseInt(chainringCount)== 2){
        bannerText = selCassette + " * " +chainringOne + "/" + chainringTwo;
        banner.setText(bannerText);
    }else if(Integer.parseInt(chainringCount) == 3){
        bannerText = selCassette + " * " + chainringOne + "/" + chainringTwo + "/" +    chainringThree;
        banner.setText(bannerText);
    }

    //call method to retrieve data from database
    queryDB("selCassette");

    //chainring teeth above gears
    tvChainringArray[0].setText(chainringOne);
    tvChainringArray[1].setText(chainringTwo);
    tvChainringArray[2].setText(chainringThree);

        }

     public void init(){
     banner = (TextView)findViewById(R.id.bigBanner);
     tvChainringArray[0] = (TextView)findViewById(R.id.tvCh1);
     tvChainringArray[1] = (TextView)findViewById(R.id.tvCh2);
     tvChainringArray[2] = (TextView)findViewById(R.id.tvCh3);
    }
... 

计算.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000" 
android:orientation="vertical">


<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="12dp"
    android:background="#C0C0C0"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/bigBanner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="Helvetica"
        android:labelFor="11"
        android:padding="5dp"
        android:text="@string/largeText"
        android:textAlignment="textStart"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="18sp" />

 </LinearLayout>

 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"  >

    <TextView
      android:id="@+id/textView2" // blank textView to keep tvCh1,2 & in place
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/mediumText"
      android:layout_marginBottom="15dp"
      android:textAppearance="?android:attr/textAppearanceMedium" />

  <TextView
     android:id="@+id/tvCh1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:labelFor="80"
     android:text="@string/smallText"
     android:textAlignment="center"
     android:textAppearance="?android:attr/textAppearanceSmall"
     android:width="60dp" />

<TextView
     android:id="@+id/tvCh2"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:labelFor="81"
     android:text="@string/smallText"
     android:textAlignment="center"
     android:textAppearance="?android:attr/textAppearanceSmall"
     android:width="60dp" />

<TextView
     android:id="@+id/tvCh3"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:labelFor="82"
     android:text="@string/smallText"
     android:textAlignment="center"
     android:textAppearance="?android:attr/textAppearanceSmall"
     android:width="60dp" />

</LinearLayout>

...

你能给出解决这种情况的提示吗?

4

1 回答 1

0

我的错,我在捆绑设置中交换了变量顺序。

Bundle b = new Bundle();
b.putString("C",selectedCassette);
b.putString("CC",chainringCount);
b.putString("TO", teethOne);
b.putString("TT", teethTwo);
b.putString("TH", teethThree);
...
intentThree.putExtras(b);
startActivity(intentThree);
于 2013-04-01T10:57:05.300 回答