我正在构建一个简单的测验app.A有四个选项(单选按钮)的问题。 我正在使用JSON来获取数据。有一次,我只想用四个选项显示一个问题,按下一个按钮,下一个问题就会出现,依此类推,直到所有问题都解决了。 直到现在我能够用选项显示第一个问题。
在按下一个按钮时,如何显示下一个问题并使用相同的布局显示问题和选项。 任何帮助都会很棒..
我的获取JSON数据的代码如下:
public class Main_Activity_For_Quiz extends Activity {
public static int question_number = 0;
JSONObject jsonobject;
JSONArray jsonarray;
String idno = " ";
String url = "";
static ArrayList<String> questionList;
ArrayList<String> opList1;
ArrayList<String> opList2;
ArrayList<String> opList3;
ArrayList<String> opList4;
ArrayList<Integer> idlist;
public RadioGroup radioGroup;
ArrayList<Quiz_MCQ> mcq;
LinearLayout layout, layout2;
// ArrayList<String> nameList, urlList, resList;
TextView textView, textView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz_mcq);
String urlid = getIntent().getStringExtra("urlchar");
url = GlobalVariable.WonderslateQuizURL + urlid;
Log.e("msg", url);
Button submit = (Button) findViewById(R.id.next);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Increment the question number for every next click and
question_number++;
Bundle bundle = new Bundle();
bundle.putExtra("question_number", question_number);
FragmentManager fm = getFragmentManager();
android.app.FragmentTransaction ft = fm.beginTransaction();
DownloadJSON.PlaceholderFragment fragment = new DownloadJSON.PlaceholderFragment();
ft.replace(R.id.quiz_mcq, "Fragment_question");
ft.addToBackStack("Fragment_question");
ft.commit();
}
});
new DownloadJSON().execute();
}
class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
mcq = new ArrayList<Quiz_MCQ>();
jsonobject = JSONfunctions
.getJSONfromURL(url);
try {
// Locate the NodeList name
jsonarray = jsonobject.getJSONArray("results");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
Collections.reverse((List<?>) mcq);
// if (jsonobject.getString("resType").contains("ps")) {
// {
Quiz_MCQ detailAll = new Quiz_MCQ();
detailAll.setPs(jsonobject.getString("ps"));
detailAll.setOp1(jsonobject.getString("op1"));
detailAll.setOp2(jsonobject.getString("op2"));
detailAll.setOp3(jsonobject.getString("op3"));
detailAll.setOp4(jsonobject.getString("op4"));
detailAll.getId(jsonobject.getInt("id"));
mcq.add(detailAll);
Collections.reverse((List<?>) mcq);
}
} catch (JSONException e) {
Log.e("Error", "" + e.getMessage());
e.printStackTrace();
}
return null;
}
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
// private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.videos, container, false);
TextView txtview = (TextView) rootView.findViewById(R.id.question);
final TextView txtview2 = (TextView) rootView.findViewById(R.id.textViewChoice);
RadioButton rb1 = (RadioButton) rootView.findViewById(R.id.radioButton);
RadioButton rb2 = (RadioButton) rootView.findViewById(R.id.radioButton2);
RadioButton rb3 = (RadioButton) rootView.findViewById(R.id.radioButton3);
RadioButton rb4 = (RadioButton) rootView.findViewById(R.id.radioButton4);
Bundle bundle = getArguments();
//Get the next question number here
int position = bundle.getInt("position");
txtview.setText(questionList.get(position));
rb1.setText(rb1.get(position));
rb2.setText(rb2.get(position));
rb3.setText(rb3.get(position));
rb4.setText(rb4.get(position));
return rootView;
}
}
@Override
protected void onPostExecute(Void args) {
}
}
}
答案 0 :(得分:0)
片段是为此任务量身定制的。它易于使用,您可以从原始documentation中学习。
主要思想是使用容器作为主要布局。容器是像LinearLayout,RelativeLayout,FrameLayout这样的ViewGroup ......所以创建你的主要布局并为你想要反复使用的部分放置FrameLayout。然后创建其他片段布局或布局以放入容器内。
答案 1 :(得分:0)
我假设您使用文本视图进行问答。
TextView textViewQuestion,textViewOpt1,textViewOpt2,textViewOpt3,textViewOpt4;
int QuestionNo=0;
现在在像
这样的视图上设置数据buttonNext.setOnClickListner(...){
MCQ msq=(MCQ)mcq.get(QuesionNo)
textViewQuestion.setText(msq.getQuestion);
textViewOpt1.setText(msq.getOpt1);
.
.
.
QuestionNo=QuestionNo+1;
}
这样,您可以在一个活动中完成。下一个测验级别下载JSON。
对于第一个问题,您可以在下载JSON之后编写此代码。只需将MCQ置于第0位。
答案 2 :(得分:0)
在选择每个问题后,在HashMap类型的Arraylist中添加所有问题和答案。只需像这样更新Arraylist:
ArrayList<HashMap<String,Object>> parent;
HashMap<String,Object> childanswer ;
parent= new ArrayList<>();
childanswer= new HashMap<>();
childanswer.put("key",selectedanswer)
parent.add(childanswer)
也可以在相同的活动中使用片段并直接替换按钮上的视图直到所有问题结束。
答案 3 :(得分:0)
到目前为止,您的代码还不错。 在XML中声明如下
<FrameLayout
android:id="@+id/question_fragment"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_height="fill_parent"/>
在您的活动中,您现在已经保存了所有选项列表。 现在创建一个片段XML文件,其布局包含
<强> question_fragment.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView"
android:layout_gravity="center_horizontal" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/radioButton1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/radioButton2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/radioButton3" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New RadioButton"
android:id="@+id/radioButton4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_gravity="center_horizontal" />
</LinearLayout>
我刚刚创建了一个TextView for Question,4个单选按钮,用于选项和提交按钮。
现在,您在MainActivity中使用以下代码
使用static关键字
在全局中声明变量 public static int question_number=0;
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Increment the question number for every next click and
send it as argument an receive in the questionfragment and
use to index all your array lists.
question_number++;
Bundle bundle = new Bundle();
bundle.putExtra("question_number",question_number);
FragmentTransaction ft = fm.beginTransaction();
Questionfragment fragment=new Questionfragment();
fragment.setArguments(bundle);
ft.replace(R.id.question_fragment, ,"Fragment_question");
ft.addToBackStack("Fragment_question");
ft.commit();
}
});
Now copy this new fragment code in MainActivity below main
function
public static class Questionfragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.question_fragment,
container, false);
TextView textview=findViewById(R.id.textview);
RadioButton radioButton1=findViewById(radioButton1);
RadioButton radioButton2=findViewById(radioButton2);
RadioButton radioButton3=findViewById(radioButton3);
RadioButton radioButton4=findViewById(radioButton4);
Bundle bundle = getArguments();
//Get the next question number here
int position = bundle.getInt("position");
textview.setText(questionList.get(position));
radioButton1.setText(rb1.get(position));
radioButton2.setText(rb2.get(position));
radioButton3.setText(rb3.get(position));
radioButton4.setText(rb4.get(position));
return view;
}
}