android-fragments 用php将mysql server中的数据显示到listview中

axr492tv  于 2022-11-14  发布在  Android
关注(0)|答案(2)|浏览(148)

我想在列表视图中显示数据从mysql数据库与php,列表视图是在片段。我怎么做呢?
下面是代码,其中显示了列表视图中的数据,但不是从数据库,我想在mysql数据库中用php检索数据,

public class ServiceFragment extends Fragment {        
        @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    // return inflater.inflate(R.layout.fragment_servicepay, container, false);

    View view = inflater.inflate(R.layout.fragment_servicepay, container, false);
    String[] ServiceItems = {"Android", "Iphone", "Java"};

    final ListView listView = view.findViewById(R.id.ServiceList);
    ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_expandable_list_item_2, ServiceItems );
    listView.setAdapter(listViewAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent in = new Intent(view.getContext(), ServiceUniqueActivity.class);
            startActivity(in);

        }
    });

    return view;

}

这是我的php文件的代码

<?php

$host = "localhost"; // host of MySQL server
$user = "root"; // MySQL user
$pwd = ""; // MySQL user's password
$db = "servicedb"; // database name

$con = mysqli_connect($host, $user, $pwd, $db) or die('Unable to connect');

if(mysqli_connect_error($con)) {
echo("Failed to connect to Database: ".mysqli_connect_error());
} 

$sql = "SELECT * FROM Services";
 $result= mysqli_query($con, $sql);
 if($result) 
 {
 while($row=mysqli_fetch_array($result))
{
$data[]=$row;
}

 print(json_encode($data));
 }
 mysqli_close($con);
 ?>
y1aodyip

y1aodyip1#

在我看来,从服务器获取数据的最佳方法是使用Retrofit库:http://square.github.io/retrofit/的值;
下面是一些从服务器检索JSON格式的数据并将其显示在ListView中的指南:

pes8fvy9

pes8fvy92#

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>DATABASE LIST DESIGN</title>
<style type="text/css">
        body
        {
            margin: 0;
            padding: 0;
            background: #3adb1e;
            font-family: sans-serif;
        }
        ul
        {
            position: relative;
            width: 370px;
            margin: 10px auto 0;
            padding: 10px;
            box-sizing: border-box;
            background: yellow;
            box-shadow: inset 0 0 10px rgba(0,0,0,.2);
            border-radius: 5px;
            overflow: hidden;
        }
        ul li
        {
            display: flex;
            background: #e0236c;
            padding: 10px 20px;
            color: #fff;
            margin: 5px 0;
            transition: .5s
        }
        ul li span
        {
            width: 100px;
            text-align: center;
        }
        ul li:hover
        {
            transform: scale(1.06);
            background: #ff3772
        }
</style>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "demon";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
$sql="select * from student";
$result=$conn->query($sql);
?>  
    <ul>
        <li>
            <span>STUDENT_ID </span>
            <span>NAME</span>
            <span>COURSE</span>
        </li>
    </ul>
    <?php
while($row=$result->fetch_assoc())
{
?> <ul>
        <li><?php echo $row["STUDENT_ID"]; ?>
            <?php echo $row["NAME"]; ?>
            <?php echo $row["COURSE"]; ?>
            <a href="deletedata.php?id=<?php echo $row["STUDENT_ID"]; ?>">
              <button type="button" class="deletebtn">Delete</button>
            </a>
        </li>
    </ul>
<?php   
}
?>
</body>
</html>

相关问题