我有一个标签式活动,有3个标签。这是它的主要活动:
public class TabbedActivity2 extends AppCompatActivity {
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbed2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setTitle("Vocabulary Paractices");
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_tabbed_activity2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(getArguments().getInt(ARG_SECTION_NUMBER)==1)
{
View rootView = inflater.inflate(R.layout.fragment_level1fragment, container, false);
return rootView;
}
else if (getArguments().getInt(ARG_SECTION_NUMBER)==2)
{
View rootView = inflater.inflate(R.layout.fragment_level2fragment, container, false);
return rootView;
}
else
{
View rootView = inflater.inflate(R.layout.fragment_level3fragment, container, false);
return rootView;
}
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
level1fragment tab1= new level1fragment();
return tab1;
case 1:
level2fragment tab2= new level2fragment();
return tab2;
case 2:
level3fragment tab3= new level3fragment();
return tab3;
default:
return null;
}
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Level 1";
case 1:
return "Level 2";
case 2:
return "Level 3";
default:
return null;
}
}
}
}
其中一个片段:
public class level1fragment extends Fragment {
private Random randomGenerator;
private TextView translation;
private TextView idofcaps;
private EditText useranswer;
private CheckBox checkbox;
private ImageView caps,nocheckbox;
private Button controlanswer;
private DatabaseReference myDatabase;
private FirebaseAuth mAut;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public level1fragment() {
}
public static level1fragment newInstance(String param1, String param2) {
level1fragment fragment = new level1fragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_level1fragment, container, false);
mAut= FirebaseAuth.getInstance();
caps=(ImageView)view.findViewById(R.id.capslevel1);
translation=(TextView)view.findViewById(R.id.translationlevel1);
idofcaps=(TextView)view.findViewById(R.id.idofcapslevel1);
useranswer=(EditText)view.findViewById(R.id.useranswerlevel1);
controlanswer=(Button)view.findViewById(R.id.controlbuttonlevel1);
nocheckbox=(ImageView)view.findViewById(R.id.nocheckboxlevel1);
String user_id = mAut.getCurrentUser().getUid();
myDatabase= FirebaseDatabase.getInstance().getReference();
final DatabaseReference current_user_db = myDatabase.child("Users").child(user_id);
final DatabaseReference myDataSelect = myDatabase.child("Caps").child("Level 1").child("Questions");
final ProgressDialog myProgress = ProgressDialog.show(getActivity(),"Wait","Test is preparing",true);
myDataSelect.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final List<String> lstd = new ArrayList<String>(); // Result will be holded Here
for (DataSnapshot ds : dataSnapshot.getChildren()) {
lstd.add(String.valueOf(ds.getKey()));
DatabaseReference solved= current_user_db.child("Caps").child("Level 1").child("Solved");
solved.addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final List<String> solved = new ArrayList<String>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
solved.add(String.valueOf(ds.getValue()));
lstd.removeAll(solved);
if (lstd.isEmpty()) {
ViewPager contain= (ViewPager)getActivity().findViewById(R.id.container);
contain.setCurrentItem(1);
}
else {
randomGenerator = new Random();
int index = randomGenerator.nextInt(lstd.size());
String item = lstd.get(index);
idofcaps.setText(item);
final DatabaseReference capsSelect = myDataSelect.child(item);
capsSelect.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dss) {
String post_Image = (String) dss.child("Image").getValue(); Picasso.with(getActivity()).load(post_Image).into(caps);
String translation_caps = (String) dss.child("Translation").getValue();
translation.setText(translation_caps);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
myProgress.dismiss();
}}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
controlanswer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String ua= useranswer.getText().toString().trim();
String aa= translation.getText().toString();
if (ua.equalsIgnoreCase(aa)) {
useranswer.setText("");
String b=idofcaps.getText().toString();
final DatabaseReference solved = current_user_db.child("Caps").child("Level 1").child("Solved").push();
solved.setValue(b);
myDataSelect.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final List<String> lst = new ArrayList<String>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
lst.add(String.valueOf(ds.getKey()));
solved.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final List<String> solved = new ArrayList<String>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
solved.add(String.valueOf(ds.getValue()));
lst.removeAll(solved);
if(lst.isEmpty())
{
}
else {
randomGenerator = new Random();
int index = randomGenerator.nextInt(lst.size());
String item = lst.get(index);
idofcaps.setText(item);
final DatabaseReference capsSelect = myDataSelect.child(item);
capsSelect.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dss) {
String post_Image = (String) dss.child("Image").getValue();
Picasso.with(getActivity()).load(post_Image).into(caps);
String post_name = (String) dss.child("Translation").getValue();
translation.setText(post_name);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
nocheckbox.setVisibility(View.INVISIBLE);
}
else
{
nocheckbox.setVisibility(View.VISIBLE);
}
}
});
return view;
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
我知道这太过分了。我有很多问题,这就是我添加它们的原因。我是Android新手。在这部分,我正在尝试做一种测试/测验应用程序。我将在开始时有3个级别。我想在选项卡中显示它们,但我不想在开始时访问所有选项卡。如果tab1中的问题解决正确,我想打开,tab2打开。我正在从firebase中检索问题并将它们存储在列表中。如果lst.isEmpty()部分如下,我尝试使用put Alert Dialog:
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Congratulations!");
builder.setMessage("You finished Level 1. Do you want to start Level 2?");
builder.setNegativeButton("Not now", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id) {
}
});
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
ViewPager contain= (ViewPager)getActivity().findViewById(R.id.container);
contain.setCurrentItem(1);
dialog.dissmiss();
}
});
然而,警报对话框并没有消失。传递tab2是关键但在屏幕上对话框永远不会关闭。我尝试过Toast Message。它有同样的问题。大约十次消息显示然后消失。即使我关闭手机屏幕上的应用程序消息也不会关闭。我哪里错了?除此之外,甚至tab1和tab2问题都完成了这个toast消息或警告对话框从tab1中显示之后的tab1开始。我怎么能只让它们出现一次?我的意思是当用户点击其中一个按钮时,它再也不会出现。此外,进度对话框也永远不会消失。除了这些之外,在一些刷卡工具栏之后就会消失。并且不明白为什么以及如何防止它。