我正在使用一个按钮,该按钮应该从相应的下拉列表中选择一个学期,并从firestore获取该特定学期的所有课程数据。然后,数据将显示在列表视图中。但是该按钮在第一次单击时不起作用。它可以在第二次点击。我该如何解决?
注意::我尝试了一些stackoverflow中可用的解决方案。那些对我没有用。
我的活动布局:只需考虑具有ID调节器的第一个微调器和第一个按钮
<?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:background="@drawable/app_back"
tools:context="bd.edu.bubt.regup.CompleteRegistrationActivity"
tools:showIn="@layout/app_bar_complete_registration">
<LinearLayout
android:layout_centerHorizontal="true"
android:layout_centerVertical="false"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="50dp"
android:id="@+id/linearLayout">
<Spinner
android:id="@+id/spinner_reguler"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="65dp"
android:layout_marginRight="80dp"/>
<Button
android:id="@+id/reguler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-50dp"
android:layout_marginLeft="250dp"
android:text="add"/>
<Spinner
android:id="@+id/spinner_major"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginRight="80dp"/>
<Button
android:id="@+id/major"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-50dp"
android:layout_marginLeft="250dp"
android:text="add"/>
<AutoCompleteTextView
android:id="@+id/autocomplete"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginRight="80dp"
android:hint="TYPE COURSE CODE - - -"
android:completionThreshold="1"
android:completionHint=" Select irregular course - - -"/>
<Button
android:id="@+id/irreguler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-50dp"
android:layout_marginLeft="250dp"
android:text="add"/>
<TextView
android:id="@+id/totalcourse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Total Course: "
android:textStyle="bold"
android:padding="5dp"
android:textSize="16dp"/>
<TextView
android:id="@+id/totalcredit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Total Credit: "
android:textStyle="bold"
android:padding="5dp"
android:textSize="16dp"/>
<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="-60dp"
android:layout_marginLeft="250dp"
android:text="Submit"/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:divider="#ffffff"
android:dividerHeight="2dp"/>
</LinearLayout>
</RelativeLayout>
我的活动Java类:检查调节器按钮的单击侦听器
package bd.edu.bubt.regup;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
public class CompleteRegistrationActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private Spinner sp1, sp2;
private AutoCompleteTextView autoComplete;
private Button reguler, major, irreguler, submit;
private TextView totalcourse, totalcredit;
private ListView listView;
private int i =0, j = 0;
private String[] reg = {"SELECT REGULER COURSE - - -", "1st semester", "2nd semester", "3rd semester", "4th semester",
"5th semester", "6th semester", "7th semester", "8th semester", "9th semester", "10th semester", "11th semester", "12th semester"};
private String[] maj = {"SELECT MAJOR - - -", "Artificial Intelligence", "Software", "Networking"};
ArrayList<CourseItem> adapterlist = new ArrayList<>();
public static ArrayList<String> finallist = new ArrayList<>();
public static ArrayList<String> codelist = new ArrayList<>();
public static ArrayList<String> namelist = new ArrayList<>();
public static ArrayList<String> creditlist = new ArrayList<>();
public static ArrayList<String> typelist = new ArrayList<>();
public static ArrayList<String> allcodelist = new ArrayList<>();
public static ArrayList<String> allnamelist = new ArrayList<>();
public static ArrayList<String> allcreditlist = new ArrayList<>();
private FirebaseFirestore firestore;
private FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_complete_registration);
sp1 = (Spinner) findViewById(R.id.spinner_reguler);
sp2 = (Spinner) findViewById(R.id.spinner_major);
autoComplete = (AutoCompleteTextView) findViewById(R.id.autocomplete);
reguler = (Button) findViewById(R.id.reguler);
major = (Button) findViewById(R.id.major);
irreguler = (Button) findViewById(R.id.irreguler);
totalcourse = (TextView) findViewById(R.id.totalcourse);
totalcredit = (TextView) findViewById(R.id.totalcredit);
submit = (Button) findViewById(R.id.submit);
listView = (ListView) findViewById(R.id.listview);
firestore = FirebaseFirestore.getInstance();
firebaseAuth = FirebaseAuth.getInstance();
sp2.setEnabled(false);
major.setEnabled(false);
final ArrayAdapter<String> ar = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,reg){
@Override
public boolean isEnabled(int position){
if(position == 0)
{
//Disable the first item of Spinner
return false;
}
else
{
return true;
}
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = super.getDropDownView(position, convertView, parent);
TextView tv = (TextView) view;
if(position == 0)
{
//Set the disabled item text color
tv.setTextColor(Color.GRAY);
}
else
{
tv.setTextColor(Color.BLACK);
}
return view;
}
};
ar.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(ar);
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(position > 10)
{
sp2.setEnabled(true);
major.setEnabled(true);
reguler.setEnabled(false);
final ArrayAdapter<String> ar = new ArrayAdapter<String>(CompleteRegistrationActivity.this, android.R.layout.simple_spinner_item,maj){
@Override
public boolean isEnabled(int position){
if(position == 0)
{
//Disable the first item of Spinner
return false;
}
else
{
return true;
}
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View view = super.getDropDownView(position, convertView, parent);
TextView tv = (TextView) view;
if(position == 0)
{
//Set the disabled item text color
tv.setTextColor(Color.GRAY);
}
else
{
tv.setTextColor(Color.BLACK);
}
return view;
}
};
ar.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(ar);
}
else if(position <= 10)
{
sp2.setEnabled(false);
major.setEnabled(false);
reguler.setEnabled(true );
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
reguler.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String position = Integer.toString(sp1.getSelectedItemPosition());
firestore.collection("CourseList").document("department").collection("CSE")
.document("semester").collection(position).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful())
{
for (DocumentSnapshot document : task.getResult()) {
final String s = document.getId();
firestore.collection("CourseList").document("department").collection("CSE")
.document("semester").collection(position).document(s).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful())
{
DocumentSnapshot documentSnapshot = task.getResult();
//code is already in the arraylist
if(codelist.indexOf(s) != -1)
{
//do nothing
}
//code is not in the arraylist
else
{
codelist.add(s);
namelist.add(documentSnapshot.getString("name"));
creditlist.add(documentSnapshot.getString("credit"));
typelist.add("Reguler");
}
}
else
{
Toast.makeText(getApplicationContext(), ""+task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
else
{
Toast.makeText(getApplicationContext(), ""+task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
//handle listview
double total_credit = 0.0;
adapterlist.clear();
int exceedflag = 0;
for(int i=0;i<codelist.size();i++)
{
total_credit = total_credit + Double.parseDouble(creditlist.get(i));
if(total_credit > 25.0)
{
exceedflag = 1;
total_credit = total_credit - Double.parseDouble(creditlist.get(i));
codelist.remove(i);
namelist.remove(i);
creditlist.remove(i);
typelist.remove(i);
}
else
{
adapterlist.add(new CourseItem(codelist.get(i), namelist.get(i), creditlist.get(i), typelist.get(i)));
}
}
if(exceedflag == 1)
{
Toast.makeText(getApplicationContext(), "Credit Exceeded!", Toast.LENGTH_SHORT).show();
}
StudentAdapter studentAdapter = new StudentAdapter(getApplicationContext(),R.layout.student_list_view_layout,adapterlist);
listView.setAdapter(studentAdapter);
totalcourse.setText("Total Course: " +adapterlist.size());
totalcredit.setText("Total Credit: " +total_credit);
}
});
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.menu_home) {
} else if (id == R.id.menu_bvault) {
} else if (id == R.id.menu_account) {
} else if (id == R.id.menu_pass_change) {
} else if (id == R.id.menu_logout) {
} else if (id == R.id.menu_about) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
答案 0 :(得分:0)
您需要在活动中改变重点。添加;
android:focusable =“ false” android:focusableInTouchMode =“ false”
单击xml文件上的按钮。