这可能是这里最简单的问题之一,但我对android很新,所以仍然有一些挑战。我正在使用JSON从mysql服务器下载一些数据。一个问题列表。每个问题都是根据问题的总数创建自己的片段。我根据为该问题指定的内容显示不同的小部件 - 编辑文本,无线电按钮,微调器等。
问题1
我的挑战是,我似乎无法从这些小部件中输入/选择文本值。这在应用程序的不同活动中运行良好,但它只是这个片段类,我会遇到问题。
问题2
我也试图获取每个问题的位置,以便我可以正确显示下一个/后退按钮。如果我在第一个阙,隐藏后退按钮,如果在最后,隐藏下一个和显示完成。这样的东西。我现在的实现只有在我不按回按钮才能查看上一个问题时才有效。如果我这样做,那么当我到达最后一个项目时,按钮已经显示,但是它仍然显示下一个'
问题3
我还尝试插入输入字段的检查。如果用户点击下一步而不输入任何内容,显示吐司并且不移动到下一个项目。但是我仍然转到下一页,并显示toast msg"响应是必需的" 请帮助我看看我在实施中做错了什么。谢谢
我的相关代码
**fragmentStatePagerAdapter class**
public class PagerAdapter extends FragmentStatePagerAdapter {
public SQLiteDatabase db;
public PagerAdapter adapter;
Question question;
Context context2;
public PagerAdapter(Context c, FragmentManager fm) {
super(fm);
this.context2 = c;
}
@Override
public int getCount() {
System.out.println("inside get count");
return questions.size(); // get number of pages to be displayed
}
@Override
public Fragment getItem(int i) {
Log.i("at PagerAdapter", "yaaay!!At pager adapter");
Fragment fragment = null;
Question que = questions.get(i); //questions is the arraylist that has been populated with question objects from the server.
// pass value to be displayed in inflated view
fragment = new FragmentRadioButton(context2, que);
return fragment;
}
public CharSequence getPageTitle(int position) {
position++;
String s = "Question " + position;// setting the title
return s;
}
}
片段类
public class FragmentRadioButton extends Fragment {
Context context3;
private Question question;
private RadioButton[] radioButton;
private RelativeLayout relativeLayout1;
static ViewPager mViewPager = FragmentStatePagerActivity.mViewPager;
private TextView question_txtView;
private RadioGroup radiogroup;
private Button btnNext;
private Spinner spinner;
private RatingBar ratingBar;
private Button btnBack;
private EditText editText;
private Button btnDone;
private TextView countRating_txt;
private RelativeLayout relativeLayout2;
private static final int radio_button = 1;
private static final int m_spinner = 2;
private static final int edit_text = 3;
private static final int rating_bar = 4;
public static ArrayList<Integer> num = new ArrayList<Integer>();
private List<RadioGroup> allRadioGroups = new ArrayList<RadioGroup>();
private List<Spinner> allSpinners = new ArrayList<Spinner>();
private List<EditText> allEds = new ArrayList<EditText>();
List<RatingBar> allRatingBars = new ArrayList<RatingBar>();
public FragmentRadioButton() {
}
public FragmentRadioButton(Context c, Question que) {
this.context3 = c;
this.question = que;
}
//@SuppressLint("NewApi")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = getActivity().getLayoutInflater().inflate(
R.layout.fragment_radio_button, null);
ScrollView scrollView = (ScrollView) view
.findViewById(R.id.scrollViewRadioButton);
relativeLayout1 = (RelativeLayout) scrollView
.findViewById(R.id.relativeLayout1);
relativeLayout2 = (RelativeLayout) relativeLayout1
.findViewById(R.id.relativeLayout2);
context3 = getActivity().getApplicationContext();
try {
question_txtView = (TextView) relativeLayout1
.findViewById(R.id.question_text_radio_button);
question_txtView.setText(question.getDescription());
num.add(question.getQuestionId());
if (question.getWidgetId() == radio_button) {
radiogroup.setVisibility(View.VISIBLE);
radioButton = new RadioButton[question.getAnswers().size()];
Typeface font4 = Typeface.createFromAsset(getActivity()
.getAssets(), "fonts/enriqueta/Enriqueta-Regular.otf");
for (int i = 0; i < question.getAnswers().size(); i++) {
radioButton[i] = new RadioButton(context3);
radioButton[i].setLines(3);
radioButton[i].setText(question.getAnswers().get(i)
.getAnswer_Text());
radioButton[i].setTag(question.getAnswers().get(i)
.getAnswer_Id());
radioButton[i].setTextColor(Color.BLACK);
radioButton[i].setTypeface(font4);
radioButton[i].setTextSize(30);
radiogroup.addView(radioButton[i]);
radiogroup.isClickable();
}
allRadioGroups.add(radiogroup);
} else if (question.getWidgetId() == m_spinner) {
spinner.setVisibility(View.VISIBLE);
List<String> list = new ArrayList<String>();
for (int b = 0; b < question.getAnswers().size(); b++) {
String data = question.getAnswers().get(b).getAnswer_Text()
.toString();
spinner.setTag(question.getAnswers().get(b).getAnswer_Id());
list.add(data);
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
context3, R.layout.my_style, list) {
public View getView(int position, View convertView,
ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(22);
return v;
}
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
View v = super.getDropDownView(position, convertView,
parent);
((TextView) v).setTextColor(Color.BLACK);
return v;
}
};
spinner.setAdapter(dataAdapter);
allSpinners.add(spinner);
} else if (question.getWidgetId() == edit_text) {
editText.setVisibility(View.VISIBLE);
allEds.add(editText);
} else if (question.getWidgetId() == rating_bar) {
ratingBar.setVisibility(View.VISIBLE);
countRating_txt.setVisibility(View.VISIBLE);
ratingBar
.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar rating_bar,
float rating, boolean fromUser) {
rating = rating_bar.getRating();
countRating_txt.setText(String.valueOf(rating)
+ "Ratings");
}
});
allRatingBars.add(ratingBar);
}
// }
} catch (Exception e) {
Log.e("Log error :",
"could not write the text views in radioButton fragment");
}
ItemPosition();
return view;
}
OnClickListener btnNextOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (question.getWidgetId() == radio_button) {
validatedRadioGroup();
} else if (question.getWidgetId() == m_spinner) {
validatedSpinner();
} else if (question.getWidgetId() == edit_text) {
validatedEditText();
} else if (question.getWidgetId() == rating_bar) {
validatedRatingBar();
}
mViewPager.setCurrentItem(getItem(+1), true);
}
};
OnClickListener btnBackOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
mViewPager.setCurrentItem(getItem(-1), true);
}
};
OnClickListener btnDoneOnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
int lastItem = question.getQuestionIds().size();
if (lastItem == question.getQuestionIds().size()) {
if (question.getWidgetId() == radio_button) {
radioText();
} else if (question.getWidgetId() == m_spinner) {
spinnerText();
} else if (question.getWidgetId() == edit_text) {
EditText_Value();
} else if (question.getWidgetId() == rating_bar) {
ratingText();
}
Intent a = new Intent(getActivity(), FinishedSurvey.class);
startActivity(a);
}
}
};
}
public void ItemPosition() {
for (int j = 0; j < num.size(); j++) {
// int item = num.indexOf(j);
if (j == 0) {
btnBack.setVisibility(View.GONE);
btnNext.setVisibility(View.VISIBLE);
btnDone.setVisibility(View.GONE);
System.out.println("the first part");
} else if (!(j == 0 || (j == question.getQuestionIds().size() - 1))) {
btnBack.setVisibility(View.VISIBLE);
btnNext.setVisibility(View.VISIBLE);
btnDone.setVisibility(View.GONE);
System.out.println("the second part");
} else {
if (j == question.getQuestionIds().size() - 1) {
btnNext.setVisibility(View.INVISIBLE);
btnDone.setVisibility(View.VISIBLE);
System.out.println("the last part");
}
}
}
}