我无法理解为什么我从中获取null listview,因为我用邮递员尝试了我的网址。我认为我的Android部分有一些问题,但我不知道问题出在哪里。
查看订单活动
public class ViewOrderActivity extends AppCompatActivity {
private ListView listView3;
private CustomAdapter2 listAdapter2;
ArrayList<Order> orderlist = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_order);
User user = SharedPrefManager.getInstance(this).getUser();
String str_oid=String.valueOf(user.getId());
String type="cancel";
BackgroundWorker backgroundWorker=new BackgroundWorker(this);
backgroundWorker.execute(type,str_oid);
listView3 = (ListView) findViewById(R.id.listView3);
getJSON("http://192.168.28.1/restaurant/getorder.php");
}
private void getJSON(final String urlWebService) {
class GetJSON extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
loadIntoListView(s);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL(urlWebService);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "\n");
}
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
}
GetJSON getJSON = new GetJSON();
getJSON.execute();
}
private void loadIntoListView(String json) throws JSONException {
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
orderlist.add(new Order(obj.getInt("orderid"), obj.getInt("pickuptime"), obj.getInt("pickupdate")));
}
listAdapter2 = new CustomAdapter2(this, orderlist);
listView3.setAdapter(listAdapter2);
}
}
这是自定义适配器,其中列表视图是结构化的。
自定义适配器
class CustomAdapter2 extends BaseAdapter {
public ArrayList<Order> ordlists;
private Context context;
CustomAdapter2(Context context, ArrayList<Order> ordlists) {
//super(context, R.layout.item_food, strilist);
this.context = context;
this.ordlists = ordlists;
}
@Override
public int getCount(){
return ordlists.size();
}
@Override
public Order getItem(int position){
return ordlists.get(position);
}
@Override
public long getItemId(int position){
return 0;
}
@NonNull
@Override
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
final ListViewHolder2 listViewHolder2;
View customView2;
if(convertView == null)
{
LayoutInflater menuInflater2 = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// LayoutInflater menuInflater = LayoutInflater.from(getContext());
customView2 = menuInflater2.inflate(R.layout.item_order, parent, false);
listViewHolder2 = new ListViewHolder2();
listViewHolder2.tvorderid = (TextView) customView2.findViewById(R.id.tvorderid );
listViewHolder2.tvtime = (TextView) customView2.findViewById(R.id.tvtime);
listViewHolder2.tvdate = (TextView) customView2.findViewById(R.id.tvdate);
listViewHolder2.btncancel = (Button) customView2.findViewById(R.id.btncancel);
customView2.setTag(listViewHolder2);
}
else
{
customView2=convertView;
listViewHolder2= (ListViewHolder2) customView2.getTag();
}
final Order order=getItem(position);
//Typeface customfont= Typeface.createFromAsset(parent.getContext().getAssets(),"fonts");
listViewHolder2.tvorderid.setText(String.valueOf(order.getOrderid()));
listViewHolder2.tvorderid.setTextColor(Color.YELLOW);
listViewHolder2.tvorderid.setTextSize(10);
listViewHolder2.tvtime.setText(String.valueOf(order.getPickuptime()));
listViewHolder2.tvtime.setTextColor(Color.YELLOW);
listViewHolder2.tvtime.setTextSize(10);
listViewHolder2.tvdate.setText(String.valueOf(order.getPickupdate()));
listViewHolder2.tvdate.setTextColor(Color.YELLOW);
listViewHolder2.tvdate.setTextSize(10);
listViewHolder2.btncancel.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
}
});
return customView2;
}
取消类型的后台工作人员
else if (type.equals("cancel")){
try{
String id=params[1];
URL url=new URL(cancel_url);
HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream=httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String post_data= URLEncoder.encode("id","UTF-8")+"="+URLEncoder.encode(id,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream=httpURLConnection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line=bufferedReader.readLine())!=null){
result+=line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
}catch(MalformedURLException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
这是我的url getorder.php
的代码<?php
$hostname = "localhost";
$username = "root";
$password = "";
$database = "y77";
$conn = new mysqli($hostname, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$orders = array();
session_start();
$id=$_POST["id"];
$stmt =$conn->prepare("SELECT `orderid`, `pickuptime`, `pickupdate` FROM `order` WHERE `id`=$id");
$stmt->execute();
$stmt->bind_result($orderid, $pickuptime, $pickupdate);
while($stmt->fetch()){
$temp = [
'orderid'=>$orderid,
'pickuptime'=>$pickuptime,
'pickupdate'=>$pickupdate
];
array_push($orders, $temp);
}
echo json_encode($orders);
?>