【问题标题】:How to set ArrayList values from database MySQL with IDE (Android Studio)?如何使用 IDE(Android Studio)从数据库 MySQL 设置 ArrayList 值?
【发布时间】:2018-12-13 11:24:59
【问题描述】:

我想将 MySQL 数据库表中的数据放入 Android Studio。
用于修改教程中的脚本。

教程中ArrayList的值为:

HomeCollection.date_collection_arr = new ArrayList<HomeCollection>();
HomeCollection.date_collection_arr.add(new HomeCollection("2018-07-08" ,"Title1","Subject1","Description1"));
HomeCollection.date_collection_arr.add(new HomeCollection("2018-07-09" ,"Title2","Subject2","Description2"));
HomeCollection.date_collection_arr.add(new HomeCollection("2018-07-10" ,"Title3","Subject3","Description3"));

我的问题:

如何修改数据数组,以MySql表中的所有数据为例 有下面的列?

|编号 |日期 |标题 |主题 |说明 |

任何帮助将不胜感激

【问题讨论】:

  • 首先你的数据库 SQLite 或 MySQL 是什么?

标签: java android mysql arrays


【解决方案1】:

谢谢你的提示,但我还是不明白,

我尝试获取数组列表,通过这种方式成功查看列表中的数据(波纹管数组脚本),

也许有人可以给我更具体的,如何实现/组合我从这种方式(下面)获得的值数据到我的问题的数组(我想从这个链接 https://www.developerbrothers.com/highlight-events-custom-calendar-android-studio-developerbrothers-com/ 执行示例脚本)但我想要数据值来自 MySql,谢谢

私人无效 showOrder(){ JSONObject jsonObject = null; ArrayList> list = new ArrayList>(); 尝试 { jsonObject = 新 JSONObject(JSON_STRING); JSONArray 结果 = jsonObject.getJSONArray(konfigurasi.TAG_JSON_ARRAY);

        for(int i = 0; i<result.length(); i++){
            JSONObject jo = result.getJSONObject(i);

            String idplace = jo.getString(konfigurasi.TAG_PLACE);
            String titl = jo.getString(konfigurasi.TAG_TITLE);
            String subj = jo.getString(konfigurasi.TAG_SUBJECT);
            String desc= jo.getString(konfigurasi.TAG_DESCRIPTION);
            String dateo = jo.getString(konfigurasi.TAG_DATEORDER);

            HashMap<String,String> orders = new HashMap<>();

            orders.put(konfigurasi.TAG_PLACE,idplace);
            orders.put(konfigurasi.TAG_TITLE,titl);
            orders.put(konfigurasi.TAG_SUBJECT,subj);
            orders.put(konfigurasi.TAG_DESCRIPTION,desc);

            orders.put(konfigurasi.TAG_DATEORDER,dateo);
            list.add(orders);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

    ListAdapter adapter = new SimpleAdapter(
            DateEventActivity.this, list, R.layout.list_item,
            new String[]{konfigurasi.TAG_PLACE,konfigurasi.TAG_TITLE,konfigurasi.TAG_SUBJECT,konfigurasi.TAG_DESCRIPTION,konfigurasi.TAG_DATEORDER},
            new int[]{R.id.idplace, R.id.titl, R.id.subj, R.id.desc, R.id.dateo});

    listView.setAdapter(adapter);
}

private void getJSON(){
    class GetJSON extends AsyncTask<Void,Void,String> {

        ProgressDialog loading;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(DateEventActivity.this,"Mengambil Data","Mohon Tunggu...",false,false);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            JSON_STRING = s;
            showOrder();
        }

        @Override
        protected String doInBackground(Void... params) {
            RequestHandler rh = new RequestHandler();
            String s = rh.sendGetRequest(konfigurasi.URL_GET_ALL);
            return s;
        }
    }
    GetJSON gj = new GetJSON();
    gj.execute();
}

【讨论】:

    【解决方案2】:

    //你可以这样做。 从arraylist中获取值到字符串数组中,然后拆分数组,然后将字符串发送到数据库。给出下面的代码

     String[] value = HomeCollection.date_collection_arr.get(0).split(",");
     String date=value[0];
     String title=value[1];
     String description=value[2];
    

    //如果您的数组列表值包含空格,请使用以下代码

     String[] valuetrim= HomeCollection.date_collection_arr.get(0).split("\\s*,\\s*");
     String valuetrim=value[0];
     String valuetrim=value[1];
     String valuetrim=value[2];
    

    【讨论】:

      猜你喜欢
      • 2017-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      • 2017-08-25
      • 2014-07-25
      • 1970-01-01
      • 2020-01-27
      相关资源
      最近更新 更多