收到了多个值但是当我将JSON解析为Android时,它只显示第一个值。
这是JsonNew.php文件:
require "index.php";
$user_shopID = $_POST["UniqueKey"] ;
$sql_query="Select * from products where ShopID like '$user_shopID';";
$result = mysqli_query($con, $sql_query);
$response = array();
while ($row = mysqli_fetch_array($result))
{
array_push($response,array("ProductID"=>$row[0],"Product Name"=>$row[1], "Buying Price"=>$row[3], "Selling Price"=>$row[4]));
}
echo json_encode(array("server_response"=>$response));
mysqli_close($con);
这是ContactAdapters.java:
public class ContactAdapter extends ArrayAdapter {
List list = new ArrayList();
public ContactAdapter(@NonNull Context context, @LayoutRes int resource) {
super(context, resource);
}
public void add(Contacts object) {
super.add(object);
list.add(object);
}
@Override
public int getCount() {
return list.size();
}
@Nullable
@Override
public Object getItem(int position) {
return list.get(position);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View row;
row = convertView;
ContactHolder contactHolder;
if (row == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.row_layout, parent, false);
contactHolder = new ContactHolder();
contactHolder.tx_id = (TextView) row.findViewById(R.id.id);
contactHolder.tx_name = (TextView) row.findViewById(R.id.name);
contactHolder.tx_buy = (TextView) row.findViewById(R.id.buy);
contactHolder.tx_sell = (TextView) row.findViewById(R.id.sell);
row.setTag(contactHolder);
} else {
contactHolder = (ContactHolder) row.getTag();
}
Contacts contacts = (Contacts) this.getItem(position);
contactHolder.tx_id.setText(contacts.getId());
contactHolder.tx_name.setText(contacts.getName());
contactHolder.tx_buy.setText(contacts.getBuy());
contactHolder.tx_sell.setText(contacts.getSell());
return row;
}
static class ContactHolder {
TextView tx_id, tx_name, tx_buy, tx_sell;
}
}
这就是row_layout.xml
android:layout_width="match_parent"
android:layout_height="75dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/id"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:text="TextView"
android:gravity="center"
android:textColor="#000000"
android:layout_below="@+id/textView13" />
<TextView
android:id="@+id/name"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/id"
android:text="TextView"
android:gravity="center"
android:textColor="#000000"
android:layout_below="@+id/textView14" />
<TextView
android:id="@+id/buy"
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="TextView"
android:gravity="center"
android:textColor="#000000"
android:layout_toRightOf="@+id/name"
android:layout_toEndOf="@+id/name"
android:layout_alignTop="@+id/name" />
<TextView
android:id="@+id/sell"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/buy"
android:text="TextView"
android:gravity="center"
android:textColor="#000000"
android:layout_alignTop="@+id/buy"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
这是DisplayProductsListview.java
listview = (ListView) findViewById(R.id.listview);
contactAdapter = new ContactAdapter(this,R.layout.row_layout);
listview.setAdapter(contactAdapter);
json_string = getIntent().getExtras().getString("json_data");
try {
jsonObject = new JSONObject(json_string);
jsonArray = jsonObject.getJSONArray("server_response");
int count=0;
String id, name, buy, sell;
while (count<jsonObject.length()){
JSONObject JO = jsonArray.getJSONObject(count);
id = JO.getString("ProductID");
name = JO.getString("Product Name");
buy = JO.getString("Buying Price");
sell = JO.getString("Selling Price");
Contacts contacts = new Contacts(id,name,buy,sell);
contactAdapter.add(contacts);
count++;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
这是Contacts.java
private String id, name, buy, sell;
public Contacts(String id, String name, String buy, String sell){
this.setId(id);
this.setName(name);
this.setBuy(buy);
this.setSell(sell);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBuy() {
return buy;
}
public void setBuy(String buy) {
this.buy = buy;
}
public String getSell() {
return sell;
}
public void setSell(String sell) {
this.sell = sell;
}
}
当我从服务器获取JSON数据时,它们完美无缺,但是当我将它们显示在列表视图中时,这些数据不会出现。只有第一个数据及其值进入列表视图。
以下是截图:
答案 0 :(得分:0)
在contactAdapter类中,您必须创建一个ArrayList。
ArrayList<Contact> arrayList = new ArrayList<>();
你必须这样做。
答案 1 :(得分:0)
更改此功能
while (count<jsonArray.length()){
JSONObject JO = jsonArray.getJSONObject(count);
id = JO.getString("ProductID");
name = JO.getString("Product Name");
buy = JO.getString("Buying Price");
sell = JO.getString("Selling Price");
Contacts contacts = new Contacts(id,name,buy,sell);
contactAdapter.add(contacts);
count++;
}
你必须使用jsonarray长度而不是对象长度。