我是新手,Android Studio我想将数据库(我使用MySQL)中的数据显示到中TextView。我也使用Button和RadioButton。单击按钮后,数据将显示在中TextView。这是我的PHP代码:
<?php
$host = 'localhost';
$user = 'root';
$pass = 'bismillah';
$database = 'rizka';
$conn = mysqli_connect($host, $user, $pass, $database);
if(!$conn) {
die('Koneksi database gagal: ');
}else{
echo 'berhasil<br>';
}
$dtkategori = $_GET["kategori"];
$sql = "SELECT * FROM manarul WHERE kategori = '$dtkategori'";
$hasil = mysqli_query($conn, $sql);
while($row = mysqli_fetch_row($hasil)) {
echo "Jumlah Slot = " .$row[1];
echo "\n";
echo "adalah " .$row[2];
echo "\n";
}
?>
这是我的Java代码:
package com.example.googlemaps;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.util.Log;
public class CobaActivity extends AppCompatActivity {
public static final String LOG_TAG ="dataBaru";
String memilih[]={"Silakan Pilih Tempat Parkir", "Masjid Manarul", "Gedung FTSP", "Gedung Rektorat"};
private RadioGroup radioGroupNb;
private RadioButton radioButtonKosong, radioButtonTerisi;
private TextView txt;
private Button pilih, proses;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_coba);
txt = (TextView) findViewById(R.id.TextView03);
pilih = (Button) findViewById(R.id.pilih_parkir1);
proses = (Button) findViewById(R.id.tampil_slot);
radioGroupNb = (RadioGroup) findViewById(R.id.radioGroupNb);
radioButtonKosong = (RadioButton) findViewById(R.id.radioButtonKosong);
radioButtonTerisi = (RadioButton) findViewById(R.id.radioButtonTerisi);
proses.setOnClickListener(new clickproses());
getSupportActionBar().setTitle("Pilih Gedung Parkir");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, memilih);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
if (position==1){
final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
pilih.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(CobaActivity.this, Pilihan_Parkir_Lanjut.class);
startActivity(intent);
finish();
}
});
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
class clickproses implements Button.OnClickListener {
public void onClick (View view){
String dtkategori = null;
if (radioButtonKosong.isChecked()){
dtkategori = "kosong";
}
if (radioButtonTerisi.isChecked()){
dtkategori = "terisi";
}
String str = downloadTeks("http://192.168.1.9/cobaget.php?kategori="+dtkategori);
txt.setText(str);
}
}
private InputStream OpenHttpConnection (String urlString) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
private String downloadTeks (String URL) {
InputStream in = null;
try {
in = OpenHttpConnection(URL);
} catch (IOException e){}
int charRead;
StringBuffer data = new StringBuffer();
String str = "";
try {
while ((charRead = in.read()) != -1){
data.append((char) charRead);
}
str = data.toString();
in.close();;
} catch (IOException e){}
return str;
}
}
这是我的数据库: MySQL数据库名称是Rizka,表名称是manarul
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。