我正在尝试按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;
}
}
答案 0 :(得分:0)
您没有在holder.userpicture.setImageBitmap(mIcon1);
中添加任何引用,因此编译器会告诉您该变量mIcon1
不存在请完成。
同样在Bitmap mIcon1 = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
中,没有人可以访问doInbackground范围之外的mIcon1
。
<强>解决方案:强>
将MyAdapter
作为LoadAllProducts
的内部,并在LoadAllProducts
Bitmap mIcon1
内创建一个全局变量,并在后台线程中使用它适配器