Java 如何使用单击导航抽屉中的项目时的单个活动导航到不同的数据库?
我为不同的数据库创建了多个要访问的概念活动,以减少我需要一个解决方案来解决创建多对单活动的问题 在ConceptActivity.Java中Java 如何使用单击导航抽屉中的项目时的单个活动导航到不同的数据库?,java,android,Java,Android,我为不同的数据库创建了多个要访问的概念活动,以减少我需要一个解决方案来解决创建多对单活动的问题 在ConceptActivity.Java中 package com.example.akshi.ictlearningengeducation; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.content.Intent; import android.suppo
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);
}
}
如何通过添加db代码以在项目单击时移动,将多个活动创建减少为单个活动
在activity.java中:
如何在此条件中使用intent转到不同的数据库适配器DBAdapters您可以使用如下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;
}
}
在Fragmnet类中,您可以使用不同的加载程序从数据库中获取值:
public class ConceptFragment1 extends Fragment implements LoaderManager.LoaderCallbacks<List<Question>>{
}
public类ConceptFragment1扩展了片段实现LoaderManager.LoaderCallbacks{
}
final DBAdapter1 dbAdapter=新DBAdapter1(此);questionsList=dbAdapter.getAllQuestions();currentQuestion=questionsList.get(问题ID);如果不使用片段,我们就不能通过添加一段代码来更改当前活动
public class ConceptFragment1 extends Fragment implements LoaderManager.LoaderCallbacks<List<Question>>{
}