如何在Android应用程序中使用php数据库中的图像和文本填充listview

时间:2015-08-31 13:34:15

标签: android mysql listview

我想在我的Android应用程序的listview中显示我的php,mysql数据库中的图像和标题...

我现在已经厌倦了为此寻找代码但是ddnt得到正确的代码..我尝试了很多不同的方法但没有获得成功..我也听说过毕加索但不知道如何使用它与php数据库来获取图像在listview中使用毕加索..

我已尝试过以下代码但无法找到结果..

public class GetThisCategoryProductsActivity extends ListActivity {


    ImageView image;     Bitmap bitmapOrg;

    String ctitle;

    private ProgressDialog pDialog;

    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();

    ArrayList<HashMap<String, String>> productsList;


    // url to get all products list
    private static final String url_all_products = "http://xx.xxx.xx/AndroidApp/get_all_products.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PRODUCTS = "products";
    private static final String TAG_PID = "pid";
    private static final String TAG_TITLE = "title";
    private static final String TAG_IMAGE = "image";
    private static final String TAG_PRICE = "price";

    private static final String TAG_CTITLE = "ctitle";


    // products JSONArray
    JSONArray products = null;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        image = (ImageView) findViewById(R.id.image);
        setContentView(R.layout.activity_get_this_category_products);



        Intent i = getIntent();
        ctitle = i.getStringExtra(TAG_CTITLE);



        // Hashmap for ListView
        productsList = new ArrayList<HashMap<String, String>>();

        // Loading products in Background Thread
        new LoadAllProducts().execute(url_all_products);

        // Get listview
        ListView lv = getListView();

        // on seleting single product
        // launching Edit Product Screen
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // getting values from selected ListItem
                String pid = ((TextView) view.findViewById(R.id.pid)).getText()
                        .toString();

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        GetThisProductActivity.class);
                // sending pid to next activity
                in.putExtra(TAG_PID, pid);

                // starting new activity and expecting some response back
                startActivityForResult(in, 100);
            }
        });


    }



    public void post(View view) {
        Intent intent = new Intent(this, PostAdActivity.class);

        intent.putExtra("ctitle", ctitle);
        startActivity(intent);
    }

    // Response from Edit Product Activity
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // if result code 100
        if (resultCode == 100) {
            // if result code 100 is received
            // means user edited/deleted product
            // reload this screen again
            Intent intent = getIntent();
            finish();
            startActivity(intent);
        }

    }



    /**
     * Background Async Task to Load all product by making HTTP Request
     * */
    class LoadAllProducts extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(GetThisCategoryProductsActivity.this);
            pDialog.setMessage("Loading. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }



        /**
         * getting All products from url
         * */
        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("ctitle", ctitle));
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);


            // Check your log cat for JSON reponse
            Log.d("All Entries: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // products found
                    // Getting Array of Products
                    products = json.getJSONArray(TAG_PRODUCTS);

                    // looping through All Products
                    for (int i = 0; i < products.length(); i++) {
                        JSONObject c = products.getJSONObject(i);

                        // Storing each json item in variable
                        String pid = c.getString(TAG_PID);
                        String title = c.getString(TAG_TITLE);
                        String ctitle = c.getString(TAG_CTITLE);
                        String price = c.getString(TAG_PRICE);
                        String str1 = c.getString(TAG_IMAGE);
                        byte[] ba2 = Base64.decode(str1 ,Base64.DEFAULT);
                        Bitmap bitmap = BitmapFactory.decodeByteArray(ba2, 0, ba2.length);
                        Drawable d = new BitmapDrawable(getResources(),bitmap);





                                                // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_PID, pid);
                        map.put(TAG_TITLE, title);
                        map.put(TAG_CTITLE, ctitle);
                        map.put(TAG_PRICE, price);
                        map.put(TAG_IMAGE, String.valueOf(d));



                        // adding HashList to ArrayList
                        productsList.add(map);
                    }
                } else {
                    // no products found
                    // Launch Add New product Activity
                    Intent i = new Intent(getApplicationContext(),
                            MainActivity.class);
                    // Closing all previous activities
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }



        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            pDialog.dismiss();

            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(



                            GetThisCategoryProductsActivity.this, productsList,
                            R.layout.list_cat_ad, new String[]{TAG_PID,
                            TAG_TITLE, TAG_CTITLE, TAG_IMAGE, TAG_PRICE},
                            new int[]{R.id.pid, R.id.title, R.id.ctitle,  R.id.image,  R.id.price});


                    // updating listview
                    setListAdapter(adapter);



                }
            });

        }

    }





}

任何人都可以帮助我

0 个答案:

没有答案