java - 来自 ListView 的相同数据被多次打印

标签 java php android listview

这是我的 java Activity 。正在从服务器检索数据。基于 System.out.println("hi"+co.transaction_id),同一项目被打印了 5 次。是我的java问题还是php问题。我尝试编辑我的 ListView xml 高度以匹配父级或填充父级,但它没有帮助。

public class connect4 extends AsyncTask<String, Void, String> {
// public static final String product_id = "product_id";
public static final String RECEIPT_INDEX = "RECEIPT_INDEX";
View view;
Activity activity;
ProductAdapter1 Adapter;
public List<ContactObject> receipt = new ArrayList<>();


Bitmap bitmap;

public connect4(Activity activity, View v, ProductAdapter1 A) {
    this.activity = activity;
    view = v;
    Adapter = A;
}

String convertStreamToString(InputStream is) {
    try {
        return new java.util.Scanner(is).useDelimiter("\\A").next();
    } catch (java.util.NoSuchElementException e) {
        return "";
    }
}
protected String doInBackground(String... arg0) {
    String ipAddress = "http://10.207.140.22/apexStore2/";
    try {
        URL url = new URL(ipAddress +"receipt.php");
        String urlParameters =
                URLEncoder.encode("user_id", "UTF-8") + "=" + 
  URLEncoder.encode(arg0[0], "UTF-8") + "&" +
                        URLEncoder.encode("shipping_name", "UTF-8") + "=" + 
  URLEncoder.encode("???", "UTF-8") + "&" +
                        URLEncoder.encode("shipping_address", "UTF-8") + "="
 + URLEncoder.encode("???", "UTF-8")+ "&" +
                        URLEncoder.encode("shipping_contact", "UTF-8") + "=" 
 + URLEncoder.encode("???", "UTF-8")+ "&" +
                        URLEncoder.encode("shipping_email", "UTF-8") + "=" + 
 URLEncoder.encode("???", "UTF-8")+ "&" +
                        URLEncoder.encode("order_date", "UTF-8") + "=" + 
 URLEncoder.encode("???", "UTF-8")+ "&" +
                        URLEncoder.encode("trx_id", "UTF-8") + "=" + 
 URLEncoder.encode("???", "UTF-8")+ "&" +
                        URLEncoder.encode("tracking_num", "UTF-8") + "=" +
 URLEncoder.encode("???", "UTF-8")+ "&" +
                        URLEncoder.encode("quantity", "UTF-8") + "=" + 
 URLEncoder.encode("???", "UTF-8")+ "&" +
                        URLEncoder.encode("product_id", "UTF-8") + "=" + 
  URLEncoder.encode("???", "UTF-8")+ "&" +
                        URLEncoder.encode("product_title", "UTF-8") + "=" + 
 URLEncoder.encode("???", "UTF-8")+ "&" +
                        URLEncoder.encode("product_price", "UTF-8") + "=" + 
  URLEncoder.encode("???", "UTF-8")+ "&" +
                        URLEncoder.encode("product_img1", "UTF-8") + "=" + 
  URLEncoder.encode("???", "UTF-8");

        HttpURLConnection connection = (HttpURLConnection) 
   url.openConnection();

        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");

        connection.setRequestProperty("Content-Length", "" +
                Integer.toString(urlParameters.getBytes().length));
        connection.setRequestProperty("Content-Language", "en-US");

        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        //Send request
        DataOutputStream wr = new DataOutputStream (
                connection.getOutputStream ());
        wr.writeBytes (urlParameters);
        wr.flush ();
        wr.close ();

        //Get Response
        InputStream is = connection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuffer response = new StringBuffer();
        while((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\r');
        }
        rd.close();
        //System.out.println(response.toString());

        JSONObject mainObject = new JSONObject(response.toString());
        JSONArray uniObject = mainObject.getJSONArray("result");
        for(int i = 0; i < uniObject.length(); i++) {
            ContactObject co = new ContactObject();
            JSONObject rowObject = uniObject.getJSONObject(i);
            //EventObject co = new EventObject();
            //co.user_id = rowObject.getString("user_id");
            co.name = rowObject.getString("shipping_name");
            co.address = rowObject.getString("shipping_address");
            co.contact = rowObject.getString("shipping_contact");
            co.email = rowObject.getString("shipping_email");
            co.orderdate = rowObject.getString("order_date");
            co.transaction_id = rowObject.getString("trx_id");
            co.tracking_id = rowObject.getString("tracking_num");
            co.quantity = rowObject.getString("quantity");
            co.product_id = rowObject.getString("product_id");
            co.product_title = rowObject.getString("product_title");
            co.product_price = rowObject.getString("product_price");
            co.img1 = ipAddress +"img/products/" + 
rowObject.getString("product_img1");
            //mContentItems.add(co);
            System.out.println("hi" +co.transaction_id);
            receipt.add(new ContactObject(co.name, co.address, co.contact, 
co.email, co.orderdate, co.transaction_id, co.tracking_id, co.quantity, 
co.product_id, co.product_title, co.product_price, co.img1));
            //         userLogin(co.product_id);
        }

        //To further break down JSON
        //JSONObject oneObject = mainObject.getJSONObject("1");
        //String id = oneObject.getJSONObject("id");
        try{

        }
        finally{
            connection.disconnect();
        }
    } catch (Exception e){
        System.out.println(e.toString());
    }
    return "";
}

protected void onPreExecute(){

}

@Override
protected void onPostExecute(String result){
    Adapter.receipt = receipt;
    Adapter.notifyDataSetChanged();
}



private class LoadImage extends AsyncTask<String, String, Bitmap> {
    ImageView img;
    public LoadImage(ImageView img){
        this.img = img;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }
    protected Bitmap doInBackground(String... args) {
        try {
            bitmap = BitmapFactory.decodeStream((InputStream) new 
URL(args[0]).getContent());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    protected void onPostExecute(Bitmap image) {

        if(image != null){
            img.setImageBitmap(image);
        }else{
        }
    }
}
}

这是我的 php 脚本。

<?php
 include ('classes/functions.php');

 if(isset($_POST['user_id'])){
$user_id = $_POST['user_id'];
$check_receipt = "select  si.shipping_name,
    si.shipping_address,
    si.shipping_contact,
    si.shipping_email,
    o.order_date,
    o.trx_id,
    o.tracking_num,
    o.quantity,
    o.product_id,
    p.product_title,
    p.product_price,
    p.product_img1
 from shipping_infos si
 inner join orders o
 on si.user_id = o.user_id inner join products p on p.product_id =    
 o.product_id   
 where si.user_id='".$user_id."';";

        $run_receipt_checking = mysqli_query($con, $check_receipt);
        $result = array();
    while($row = mysqli_fetch_array($run_receipt_checking)){
    array_push($result,
    array(
          'shipping_name'=>$row[0],
          'shipping_address'=>$row[1],
          'shipping_contact'=>$row[2],
          'shipping_email'=>$row[3],
          'order_date'=>$row[4],
          'trx_id'=>$row[5],
          'tracking_num'=>$row[6],  
          'quantity'=>$row[7],
          'product_id'=>$row[8],
          'product_title'=>$row[9],           
          'product_price'=>$row[10],
          'product_img1'=>$row[11]            

));
}
echo json_encode(array("result"=>$result));
 }
?>

最佳答案

我不确定 100%,但我在处理自定义适配器时遇到了同样的问题。我的问题已通过更正自定义适配器的 getView() 中的代码得到解决。

希望这有帮助。

关于java - 来自 ListView 的相同数据被多次打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36710279/

相关文章:

Java Regex - 掩码信用卡号

java - 在 android 中使用 okhttp 在 webview 中发送每个请求的授权 header

java - 未找到 JPA 构造函数类 [Emp] - oracle.toplink.essentials.exceptions.EJBQLException

php - 如何查找日期是否有效且不大于 2038 年?

php - 如何避免在 codeigniter Controller 函数上调用多个模型?

php - 在 php 运行时包含来自外部站点的数据

android - 如何检测用户是否链接到 Google Market 帐户

android - 如何将 HTML 文本转换为字符串并将其设置为 Android 中的 TextView?

java - 漂亮的 HTML5 输出格式

java - Java 线程转储中的 defined_classes 是什么?