我想将MySQL表数据显示到android中的listview
。
我使用UserClass
作为模型,ListViewAdapter
作为列表适配器,ViewUser
类是将显示ListView
的主类。我正在使用PHP脚本从MySQL获取数据。
我认为在解析JSON对象时存在一些问题。
活动
public class ViewUser extends AppCompatActivity {
private static final String VIEW_URL= "http://ajaygohel012.000webhostapp.com/ViewUser.php";
ListView listUser;
List<User> userlist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_user);
listUser= findViewById(R.id.listUser);
userlist= new ArrayList<>();
loadUserList();
}
private void loadUserList(){
final ProgressBar progressBar= (ProgressBar) findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
StringRequest stringRequest= new StringRequest(Request.Method.GET, VIEW_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressBar.setVisibility(View.INVISIBLE);
try {
JSONArray userArray= new JSONArray(response);
for (int i=0;userArray.length()<i;i++){
JSONObject userObject = userArray.getJSONObject(i);
User user=new User();
user.getName();
user.getEmpcode();
user.getLocation();
user.getDepartment();
user.getUsername();
userlist.add(user);
}
ListViewAdapter adapter = new ListViewAdapter(userlist, ViewUser.this);
listUser.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(ViewUser.this, error.getMessage(), Toast.LENGTH_SHORT);
}
});
RequestQueue requestQueue= Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
UserClass的
public class User {
String name, empcode, location, department, username;
public User(){
this.name=name;
this.empcode=empcode;
this.location=location;
this.department=department;
this.username=username;
}
public String getName() {
return name;
}
public String getEmpcode() {
return empcode;
}
public String getLocation() {
return location;
}
public String getDepartment() {
return department;
}
public String getUsername() {
return username;
}
}
ListviewAdapter
public class ListViewAdapter extends ArrayAdapter<User> {
private List<User> userlist;
private Context cntx;
public ListViewAdapter(List<User> userlist, Context cntx){
super(cntx, R.layout.list_item, userlist);
this.userlist=userlist;
this.cntx=cntx;
}
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater= LayoutInflater.from(cntx);
View listViewitem= inflater.inflate(R.layout.list_item,null, true);
TextView viewName= listViewitem.findViewById(R.id.viewName);
TextView viewEmployeeCode= listViewitem.findViewById(R.id.viewEmployeeCode);
TextView viewLocation= listViewitem.findViewById(R.id.viewLocation);
TextView viewDepartment= listViewitem.findViewById(R.id.viewDepartment);
TextView viewUserName= listViewitem.findViewById(R.id.viewUserName);
User user=userlist.get(position);
viewName.setText(user.getName());
viewEmployeeCode.setText(user.getEmpcode());
viewLocation.setText(user.getLocation());
viewDepartment.setText(user.getDepartment());
viewUserName.setText(user.getUsername());
return listViewitem;
}
}
这是我的php文件
ViewUser
<?php
$connection = mysqli_connect("localhost", "abc", "xyz", "id3275958_user") or die("Error " . mysqli_error($connection));
$sql = "select name,empcode,location,department,username from user";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
$emparray[] = $row;
}
echo json_encode($emparray);
mysqli_close($connection);
?>
JSON
[{
"name": "ajay",
"empcode": "1234",
"location": "Pune",
"departmnt": "Technology",
"username": "acg"
}, {
"name": "Ajay",
"empcode": "a123",
"location": "Goa",
"department": "Finance",
"username": "acg12"
}
]
item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/viewName"
android:layout_width="100dp"
android:layout_height="28dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.067"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.017" />
<TextView
android:id="@+id/viewEmployeeCode"
android:layout_width="100dp"
android:layout_height="31dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.533"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.017" />
<TextView
android:id="@+id/viewLocation"
android:layout_width="100dp"
android:layout_height="33dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.97"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.017" />
<TextView
android:id="@+id/viewDepartment"
android:layout_width="100dp"
android:layout_height="27dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.087" />
<TextView
android:id="@+id/viewUserName"
android:layout_width="100dp"
android:layout_height="29dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.533"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.087" />
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:1)
不可否认,您可能不需要在PHP中添加顶级数组,但这对我有用。
network = input_data(shape=[None, 1600, 1], name='input')
network = conv_1d(network, 32, 11, activation='relu', padding="same")
network = max_pool_1d(network, 4, strides=4)
network = fully_connected(network, 64, activation='softmax')
network = regression(network,
optimizer='adam',
learning_rate=0.01,
loss='categorical_crossentropy',
name='target',
to_one_hot=True,
n_classes=(4000, 64),
batch_size=1)
现在您可以像这样解析JSON响应:
// Add a top level array object to contain your rows of data
$response["data"] = array();
while ($row = mysqli_fetch_assoc($result)) {
$data = array(); // Your return array
$data["name"] = $row["name"]
$data["empcode"] = $row["empcode"];
$data["location"] = $row["location"];
$data["department"] = $row["department"];
$data["username"] = $row["username"];
// pushes a new row onto your array with each iteration
array_push($response["data"], $data);
}
// Echo your response
echo json_encode($response);