我正在学习 android 应用程序开发,最近学习了如何将 web 服务合并到我的应用程序中。我在互联网和这个网站上搜索了几天,完全没有运气!我正在开发一个简单的测验应用程序,其中问题是从远程 MySQL 数据库加载的,目前它们显示在列表视图中,但目的是让每个问题和答案选项在它自己的单独活动中,这样用户选择一个答案并按下下一个按钮进入下一个问题等。我已经可以直接使用每个问题创建活动,但是应用程序需要具有可以从服务器更改问题的动态功能。我真的很感激您对想法的帮助,也许是教程或任何对我有帮助的东西。谢谢。
下面是我用 viewpager 从这里编辑的代码 < http://www.youtube.com/watch?v=JqFkkXMwWDg >
我的 AsyncTask 类:
public class FetchQuestionsTask extends AsyncTask<String, Void, String> {
private Context mContext;
private View rootView;
private ViewPager mViewPager;
private PagerAdapter mPagerAdapter;
ArrayList<String> QuestionArray = new ArrayList<String>();
ArrayList<String> AnswerArray = new ArrayList<String>();
private String msg;
View description;
public FetchQuestionsTask(Context context, View rootView) {
this.mContext = context;
this.rootView = rootView;
}
@Override
protected String doInBackground(String... params) {
if (params == null)
return null;
// get url from params
String url = params[0];
try {
// create http connection
HttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
// connect
HttpResponse response = client.execute(httpget);
// get response
HttpEntity entity = response.getEntity();
if (entity == null) {
msg = "No response from server";
return null;
}
// we get response content and convert it to json string
InputStream is = entity.getContent();
return streamToString(is);
} catch (IOException e) {
msg = "No Network Connection";
Log.e("Log message", "No network connection");
}
return null;
}
@Override
protected void onPostExecute(String sJson) {
if (sJson == null) {
return;
}
try {
ArrayList<String> QuestionArray = new ArrayList<String>();
ArrayList<String> AnswerArray = new ArrayList<String>();
// convert json string to json array
JSONArray aJson = new JSONArray(sJson);
for (int i = 0; i < aJson.length(); i++) {
JSONObject json = aJson.getJSONObject(i);
QuestionArray.add(json.getString("description"));
// AnswerArray.add(json.getString("answer_text"));
}
} catch (JSONException e) {
msg = "Invalid response";
}
mPagerAdapter = new PagerAdapter(null, mContext);
mViewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
mViewPager.setAdapter(mPagerAdapter);
}
/**
* This function will convert response stream into json string
*
* @param is
* response string
* @return json string
* @throws IOException
*/
public String streamToString(final InputStream is) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
throw e;
} finally {
try {
is.close();
} catch (IOException e) {
throw e;
}
}
return sb.toString();
}
}
MyFragment 类:
@SuppressLint("ValidFragment")
public class Page1 extends Fragment {
Context c2;
String description, answer_text;
/* public Page1() {
}*/
@SuppressLint("ValidFragment")
public Page1(Context c2,String description,String answer_text){
this.c2 = c2;
this.description = description;
this.answer_text = answer_text;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_pages_list,container, false);
c2=getActivity();
new FetchQuestionsTask(c2, v).execute("http://10.0.2.2/webservice/new_questions");
try{
TextView question_txt = (TextView)v.findViewById(R.id.q_description);
question_txt.setText(description);
TextView answer_txt = (TextView)v.findViewById(R.id.ans_txt);
answer_txt.setText(answer_text);
}catch(Exception e){
Log.e("Log error :", "could not write the text views");
}
return v;
}
}
我的适配器类:
public class PagerAdapter extends FragmentStatePagerAdapter{
ArrayList<String> QuestionArray = new ArrayList<String>();
ArrayList<String>AnswerArray = new ArrayList<String>();
private int []colors = {Color.parseColor("#00B050"),Color.parseColor("#FFC000"), Color.parseColor("#DB1351"),
Color.parseColor("#B61C83"), Color.parseColor("#0070C0")};
Context c;
private Random rnd;
public PagerAdapter(FragmentManager fm,Context c) {
super(fm);
this.c = c;}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
String description = QuestionArray.get(position);
String answer_text = AnswerArray.get(position);
fragment = new Page1(c, description, answer_text);//pass value to be displayed in inflated view
return fragment;
}
@Override
public int getCount() {
return QuestionArray.size(); //get number of pages to be displayed
}
}