繁体   English   中英

如何使用数据库中的数据填充列表视图

[英]How do i populate my list view with data from DB

我有一个下面的适配器的列表视图:

public class ListViewAdapter extends ArrayAdapter<Product> {
public ListViewAdapter(Context context, int resource, List<Product> objects){
    super (context, resource, objects);
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent){
    View v = convertView;

    if(null == v) {
        LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.list_item, null);
    }

    Product product = getItem(position);
    ImageView img = (ImageView) v.findViewById(R.id.imageView3);
    TextView txtTitle = (TextView) v.findViewById(R.id.txtTitle);
    TextView txtDescription = (TextView) v.findViewById(R.id.txtDescription);
    TextView txtDateandTime = (TextView) v.findViewById(R.id.txtDateandTime);
    TextView txtLocation = (TextView) v.findViewById(R.id.txtLocation);


    img.setImageResource(product.getImageId());
    txtTitle.setText(product.getTitle());
    txtDescription.setText(product.getDescription());
    txtDateandTime.setText(product.getDateandTime());
    txtLocation.setText(product.getLocation());

    return v;
}


                 AsyncTask.execute(new Runnable() {
           @Override
           public void run() {
              //TODO your background code
            //pseudo code to get product, replace this code to get real products from db
                productList = new ArrayList<>();


                //Create a JSON parser Instance ----- Used JSON parser from Android
                JSONparser jParser=new JSONparser();

                //Getting JSON string from URL ------ Used JSON Array from Android
                JSONArray rows=jParser.getJSONFromUrl(url);

                try{
                    for(int i=0;i < rows.length();i++){
                        JSONObject e = rows.getJSONObject(i);

                        productList.add(new Product(e.optInt("id"),
                                                    e.optString("title"),
                                                    e.optString("price"),
                                                    e.optString("decription"),
                                                    e.optString("location")));
                    }
                }
                catch (JSONException e) {
                    //Log.
                    //
                    //
                    //
                    //
                    // e("Error", "json "+e.toString());
                }


                return productList;

使用以下方法填充列表视图(当前正在手动填充)

  public List<Product> getProductList() {
    //pseudo code to get product, replace this code to get real products from db
    productList = new ArrayList<>();
    productList.add(new Product(R.drawable.productimage, "Title 1 just to make", "This is description 1 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
    productList.add(new Product(R.drawable.productimage, "Title 2", "This is description 2 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
    productList.add(new Product(R.drawable.productimage, "Title 3", "This is description 3 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
    productList.add(new Product(R.drawable.productimage, "Title 4", "This is description 4 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
    productList.add(new Product(R.drawable.productimage, "Title 5", "This is description 5 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
    productList.add(new Product(R.drawable.productimage, "Title 6", "This is description 6 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
    productList.add(new Product(R.drawable.productimage, "Title 7", "This is description 7 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
    productList.add(new Product(R.drawable.productimage, "Title 8", "This is description 8 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
    productList.add(new Product(R.drawable.productimage, "Title 9", "This is description 9 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
    productList.add(new Product(R.drawable.productimage, "Title 10", "This is description 10 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
    productList.add(new Product(R.drawable.productimage, "Title 11", "This is description 11 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
    productList.add(new Product(R.drawable.productimage, "Title 12", "This is description 12 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
    productList.add(new Product(R.drawable.productimage, "Title 13", "This is description 13 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
    productList.add(new Product(R.drawable.productimage, "Title 14", "This is description 14 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
    productList.add(new Product(R.drawable.productimage, "Title 15", "This is description 15 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));
    productList.add(new Product(R.drawable.productimage, "Title 16", "This is description 16 but let me add more text in the description let see what happens when you keep writing", "Today 5:09pm", "Main Camp"));

    return productList;
}

我想用我数据库中的数据填充; 我已经尝试了下面的PHP服务器和代码,但没有工作。 谢谢你的帮助。 get_all_products.php

        <?php

        /*
         * Following code will list all the products
         */

        // array for JSON response
        $response = array();

         //connect to db
         $con=mysqli_connect("localhost","root","","ujmartdb");


        // get all products from products table
        $result = mysqli_query($con,"SELECT *FROM products") or die(mysql_error());

        // check for empty result
        if (mysql_num_rows($result) > 0) {
            // looping through all results
            // products node
            $response["products"] = array();

            while ($row = mysql_fetch_array($result)) {
                // temp user array
                $product = array();
                $product["id"] = $row["id"];
                $product["title"] = $row["title"];
                $product["price"] = $row["price"];
                $product["description"] = $row["description"];
                $product["location"] = $row["location"];


                // push single product into final response array
                array_push($response["products"], $product);
            }
            // success
            $response["success"] = 1;

            // echoing JSON response
            echo json_encode($response);
        } else {
            // no products found
            $response["success"] = 0;
            $response["message"] = "No products found";

            // echo no users JSON
            echo json_encode($response);
        }
        ?>



           AsyncTask.execute(new Runnable() {
       @Override
       public void run() {
          //TODO your background code
        //pseudo code to get product, replace this code to get real products from db
            productList = new ArrayList<>();


            //Create a JSON parser Instance ----- Used JSON parser from Android
            JSONparser jParser=new JSONparser();

            //Getting JSON string from URL ------ Used JSON Array from Android
            JSONArray rows=jParser.getJSONFromUrl(url);

            try{
                for(int i=0;i < rows.length();i++){
                    JSONObject e = rows.getJSONObject(i);

                    productList.add(new Product(e.optInt("id"),
                                                e.optString("title"),
                                                e.optString("price"),
                                                e.optString("decription"),
                                                e.optString("location")));
                }
            }
            catch (JSONException e) {
                //Log.
                //
                //
                //
                //
                // e("Error", "json "+e.toString());
            }


            return productList;

可能是您有JSONException,这是您无法捕获的,因为您将catch块留为空白

try{
                    for(int i=0;i < rows.length();i++){
                        JSONObject e = rows.getJSONObject(i);

                        productList.add(new Product(e.optInt("id"),
                                                    e.optString("title"),
                                                    e.optString("price"),
                                                    e.optString("decription"),
                                                    e.optString("location")));
                    }
                }
                catch (JSONException e) {
                        e.printStackTrace();
                    //Log.
                    //
                    //
                    //
                    //
                    // e("Error", "json "+e.toString());
                }

并检查堆栈跟踪是否在Catch块中。

还要检查它是否返回JSOn(如果不是,则为JSON格式)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM