0
好吧,我已经在这6小时了,由于某种原因,我只是不明白。点击按钮上添加时不显示视图?
我想使一个应用程序来计算使用某一公式的东西。
现在考虑它作为一个成绩点平均计算的东西。我有一个按钮,应该允许用户在布局中添加两个旋钮,从而进一步继续。现在我为此做了一个方法。当我第一次调用这个方法时,添加了spinners。但第二次不起作用。 如果我将它放在按钮onClick()方法中,它也不起作用。我已经通过网上发布的多个例子,并在stackoverflow,也许我犯了一些愚蠢的错误。请引导我通过这个。
下面是代码: -
SGPA.class
public class SGPA extends AppCompatActivity {
View v;
Context c;
LinearLayout subjectData;
ArrayList<Integer> creditIds;
ArrayList<Integer> gradeIds;
ArrayList<String> credits;
ArrayList<String> grades;
ArrayList<Integer> gradePoints;
//int id = View.generateViewId();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sgpa);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
credits=new ArrayList<>();
grades=new ArrayList<>();
gradePoints=new ArrayList<>();
//TODO if 2015 or 2013 regulations
//assuming 2015
fill2015();
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.activity_sgpa, null);
subjectData=(LinearLayout)v.findViewById(R.id.subjectdata);
c = this;
creditIds=new ArrayList<>();
gradeIds=new ArrayList<>();
Button button2=(Button)findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addViewForAnotherSubject();
setContentView(v);
}
});
setContentView(v);
}
void addViewForAnotherSubject()
{
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LinearLayout
.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
LinearLayout l=new LinearLayout(c);
l.setOrientation(LinearLayout.HORIZONTAL);
l.setLayoutParams(lp);
l.setGravity(Gravity.CENTER_HORIZONTAL);
Spinner creditspinner=new Spinner(c);
Spinner gradespinner=new Spinner(c);
ArrayAdapter<String> creditSpinnerArrayAdapter = new ArrayAdapter<>
(this,
android.R.layout.simple_spinner_dropdown_item, credits);
creditspinner.setAdapter(creditSpinnerArrayAdapter);
ArrayAdapter<String> gradeSpinnerArrayAdapter = new ArrayAdapter<>
(this,
android.R.layout.simple_spinner_dropdown_item, grades);
gradespinner.setAdapter(gradeSpinnerArrayAdapter);
l.addView(creditspinner);
l.addView(gradespinner);
subjectData.addView(l);
}
void fill2015()
{
grades.add("O"); grades.add("A+");
grades.add("A"); grades.add("B+");
grades.add("B"); grades.add("C");
grades.add("P"); grades.add("F");
grades.add("Ab"); grades.add("I");
gradePoints.add(10); gradePoints.add(9);
gradePoints.add(8); gradePoints.add(7);
gradePoints.add(6); gradePoints.add(5);
gradePoints.add(4); gradePoints.add(0);
gradePoints.add(0); gradePoints.add(0);
for(int i=1;i<=10;i++)
credits.add(""+i);
}
void fill2013()
{
}
}
activity_sgpa.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="innominatebit.srmite.SGPA">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_sgpa" />
</android.support.design.widget.CoordinatorLayout>
content_sgpa.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="6dp"
android:paddingEnd="6dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:paddingStart="6dp"
android:paddingTop="6dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="innominatebit.srmite.SGPA"
tools:showIn="@layout/activity_sgpa">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/subjectdata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center_horizontal"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button" />
</RelativeLayout>
所有帮助表示赞赏。提前致谢!
第二次它再次添加相同的视图..尝试删除已添加的视图,并再次添加它..(我不知道我是否真的理解你) – Pzy64
尝试注释代码,直到你减少几行问题。 –
@Pzy64不,不添加相同的视图。新创建并添加。由于'sbujectData'是一个线性布局,它应该在下面添加,但由于某种原因,它不会。 –