I have GridViewwith pictures / texts downloaded from different servers (I only know the URL). I tried changing my code based on the ryac tutorial .
In my Activity file, I set GAdapterto my GridViewas follows:
GridView mGridMain = (GridView)findViewById(R.id.gvMain);
mGridMain.setAdapter(new GAdapter(this, listAppInfo));
I have changed my own adapter and will try to adapt it:
public class GAdapter extends BaseAdapter {
private Context mContext;
private List<SiteStaff> mListAppInfo;
private HashMap<Integer, ImageView> views;
public GAdapter(Context context, List<SiteStaff> list) {
mContext = context;
mListAppInfo = list;
}
@Override
public int getCount() {
return mListAppInfo.size();
}
@Override
public Object getItem(int position) {
return mListAppInfo.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SiteStaff entry = mListAppInfo.get(position);
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.list_g, null);
}
ImageView v;
v = (ImageView)convertView.findViewById(R.id.ivIcon);
v.setImageResource(android.R.drawable.ic_menu_gallery);
TextView tvName = (TextView)convertView.findViewById(R.id.tvName);
tvName.setText(entry.getName());
Bundle b = new Bundle ();
b.putString("file", "http://www.test.com/mypict.jpg");
b.putInt("pos", position);
new LoadImage().execute(b);
views.put(position, v);
return v;
}
private class LoadImage extends AsyncTask<Bundle, Void, Bundle> {
@Override
protected Bundle doInBackground(Bundle... b) {
String file = b[0].getString("file");
URL UrlImage;
Bitmap bm=null;
try {
UrlImage = new URL (file);
HttpURLConnection connection;
connection = (HttpURLConnection) UrlImage.openConnection();
bm= BitmapFactory.decodeStream(connection.getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Bundle bundle = new Bundle();
bundle.putParcelable("bm", bm);
bundle.putInt("pos", b[0].getInt("pos"));
bundle.putString("file", file);
return bundle;
}
@Override
protected void onPostExecute(Bundle result) {
super.onPostExecute(result);
Log.d("test", "*after: " + result.getInt("pos") + " | " + result.getString("file"));
ImageView view = views.get(result.getInt("pos"));
view.setImageBitmap((Bitmap) result.getParcelable("bm"));
}
}
}
But the following error is displayed:
11-16 06:16:08.285: E/AndroidRuntime(517): FATAL EXCEPTION: main
11-16 06:16:08.285: E/AndroidRuntime(517): java.lang.NullPointerException
11-16 06:16:08.285: E/AndroidRuntime(517): at common.adapter.GAdapter.getView(GAdapter.java:139)
11-16 06:16:08.285: E/AndroidRuntime(517): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
11-16 06:16:08.285: E/AndroidRuntime(517): at android.widget.GridView.onMeasure(GridView.java:932)
11-16 06:16:08.285: E/AndroidRuntime(517): at android.view.View.measure(View.java:8171)
11-16 06:16:08.285: E/AndroidRuntime(517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
11-16 06:16:08.285: E/AndroidRuntime(517): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
11-16 06:16:08.285: E/AndroidRuntime(517): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
11-16 06:16:08.285: E/AndroidRuntime(517): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
11-16 06:16:08.285: E/AndroidRuntime(517): at android.view.View.measure(View.java:8171)
11-16 06:16:08.285: E/AndroidRuntime(517): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
11-16 06:16:08.285: E/AndroidRuntime(517): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
11-16 06:16:08.285: E/AndroidRuntime(517): at android.view.View.measure(View.java:8171)
So far, I have never used Threads in Android. I would be grateful if someone could help me.
source
share