Reputation:
I'm trying to create a quiz app page with swipe function. Each page shows a question and 3 optional answers. For the answers I'm using listview. Contents of listview are not looping. Every time I swipe pages, the answer list keep showing row of option-1. Please anyone help.
public class FActivity extends FragmentActivity {
public static TextView mTitleView;
public static TextView mQuestionView;
public static ListView answerView;
private static String[]singleRow;
private static ArrayAdapter<String> arrayAdapter;
public static int index = 6;
CollectionPagerAdapter mCollectionPagerAdapter;
static ViewPager mViewPager;
private static final String TAG = "QuizActivity";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_collection);
mCollectionPagerAdapter = new CollectionPagerAdapter(getSupportFragmentManager());
populateListView();
// Set up the ViewPager, attaching the arrayAdapter.
mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mCollectionPagerAdapter);
}
private String[] populateListView() {
int row = 0;
if (row < QuestionAndAnswer.Answer.getmAnswer().length) {
singleRow = QuestionAndAnswer.Answer.getmAnswer()[row++];
Log.d(TAG, Arrays.toString(QuestionAndAnswer.Answer.getmAnswer()[row]));
arrayAdapter = new ArrayAdapter<>
(this, //context
R.layout.activity_f, //textView set up in xml
singleRow //name of array
);
}return singleRow;
}
public static class CollectionPagerAdapter extends FragmentStatePagerAdapter implements com.example.michikoote.testa.CollectionPagerAdapter {
public CollectionPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int m) {
ObjectFragment fragment2 = new ObjectFragment();
Bundle args = new Bundle();
args.putInt(ObjectFragment.ARG_OBJECT, m + 1); //object is just an integer
fragment2.setArguments(args);
return fragment2;
}
@Override
public int getCount() {
// determine how many pages to show
return index;
}
}
public static class ObjectFragment extends Fragment {
public static final String ARG_OBJECT = "";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_collection_object, container, false);
Bundle args = getArguments();
mTitleView = (TextView) rootView.findViewById(R.id.page_number);
mTitleView.setText((Integer.toString(args.getInt(ARG_OBJECT))) + "/" + index);
mQuestionView = (TextView) rootView.findViewById(R.id.tv_question);
mQuestionView.setText(Question.GetSingleQuestion());
return rootView;
}
}
}
QuestionAndAnswer.java -class file
public class QuestionAndAnswer {
private static final String TAG = "MyActivity";
public static int maxPagenumber;
public static class Question {
public static int q = 0;
public static String GetSingleQuestion() {
String[] mQuestion = {
"question1",
"question2",
"question3",
"question4",
"question5"
};
if (q > mQuestion.length-1) {
}
return mQuestion[q++];
}
}
public static class Answer {
public static String[][] getmAnswer() {
return mAnswer;
}
private static String[][] mAnswer = {
{"option1-1", "option1-2", "option1-3"},
{"option2-1", "option2-2"},
{"option3-1", "option3-2", "option3-3"},
{"option4-1", "option4-2", "option4-4"},
{"option5-1", "option5-2"},
};
}
}
activity_f.xml -layout
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TextView>
Upvotes: 0
Views: 875
Reputation: 1935
Change this below:
private void populateListView() {
for (int row = 0;row < QuestionAndAnswer.Answer.getmAnswer().length; row++) {
singleRow = Answer.getmAnswer()[row];
//set Answer
answerView = (ListView)this.findViewById(R.id.li_answers);
answerView.setAdapter(arrayAdapter);
Log.d(TAG, Arrays.toString(Answer.getmAnswer()[row]));
}
arrayAdapter = new ArrayAdapter<String>
(this, //context
R.layout.activity_f, //textView set up in xml
singleRow //name of array
);
}
To this:
private void populateListView() {
arrayAdapter = new ArrayAdapter<String>
(this, //context
R.layout.activity_f, //textView set up in xml
singleRow //name of array
);
for (int row = 0;row < QuestionAndAnswer.Answer.getmAnswer().length; row++) {
singleRow = Answer.getmAnswer()[row];
//set Answer
answerView = (ListView)this.findViewById(R.id.li_answers);
answerView.setAdapter(arrayAdapter);
Log.d(TAG, Arrays.toString(Answer.getmAnswer()[row]));
}
}
Upvotes: 1