[英]How to navigate to different databases using single activity on click an item in navigation drawer?
I have created multiple concept activities for different databases to access,to reduce that i need a solution which solves my issue in creation of multiple to single activity? 我已经创建了多个概念活动供不同的数据库访问,以减少需要一个解决我创建多个活动到单个活动的问题的解决方案?
IN ConceptActivity.Java 在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);
}
}
QUIZ MAIN ACTIVITY CODE 测验主要活动代码
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;
}
}
How to reduce the multiple activities creation to single activity by adding db code to move on item click 如何通过添加数据库代码以在项目单击上移动来将多个活动的创建减少为单个活动
In activity.java : how to use intent in this criteria to go to different databaseadapters-DBAdapters 在activity.java中 :如何在此条件下使用意图转到不同的databaseadapters-DBAdapters
you can use a FragmentPageAdapter like this: 您可以像这样使用FragmentPageAdapter:
public class CategoryAdapter extends FragmentPagerAdapter {
public CategoryAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new ConceptFragment1();
case 1:
return new ConceptFragment2();
case 2:
return new ConceptFragment3();
case 3:
return new ConceptFragment4();
default:
return null;
}
}
@Override
public int getCount() {
return 4;
}
}
In the Fragmnet Class you can use diferents Loaders to get the values from database: 在Fragmnet类中,可以使用不同的加载程序从数据库获取值:
public class ConceptFragment1 extends Fragment implements LoaderManager.LoaderCallbacks<List<Question>>{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.