android - 除了 Listview 之外,还有其他替代方法可以在 Android 中显示 MySQL 服务器数据吗?

标签 android mysql

我正在学习 Android 应用程序开发,最近学会了如何将网络服务合并到我的应用程序中。我已经在互联网和这个网站上搜索了好几天,但完全没有运气!我正在开发一个简单的测验应用程序,其中问题从远程 MySQL 数据库加载,当前它们显示在 ListView 中,但目的是在其自己的单独 Activity 中获取每个问题和答案选项,以便用户选择一个答案并按下一个按钮转到下一个问题等。我可以直接为每个问题创建 Activity ,但应用程序需要具有可以从服务器更改问题的动态功能。我真的很感激您提供的想法帮助,也许是教程或任何对我有帮助的东西。谢谢。

下面是我使用 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
    }

}

最佳答案

您可以使用viewpager 。它将完美解决您的问题。

这是 FragmentStatePagerAdapter 的链接

如果您仍然无法理解,请查看此 link 中的示例。

这里还有一个链接,显示 Android FragmentPagerAdapter vs FragmentStatePagerAdapter 的比较

看一下您就会了解其中的差异。

关于android - 除了 Listview 之外,还有其他替代方法可以在 Android 中显示 MySQL 服务器数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18591195/

相关文章:

android - 如何正确停止前台服务?

android - 如何通过 Windows azure 通过 GCM 发送特定 Android 设备的通知?

android - 创建 ArrayAdapter 时出错

php - 通过 MySQL/PHP 在 CRUD 网格中实现年龄属性

php - 我如何通过从数据库检索代码来输出这种格式?

php - 用户照片库的 MySQL 设计

sql - 下面的 sql 查询数据规范化应该工作吗?

java - 使用 LoadData 时在 WebView 中运行 Javascript

java - 无法启动 ActivityComponentInfo

mysql - 选择 * 重复项,然后选择 * 非重复项 - mysql