来自服务器的 XML 用于填充 Spinner

发布于 2024-11-26 16:48:49 字数 180 浏览 1 评论 0原文

我希望旋转器从服务器的 MySQL 填充,该网站是 PHP 的,并且可以格式化以发送 XML 或 JSON 文件。

有没有办法用这些信息填充微调器?我可以将文件保存到内存中,但如何在微调器上读取它?

是否应该将该文件另存为首选项文件,然后以这种方式读取?

任何帮助将不胜感激。

谢谢

I would like for the spinner to be populated from a server's MySQL, the website is PHP and can be formated to send an XML or JSON file.

Is there a way to populate the spinner with that information? I can save the file to memory, but how can I read it on the spinner?

Should the file be save as a Preferences file and then read that way?

Any help will be appreciated.

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情泪▽动烟 2024-12-03 16:48:49

各位;

经过大量的寻找、尝试、测试和做数百件事之后,我回答了我自己的问题。

这可能是 Doh 的东西!程序员的答案,但我不是程序员,我正在学习和做这个作为一个学校项目,所以我对Java的了解甚至不足以被称为“基础”。

这就是我自己的问题的答案;

String CountryName = preferences.getString("country name", null);
List<String> lCounties = Arrays.asList(Counties(CountryName).split(","));
CharSequence[] csCounties = lCounties.toArray(new CharSequence[lCounties.size()]);

final Spinner sCounty = (Spinner) findViewById(R.id.county);    
ArrayAdapter<CharSequence> aCounty = new ArrayAdapter<CharSequence>(
        this, android.R.layout.simple_spinner_item,csCounties);
aCounty.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sCounty.setAdapter(aCounty);
sCounty.setSelection(preferences.getInt("county",0));
sCounty.setOnItemSelectedListener(new OnItemSelectedListener(){
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){
      int iCounty = sCounty.getSelectedItemPosition();
      String CountyName = sCountry.getItemAtPosition(arg2).toString();
      SharedPreferences.Editor editor = preferences.edit();
      editor.putString("county name", CountyName);
      editor.putInt("county", iCounty);
      editor.commit();

      //Refresh Page is county has changed
      if (oCounty != iCounty){ refresh(); }
    }
    public void onNothingSelected(AdapterView<?> arg0) {}
  });

现在这是解释(据我所知......)

首先我从另一个旋转器读取国家(我有“几个”旋转器连接在一起)

String CountryName = preferences.getString("country name", null);

然后我使用这些信息来调用一个子类(我也可以发布这个)。该子类联系服务器以创建 MySQL 查询并检索所需信息;在本例中,是给定国家/地区的县(或州),列表被接收为 state1、state2、state3,[这是在服务器上的 PHP 上格式化的以提供帮助]

List<String> lCounties = Arrays.asList(Counties(CountryName).split(","));
CharSequence[] csCounties = lCounties.toArray(new CharSequence[lCounties.size()]);

接下来的代码行是实际的 Spinner,有关于旋转器的信息网上有很多,所以我不会解释太多。

微调器使用此行读取保存的信息;

sCounty.setSelection(preferences.getInt("county",0));

这允许微调器预先填充用户的选择。

然后我存储选择,我存储名称和指针。加载页面时会保存名称以完成 URL,并且会保存指针,因为 Spinner 似乎喜欢指针。

  SharedPreferences.Editor editor = preferences.edit();
  editor.putString("county name", CountyName);
  editor.putInt("county", iCounty);
  editor.commit();

我做的最后一件事是比较,当页面加载时读取变量oCounty,它只是读取preference文件并将值存储在oCounty上code>(原始县),如果县名称发生变化,则调用refresh(),该子类刷新页面。 [这对我来说也很痛苦]

  //Refresh Page is county has changed
  if (oCounty != iCounty){ refresh(); }

现在整个事情的工作方式是,一旦从列表中选择了一个国家/地区,页面就会转到服务器,加载该国家/地区的县/州并刷新页面,因此,在旋转器上加载值,如果国家/地区发生变化,该过程将再次加载县/州。

相同的代码用于县/州来加载城市,我只是更改了变量的名称。

我确信程序员有更好、更快、更快的方法来做到这一点,但这些是我关于如何使用存储在服务器上的 MySQL 数据库中的信息填充微调器的 2 美分。

问候;
拉蒙

Folks;

After lots of looking and trying and testing and doing hundread of other things I answered my own question.

This is something that may be a Doh! answer for programmers, but I am not a programmer, I am learning and doing this as a school project, so my knowledge of Java is not even enought to be called "basic".

This is the answer to my own question;

String CountryName = preferences.getString("country name", null);
List<String> lCounties = Arrays.asList(Counties(CountryName).split(","));
CharSequence[] csCounties = lCounties.toArray(new CharSequence[lCounties.size()]);

final Spinner sCounty = (Spinner) findViewById(R.id.county);    
ArrayAdapter<CharSequence> aCounty = new ArrayAdapter<CharSequence>(
        this, android.R.layout.simple_spinner_item,csCounties);
aCounty.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sCounty.setAdapter(aCounty);
sCounty.setSelection(preferences.getInt("county",0));
sCounty.setOnItemSelectedListener(new OnItemSelectedListener(){
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){
      int iCounty = sCounty.getSelectedItemPosition();
      String CountyName = sCountry.getItemAtPosition(arg2).toString();
      SharedPreferences.Editor editor = preferences.edit();
      editor.putString("county name", CountyName);
      editor.putInt("county", iCounty);
      editor.commit();

      //Refresh Page is county has changed
      if (oCounty != iCounty){ refresh(); }
    }
    public void onNothingSelected(AdapterView<?> arg0) {}
  });

