Note that there are some explanatory texts on larger screens.

plurals
  1. POAny other alternatives to displaying MySQL server data in android apart from Listview?
    primarykey
    data
    text
    <p>I'm learning android app development and recently learnt how to incorporate webservices into my app.I have searched for days over the internet and on this site as well,with absolutely no luck! I'm developing a simple quiz app,where questions are loaded from a remote MySQL database and currently they are being displayed in a List view,but the aim is to get each question and answer options on it's own separate activity ,such that the user selects an answer and presses a next button to move onto the next question etc.I could create the activities directly with each question already,but the app needs to have that dynamic functionality where the questions could be altered from the server.I would really appreciate your help with ideas,maybe a tutorial or anything that will help me.Thank you.</p> <p>Below is my edited code with viewpager from here &lt; <a href="http://www.youtube.com/watch?v=JqFkkXMwWDg" rel="nofollow">http://www.youtube.com/watch?v=JqFkkXMwWDg</a>></p> <p>My AsyncTask class:</p> <pre><code>public class FetchQuestionsTask extends AsyncTask&lt;String, Void, String&gt; { private Context mContext; private View rootView; private ViewPager mViewPager; private PagerAdapter mPagerAdapter; ArrayList&lt;String&gt; QuestionArray = new ArrayList&lt;String&gt;(); ArrayList&lt;String&gt; AnswerArray = new ArrayList&lt;String&gt;(); 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&lt;String&gt; QuestionArray = new ArrayList&lt;String&gt;(); ArrayList&lt;String&gt; AnswerArray = new ArrayList&lt;String&gt;(); // convert json string to json array JSONArray aJson = new JSONArray(sJson); for (int i = 0; i &lt; 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(); } } </code></pre> <p>MyFragment class:</p> <pre><code> @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; } } </code></pre> <p>My adapter class:</p> <pre><code> public class PagerAdapter extends FragmentStatePagerAdapter{ ArrayList&lt;String&gt; QuestionArray = new ArrayList&lt;String&gt;(); ArrayList&lt;String&gt;AnswerArray = new ArrayList&lt;String&gt;(); 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 } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload