【问题标题】:using php to dynamically populate the string.xml file in android使用php动态填充android中的string.xml文件
【发布时间】:2015-04-16 09:59:02
【问题描述】:

如何从数据库中获取 ChatList 名称并使用 php 和 mysql 将它们填充到 strings.xml 文件字符串数组标记中

   <string-array name="chatListNames">
            <item>Byamukama Robinhood</item>
            <item>Test name</item>
            <item>Ivan</item>
            <item>Mohsin Afir</item>
            <item>Test</item>

    </string-array>

【问题讨论】:

    标签: php android mysql xml


    【解决方案1】:

    使用 php 和 mysql 将它们填充到 strings.xml 文件字符串数组标记中

    strings.xml 用于常量(只读)字符串,因此无法在运行时更改

    因此,不要在strings.xml 中添加要在运行时更改的字符串,而是保存在SharedPreferences 中。

    如何从数据库中获取 ChatList 名称

    在应用程序中使用网络服务从服务器获取 ChatList 名称,然后更新 SharedPreferences 中的所有值。

    请参阅以下关于在 PHP 中创建 Web 服务和从 Android 访问的优秀教程:

    How to connect Android with PHP, MySQL

    【讨论】:

    • 使用这段代码,如何调用php script protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_chat); /* 从资源加载 (names,lastSeen,images) * 在这里您可以从 Web 服务器加载它 * */ names = getResources().getStringArray(R.array.chatListNames); lastSeen = getResources().getStringArray(R.array.usersLastSeen); images = getResources().getStringArray(R.array.chatListImages);
    【解决方案2】:

    这在 Android 中是不可能的,您可以使用数据库来做到这一点。 Strings.xml 不能在运行时修改。

    【讨论】:

      【解决方案3】:

      使用以下代码并更改数据库名称和访问权限,在浏览器中运行它并将其复制粘贴到 XML 文件中。

      <?php
      $servername = "localhost";
      $username = "username";
      $password = "password";
      $dbname = "myDB";
      
      // Create connection
      $conn = new mysqli($servername, $username, $password, $dbname);
      // Check connection
      if ($conn->connect_error) {
          die("Connection failed: " . $conn->connect_error);
      } 
      
      $sql = "SELECT id, firstname, lastname FROM MyGuests";
      $result = $conn->query($sql);
      
      if ($result->num_rows > 0) {
          // output data of each row
          echo "<string-array name="chatListNames">";
          while($row = $result->fetch_assoc()) {
              echo "<item>" . $row["id"]. "</item><br>";
          }
          echo "</string-array>";
      } else {
          echo "0 results";
      }
      $conn->close();
      ?>
      

      【讨论】:

        猜你喜欢
        • 2013-01-30
        • 1970-01-01
        • 2023-03-13
        • 2010-12-16
        • 1970-01-01
        • 2011-02-03
        • 1970-01-01
        • 2012-10-10
        • 1970-01-01
        相关资源
        最近更新 更多