Now this is the explanation (as far as I undertand...)

First I read the country from another spinner (I have 'few' spinners joined together)

String CountryName = preferences.getString("country name", null);

Then I use this information to call a subclass (I can publish this too). This subclass contacts the server to create a MySQL query an retrieves the needed information; in this case the counties (or states) for a given country, the list is received as state1,state2,state3, [this was formated on the PHP on the server to help]

List<String> lCounties = Arrays.asList(Counties(CountryName).split(","));
CharSequence[] csCounties = lCounties.toArray(new CharSequence[lCounties.size()]);

The next lines of code are the actual Spinner, there is alot of information about spinner on the net, so I will not explain much of this.

The spinner read the saved information using this line;

sCounty.setSelection(preferences.getInt("county",0));

this allows the spinner to be pre-populated with the users choice.

Then I store the selection, I store both the name and pointer. The name is saved to complete the URL when the page is loaded and the pointer is saved bacuase the Spinner seems to like pointers.

  SharedPreferences.Editor editor = preferences.edit();
  editor.putString("county name", CountyName);
  editor.putInt("county", iCounty);
  editor.commit();

The last thing I do is a comparison, the varible oCounty is read when the page loads, it simply reads the preference file and stores the value on oCounty (Original County), if the county name changes, then it calls refresh(), this subclass refreshes the page. [This too was a pain to put together for me]

  //Refresh Page is county has changed
  if (oCounty != iCounty){ refresh(); }

The way the whole thing works now is that once a country is selected from the list, the page goes to the server, loads the counties/states for the country and refreshed the page, thus loading the values on the spinner, if the country changes, the process will load the counties/states again.

The same code is used with the counties/states to load the cities, I jsut changed the variable's names.

I am sure programmers have a nicer, faster, quicker way to do this, but these are my 2 cents on how to populate spinners with iformation stored on a MySQL database on the server.

Regards;
Ramón

慕烟庭风 2024-12-03 16:48:49

首先解析您的 XMl 或 JSON。然后创建将在微调器中显示的数据列表或数组。将数据传递给微调器的适配器。

例子

Spinner sp = (Spinner) findViewById(R.id.spnContactTypes);

        String numbers[] = { "ONE", "TWO" }; // data that you got from xml or JSON
        ArrayAdapter<String> adapter; = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, numbers);
        sp.setAdapter(adapter);

First parse your XMl or JSON. then create a list or array of the data which will be shown in spinner. pass the data to the adapter of the spinner.

Example

Spinner sp = (Spinner) findViewById(R.id.spnContactTypes);

        String numbers[] = { "ONE", "TWO" }; // data that you got from xml or JSON
        ArrayAdapter<String> adapter; = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, numbers);
        sp.setAdapter(adapter);
葵雨 2024-12-03 16:48:49

是的,Spinner 可以从解析的 XML/JSON 的输出中填充

ArrayAdapter<String> adapter = new ArrayAdapter<String>(Map.this.getApplicationContext(), android.R.layout.simple_spinner_item, HERE-GOES-PARSED-ARRAY-OF-JSON-or-XML);
                        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        spin.setAdapter(adapter);

Yes, Spinner can be populated from the output of the parsed XML/JSON

ArrayAdapter<String> adapter = new ArrayAdapter<String>(Map.this.getApplicationContext(), android.R.layout.simple_spinner_item, HERE-GOES-PARSED-ARRAY-OF-JSON-or-XML);
                        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        spin.setAdapter(adapter);
春夜浅 2024-12-03 16:48:49

后端

这由Mysql数据库和Web服务器组成。

遵循的摘要步骤:

  1. 创建一个 mysql 数据库
  2. 创建一个配置文件以在我们的数据库和 Web 服务器(php)之间建立连接
  3. 创建一个 php 脚本,该脚本将包含查询数据库的方法,请访问下面的链接。

前端

  1. 创建activity_main.xml,在activity_main.xml中添加TextView和Spinner控件。
  2. 打开MainActivity类,使用请求方法创建到服务器的Android路径,该方法与服务器连接以获取服务器输出并将其转换为响应对象。
  3. 创建一个 java 文件作为实体类来保存微调项目。
  4. 创建自定义 BaseAdapter 将远程数据源与微调器项绑定。
  5. 调整 Gradle Build,以在应用程序 gradle.build 中包含项目依赖项。
  6. 调整 Manifest.xml 文件以包含 Manifest.xml 文件用户权限 - 由于应用程序需要互联网,因此解释了互联网进行网络调用的情况。阅读​​完整示例:如何使用 php mysql 数据库值加载 android spinner

Back-end

This consists of the Mysql Database and Webserver.

Summary steps to follow:

  1. Create a mysql database
  2. Create a configuration file to make a connection between our database and web server(php)
  3. Create a php script which will house the method that will query the database visit the link below.

Front-end

  1. Create activity_main.xml , add TextView and Spinner controls in activity_main.xml.
  2. Open MainActivity class,Create Android path to the server with a request method that connects with the server to obtain the server output and convert it to the response�s object.
  3. Creating a java file to serve as an entity class to hold spinner items.
  4. Creating a custom BaseAdapter to bind the remote data source with the spinner items.
  5. Adjusting Gradle Build,to include projectdependencies in the app gradle.build.
  6. Adjust Manifest.xml file to include Manifest.xml file user permissions-Explained for internet to make a network call since application needs internet.Read full example:How to load android spinner with php mysql database values
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文