繁体   English   中英

从自定义适配器引用AsyncTask

[英]Reference To AsyncTask from Custom Adapter

我正在尝试通过ID加载Facebook照片,并使用我的自定义适配器将其放置在ListView中。

我的问题是在MyAdapter中,无法将mIcon1解析为变量,而在LoadAllProducts中,未使用mIcon1。

显然,我没有在适配器中正确引用mIcon1,或者我没有在LoadAllProducts中正确设置它,但是我不知道哪一个是错误的!

任何帮助表示赞赏! 谢谢!

LoadAllProducts AsyncTask:

public class LoadAllProducts extends AsyncTask<String, String, String> {

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

    }



    /**
     * getting All products from url
     * */
    protected String doInBackground(String... args) {

        String fbid = fbID;
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("fbid", fbid));

        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_all_videos, "POST", params);

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


        //GETTING PROFILE PICTURE START-----------------------//


        URL img_value = null;
        try {
            img_value = new URL("http://graph.facebook.com/"+TAG_FACEBOOKID+"/picture?width=100&height=100");
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
            }

        try {
            Bitmap mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
        } catch (IOException e1) {
            e1.printStackTrace();
        }

       //GETTING PROFILE PICTURE END-----------------------//

        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 facebookid = c.getString(TAG_FACEBOOKID);
                    String to_user = c.getString(TAG_TO_USER);
                    String available = c.getString(TAG_AVAILABLE);

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

                    // adding each child node to HashMap key => value
                    map.put(TAG_FACEBOOKID, facebookid);
                    map.put(TAG_TO_USER, to_user);
                    map.put(TAG_AVAILABLE, available);

                    // adding HashList to ArrayList
                    productsList.add(map);
                }
            } else {
                // no products found
                // Launch Add New product Activity
                Intent i = new Intent(getApplicationContext(),
                        NewProductActivity.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
                 * */
                MyAdapter adapter = new MyAdapter(AllProductsActivity.this, R.layout.list_item, productsList);

                // updating listview
                setListAdapter(adapter);

            }

        });

    }

}

MyAdapter:

public class MyAdapter extends ArrayAdapter<HashMap<String, String>> {

    Context context;
    int resourceId;
    LayoutInflater inflater;

    ArrayList<HashMap<String, String>>  items;
    public MyAdapter (Context context, int resourceId, ArrayList<HashMap<String, String>> items)
    {
        super(context, resourceId, items);
        this.items =items;
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        final ViewHolder holder;
        if (convertView == null){

            convertView = inflater.inflate(R.layout.list_item, null);

            holder = new ViewHolder();
            holder.name = (TextView)convertView.findViewById(R.id.name);
            holder.userpicture=(ImageView)findViewById(R.id.userpicture);
            convertView.setTag(holder);

        } else {

            holder = (ViewHolder)convertView.getTag();
        }

        HashMap<String,String> item = (HashMap<String,String> ) items.get(position);
        if (item != null)
        {

            // This is where you set up the views.
            holder.name.setText(item.get(TAG_FACEBOOKID));
            holder.userpicture.setImageBitmap(mIcon1);

        }

        return convertView;
    }

    public class ViewHolder
    {
        TextView    name;
        ImageView userpicture;
    }
}

您没有在holder.userpicture.setImageBitmap(mIcon1);放置任何引用holder.userpicture.setImageBitmap(mIcon1); 因此编译器会告诉您变量mIcon1不存在,请进行设置。

同样Bitmap mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream()); 没有人可以访问mIcon1范围之外的mIcon1。

解:

使MyAdapter作为内蒙古的LoadAllProducts并创建内部的全局变量LoadAllProducts这是Bitmap mIcon1并在后台线程,并在适配器使用

暂无
暂无

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

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