【发布时间】:2018-04-11 08:31:03
【问题描述】:
我想用 php 在 mysql 数据库中的 listview 数据中显示,listview 在片段中。我该怎么做。
这里是代码,其中显示列表视图中的数据而不是来自数据库的数据,我希望使用 php 在 mysql 数据库中检索数据,
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);
?>
【问题讨论】:
标签: java php android listview android-fragments