1

Can Anyone help me for this questions? I want the custom listview can load photo in background, so that it can reduce some response time.

Besides, can anyone tell me that if i want to load more record by using this custom listview, how can it be acheived?

Thank you!

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    StrictMode.ThreadPolicy policy = new        StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy); 
    setContentView(R.layout.activity);
    //ProgressDialog dialog = ProgressDialog.show(this, "", "Loading...", true);
    //new Thread(new Runnable() {
       // public void run() {
        list = (ListView) findViewById(R.id.MyListView);    mylist = new ArrayList<HashMap<String, Object>>(); 





        try{
        LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);

        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, true);
       Location location = locationManager.getLastKnownLocation(provider);
        lat = location.getLatitude();
        lon = location.getLongitude();
       mylist=latlng(mylist,lat,lon,From);
        }catch(Exception e){
            //ShowMsgDialog(e.toString());

        }
        mSchedule = new SimpleAdapter(this, mylist,  R.layout.texteest,new String[] {"textView1","imageView1","textView3"},new int[] {R.id.textView1,R.id.imageView1,R.id.textView3});  
        mSchedule.setViewBinder(new MyViewBinder());
            list.setAdapter(mSchedule);  
            Button btn_map;
            btn_map=(Button)findViewById(R.id.button1);

            list.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,int position, long id) {



                    Intent i = new Intent(this,"other_java".class);
                    startActivity(i);
                }
        });
            btn_map.setOnClickListener(new ImageButton.OnClickListener(){   
            public void onClick(View v) {
                //mapresult = Map.showmap;

                    Intent i = new Intent(this,Map.class);
                    startActivity(i);
                }});

        }


private ArrayList<HashMap<String, Object>> latlng(ArrayList<HashMap<String, Object>> mylist,double lat,double lon, int from2) {

        try{

            ArrayList<NameValuePair> params =new ArrayList<NameValuePair>();    
               params.add(new BasicNameValuePair("lat",String.valueOf(lat)));
              params.add(new BasicNameValuePair("lng",String.valueOf(lon)));
             params.add(new BasicNameValuePair("fromwhere",String.valueOf(From)));

               result = httprequestjava.executeQuery(params);


       mapresult = result;
      result = result.substring(1);

       JSONArray jsonArray = new JSONArray(result);


    //   result = "";
       for(int i = 0; i < jsonArray.length(); i++) {
           JSONObject jsonData = jsonArray.getJSONObject(i);

            HashMap<String, Object> map = new HashMap<String, Object>();  
            map.put("textView1",jsonData.getString("somecontent"));  
          myUrl = new URL(jsonData.getString("img_src"));
               InputStream inputStream = (InputStream)myUrl.getContent();
               Drawable drawable = Drawable.createFromStream(inputStream, null);
               Bitmap mIcon1 =BitmapFactory.decodeStream(myUrl.openConnection().getInputStream());
           map.put("imageView1",mIcon1);  
           map.put("textView3",jsonData.getString("distance")+"m");
            //map.put("ItemText", "This is text.....");  
           //type is not yet done
            mylist.add(map);


       }
       }catch(Exception e){
           Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_SHORT).show();
       }

        return mylist;

}



class MyViewBinder implements ViewBinder
{
    @Override
    public boolean setViewValue(View view, Object data,
            String textRepresentation) {
        if((view instanceof ImageView)&(data instanceof Bitmap))
        {
            try{
            ImageView iv = (ImageView)view;
            Bitmap bmp = (Bitmap)data;
            iv.setImageBitmap(bmp);
            }catch(Exception e)
            {
                 Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_SHORT).show();
            }
            return true;
        }
        return false;
    }


}

//auto gen

}

LazyP
  • 33
  • 5

1 Answers1

0

Use Universal Image Loader library to make your image downloading task easier. It will make your task a lot more simpler and will help keep your code clean.

Aman Agnihotri
  • 2,973
  • 1
  • 18
  • 22
  • How can i run it? Eclipse said that it contains error but i can't find any of them – LazyP Feb 23 '14 at 10:17
  • First of all, you're trying to do network tasks on the UI thread which isn't proper for production quality code. Import and use the [UIL](https://github.com/nostra13/Android-Universal-Image-Loader) library in your project, and learn about [AsyncTask](http://developer.android.com/reference/android/os/AsyncTask.html) and you'll be able to write much better and cleaner and correct code. – Aman Agnihotri Feb 23 '14 at 10:20