如何在单击导航抽屉中的项目时使用单个活动导航到不同的数据库?

时间:2018-08-24 13:10:01

标签: java android

我已经创建了多个概念活动供不同的数据库访问,以减少我需要一个解决方案来解决创建多个活动到单个活动的问题?

IN ConceptActivity.Java

package com.example.akshi.ictlearningengeducation;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.content.Intent;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.akshi.ictlearningengeducation.db.DBAdapter;
import com.example.akshi.ictlearningengeducation.model.Question;    

import java.util.ArrayList;
import java.util.List;    

public class ConceptActivity extends AppCompatActivity {

    private List<Question> questionsList;
    private Question currentQuestion;

    private TextView txtQuestion,tvNoOfQs;
    private RadioButton rbtnA, rbtnB, rbtnC,rbtnD;
    private Button btnNext;

    private int obtainedScore=0;
    private int questionId=0;

    private int answeredQsNo=0;

    ArrayList<String> myAnsList;

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

        //Initialize the view
        init();

        //Initialize the database
        final DBAdapter dbAdapter=new DBAdapter(this);
        questionsList= dbAdapter.getAllQuestions();
        currentQuestion=questionsList.get(questionId);

        //Set question
        setQuestionsView();

        //Check and Next
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
                RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());

                Log.e("Answer ID", "Selected Positioned  value - "+grp.getCheckedRadioButtonId());

                if(answer!=null){
                    Log.e("Answer", currentQuestion.getANSWER() + " -- " + answer.getText());
                    //Add answer to the list
                    myAnsList.add(""+answer.getText());

                    if(currentQuestion.getANSWER().equals(answer.getText())){
                        obtainedScore++;
                        Log.e("comments", "Correct Answer");
                        Log.d("score", "Obtained score " + obtainedScore);
                    }else{
                        Log.e("comments", "Wrong Answer");
                    }
                    if(questionId<dbAdapter.rowCount()){
                        currentQuestion=questionsList.get(questionId);
                        setQuestionsView();
                    }else{
                        Intent intent = new Intent(ConceptActivity.this, ResultActivity.class);

                        Bundle b = new Bundle();
                        b.putInt("score", obtainedScore);
                        b.putInt("totalQs", questionsList.size());
                        b.putStringArrayList("myAnsList", myAnsList);
                        intent.putExtras(b);
                        startActivity(intent);
                        finish();

                    }

                }else{
                    Log.e("comments", "No Answer");
                }

                //Need to clear the checked item id
                grp.clearCheck();


            }//end onClick Method
        });


    }

    public void init(){
        tvNoOfQs=(TextView)findViewById(R.id.tvNumberOfQuestions);
        txtQuestion=(TextView)findViewById(R.id.tvQuestion);
        rbtnA=(RadioButton)findViewById(R.id.radio0);
        rbtnB=(RadioButton)findViewById(R.id.radio1);
        rbtnC=(RadioButton)findViewById(R.id.radio2);
        rbtnD=(RadioButton)findViewById(R.id.radio3);

        btnNext=(Button)findViewById(R.id.btnNext);

        myAnsList = new ArrayList<String>();
    }


    private void setQuestionsView()
    {
        rbtnA.setChecked(false);
        rbtnB.setChecked(false);
        rbtnC.setChecked(false);
        rbtnD.setChecked(false);

        answeredQsNo=questionId+1;
        tvNoOfQs.setText("Questions "+answeredQsNo+" of "+questionsList.size());

        txtQuestion.setText(currentQuestion.getQUESTION());
        rbtnA.setText(currentQuestion.getOptionA());
        rbtnB.setText(currentQuestion.getOptionB());
        rbtnC.setText(currentQuestion.getOptionC());
        rbtnD.setText(currentQuestion.getOptionD());

        questionId++;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

测验主要活动代码

package com.example.akshi.ictlearningengeducation;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class quizass1 extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Button btnStart=(Button) findViewById(R.id.btnStart);
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent conceptIntent=new Intent(quizass1.this,ConceptActivity.class);
                startActivity(conceptIntent);

            }
        });

        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.setDrawerListener(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 onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        return true;
    }
    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_home) {

            Intent conceptIntent=new Intent(quizass1.this,MainActivity.class);
            startActivity(conceptIntent);
        }
        else if (id == R.id.nav_quizone) {

            Intent conceptIntent=new Intent(quizass1.this,ConceptActivity.class);
            startActivity(conceptIntent);
        }
        else if (id == R.id.nav_quiztwo) {

            Intent conceptIntent=new Intent( quizass1.this,ConceptActivity1.class );
            startActivity(conceptIntent);

        }
        else if (id == R.id.nav_quizthree) {

            Intent conceptIntent=new Intent( quizass1.this,ConceptActivity2.class );
            startActivity(conceptIntent);

        }
        else if (id == R.id.nav_quizfour) {

            Intent conceptIntent=new Intent( quizass1.this,ConceptActivity3.class );
            startActivity(conceptIntent);

        }
        else if (id == R.id.nav_quiz5) {

            Intent conceptIntent=new Intent(quizass1.this,ConceptActivity4.class);
            startActivity(conceptIntent);

        }
        else if (id == R.id.nav_quiz6) {

            Intent conceptIntent=new Intent( quizass1.this,ConceptActivity5.class );
            startActivity(conceptIntent);

        }
        else if (id == R.id.nav_quiz7) {

            Intent conceptIntent=new Intent( quizass1.this,ConceptActivity6.class );
            startActivity(conceptIntent);

        }
        else if (id == R.id.nav_quiz8) {

            Intent conceptIntent=new Intent( quizass1.this,ConceptActivity7.class );
            startActivity(conceptIntent);

        }
        else if (id == R.id.nav_quiz9) {

            Intent conceptIntent=new Intent(quizass1.this,ConceptActivity8.class);
            startActivity(conceptIntent);

        }
        else if (id == R.id.nav_quiz10) {

            Intent conceptIntent=new Intent( quizass1.this,ConceptActivity9.class );
            startActivity(conceptIntent);

        }
        else if (id == R.id.nav_quiz11) {

            Intent conceptIntent=new Intent( quizass1.this,ConceptActivity10.class );
            startActivity(conceptIntent);

        }
        else if (id == R.id.nav_quiz12) {

            Intent conceptIntent=new Intent( quizass1.this,ConceptActivity11.class );
            startActivity(conceptIntent);

        }
        else if (id == R.id.nav_quiz13) {

            Intent conceptIntent=new Intent(quizass1.this,ConceptActivity12.class);
            startActivity(conceptIntent);
        }
        else if (id == R.id.nav_quiz14) {

            Intent conceptIntent=new Intent( quizass1.this,ConceptActivity13.class );
            startActivity(conceptIntent);

        }
        else if (id == R.id.nav_quiz15) {

            Intent conceptIntent=new Intent( quizass1.this,ConceptActivity14.class );
            startActivity(conceptIntent);

        }
        else if (id == R.id.nav_quiz16) {

            Intent conceptIntent=new Intent( quizass1.this,ConceptActivity15.class );
            startActivity(conceptIntent);

        }

        else if (id == R.id.nav_cse) {

            Intent conceptIntent=new Intent(quizass1.this,ConceptActivity16.class);
            startActivity(conceptIntent);

        }
        else if (id == R.id.nav_ece) {

            Intent conceptIntent=new Intent( quizass1.this,ConceptActivity17.class );
            startActivity(conceptIntent);

        }
        else if (id == R.id.nav_eee) {

            Intent conceptIntent=new Intent( quizass1.this,ConceptActivity18.class );
            startActivity(conceptIntent);

        }
        else if (id == R.id.nav_mech) {

            Intent conceptIntent=new Intent( quizass1.this,ConceptActivity19.class );
            startActivity(conceptIntent);

        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

如何通过添加db代码以移动项目单击来减少将多个活动创建为单个活动

在activity.java 中: 如何在此条件下使用意图去不同的数据库适配器-DBAdapters

1 个答案:

答案 0 :(得分:0)

您可以像这样使用FragmentPageAdapter:

besoins.stream().forEach(besoin ->

在Fragmnet类中,您可以使用不同的加载程序从数据库中获取值:

for (Besoin besoin : besoins)