我打算检查纺纱厂,以确保他们没有选择相同的商品。
我的代码
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/StudentName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please select Course and Grade"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >
<Spinner
android:id="@+id/SpinnerC1"
android:layout_width="221dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:entries="@array/course"/>
<Spinner
android:id="@+id/SpinnerG1"
android:layout_width="83dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:entries="@array/grade" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >
<Spinner
android:id="@+id/SpinnerC2"
android:layout_width="221dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:entries="@array/course"/>
<Spinner
android:id="@+id/SpinnerG2"
android:layout_width="83dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:entries="@array/grade"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >
<Spinner
android:id="@+id/SpinnerC3"
android:layout_width="221dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:entries="@array/course" />
<Spinner
android:id="@+id/SpinnerG3"
android:layout_width="83dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:entries="@array/grade"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1" >
<Spinner
android:id="@+id/SpinnerC4"
android:layout_width="221dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:entries="@array/course"/>
<Spinner
android:id="@+id/SpinnerG4"
android:layout_width="83dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:entries="@array/grade"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom|center"
android:weightSum="1"
>
<Spinner
android:id="@+id/SpinnerC5"
android:layout_width="221dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:entries="@array/course"/>
<Spinner
android:id="@+id/SpinnerG5"
android:layout_width="83dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:entries="@array/grade"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:orientation="horizontal" >
<Spinner
android:id="@+id/SpinnerC6"
android:layout_width="221dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:entries="@array/course"/>
<Spinner
android:id="@+id/SpinnerG6"
android:layout_width="83dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:entries="@array/grade"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate GPA"
android:onClick="calculateGPA" />
</LinearLayout>
</LinearLayout>
然后我的这个页面的整个java代码将是
package com.example.gpapredict;
import java.text.DecimalFormat;
import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class Selection extends ActionBarActivity {
String stuName,stuId, gpaScore, defCourse,gradeA,gradeB,gradeC,gradeD,gradeF,gradeG;
ArrayList<String> check;
double cor,score,gpa=999;
TextView tvName;
Intent i2;
Spinner SpinnerC1, SpinnerC2, SpinnerC3, SpinnerC4, SpinnerC5, SpinnerC6, SpinnerG1, SpinnerG2, SpinnerG3, SpinnerG4, SpinnerG5, SpinnerG6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selection);
stuName = getIntent().getStringExtra("name");
stuId = getIntent().getStringExtra("stuId");
tvName = (TextView)findViewById(R.id.StudentName);
tvName.setText("Hi," + stuName);
}
public void calculateGPA(View v){
//retrieve data from spinners
SpinnerC1 = (Spinner)findViewById(R.id.SpinnerC1);
SpinnerC2 = (Spinner)findViewById(R.id.SpinnerC2);
SpinnerC3 = (Spinner)findViewById(R.id.SpinnerC3);
SpinnerC4 = (Spinner)findViewById(R.id.SpinnerC4);
SpinnerC5 = (Spinner)findViewById(R.id.SpinnerC5);
SpinnerC6 = (Spinner)findViewById(R.id.SpinnerC6);
SpinnerG1 = (Spinner)findViewById(R.id.SpinnerG1);
SpinnerG2 = (Spinner)findViewById(R.id.SpinnerG2);
SpinnerG3 = (Spinner)findViewById(R.id.SpinnerG3);
SpinnerG4 = (Spinner)findViewById(R.id.SpinnerG4);
SpinnerG5 = (Spinner)findViewById(R.id.SpinnerG5);
SpinnerG6 = (Spinner)findViewById(R.id.SpinnerG6);
//save the data into string type
String[] course = new String[6];
String[] grade = new String[6];
course[0] = SpinnerC1.getSelectedItem().toString();
course[1] = SpinnerC2.getSelectedItem().toString();
course[2] = SpinnerC3.getSelectedItem().toString();
course[3] = SpinnerC4.getSelectedItem().toString();
course[4] = SpinnerC5.getSelectedItem().toString();
course[5] = SpinnerC6.getSelectedItem().toString();
grade[0] = SpinnerG1.getSelectedItem().toString();
grade[1] = SpinnerG2.getSelectedItem().toString();
grade[2] = SpinnerG3.getSelectedItem().toString();
grade[3] = SpinnerG4.getSelectedItem().toString();
grade[4] = SpinnerG5.getSelectedItem().toString();
grade[5] = SpinnerG6.getSelectedItem().toString();
//check for no same course
defCourse = "Select Course";
check = new ArrayList<String>();
check.add(course[0]);
for (String checkCourse:check ){
for (int c=1;c<6;c++){
if (checkCourse.equals(course[1])){
Toast.makeText(getApplicationContext(), "Cannot Proceed : Same course is selected", Toast.LENGTH_LONG).show();
return;
}
else if(course[c].equals(defCourse)) {
continue;
}else {
check.add(course[c]);
break;
}
}
}
//calculate score
gradeA="A";
gradeB="B";
gradeC="C";
gradeD="D";
gradeF="F";
gradeG="G";
//initialize value
score = 0.0;
cor=0.0;
for (int i=0;i<6;i++){
if(grade[i].equals(gradeA)){
score = score + 4.0;
cor++;
}else
if (grade[i].equals(gradeB)){
score = score + 3.0;
cor++;
}else
if (grade[i].equals(gradeC)){
score = score + 2.0;
cor++;
}else
if (grade[i].equals(gradeD)){
score = score + 1.0;
cor++;
}else
if (grade[i].equals(gradeF)){
score = score+0.0;
cor++;
}else{
score = score +0.0;
}
}
if (cor == 0.0){
cor++;
}
gpa = score/cor;
//set precision to 2 decimal places
DecimalFormat dec = new DecimalFormat("#0.00");
gpaScore = String.valueOf(dec.format(gpa));
Toast.makeText(getApplicationContext(), "okay", Toast.LENGTH_LONG).show();
i2 = new Intent(Selection.this,Result.class);
i2.putExtra("stuName",stuName);
i2.putExtra("stuId",stuId);
i2.putExtra("courseArray", course);
i2.putExtra("gradeArray", grade);
i2.putExtra("gpa", gpaScore);
startActivity(i2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.selection, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
由于我可以发布的链接限制,我会在另一张图片上显示我的界面和错误消息。
现在我的应用程序能够在将其他微调器的值与第一个微调器的值进行比较时显示用户错误。因为我的应用程序发现第一个和第二个微调器正在选择相同的过程。它如图所示。
如果第二个微调器的值与第一个微调器的值不同但与第三个微调器的值相同,则应用程序允许进入下一页。这不是我想要的。我希望我的应用程序确保用户不要从微调器中选择相同的主题。
此图显示用户在第二个微调器和第三个微调器上选择相同路线的结果。我不希望它。
Result of 2nd spinner and third spinner have the same value.
再次感谢你的帮助,如果我的解释仍然不够清楚,我想解释更多细节。
答案 0 :(得分:0)
在我看来,点击“继续”按钮,你应该有一个选定值spinners
的arraylist,然后检查arraylist是否有重复值。请尝试以下代码:
//check for no same course
defCourse = "Select Course";
ArrayList check = new ArrayList<String>();
check.add(defCourse);
for (int c=0;c<6;c++){
if (!check.contains(course[c])) {
check.add(course[c]);
}else{
Toast.makeText(getApplicationContext(), "Cannot Proceed : Same course is selected", Toast.LENGTH_LONG).show();
}
}
答案 1 :(得分:0)
我找到了解决方案
// check for no same course
defCourse = "Select course";
check = new ArrayList<String>();
check.add(course[0]);
for (int c = 1; c < 6; c++) {
if (check.contains(course[c])) {
Toast.makeText(getApplicationContext(),
"Cannot Proceed : Same course is selected",
Toast.LENGTH_LONG).show();
return;
} else if (course[c].equals(defCourse)) {
continue;
} else {
check.add(course[c]);
}
}
谢谢你的帮助:)