开发者

How can I change the images in a listvue, after it is created?

I have created a program that reads in a file from a server to construct a list view. It then loads an image in the background. Now the image is loaded into an imageview to make sure it works. How can I load the images into my listview imageview rows?

public class Welcome extends ListActivity {
    /** Called when the activity is first created. */
  开发者_JAVA百科  // data to load from html file
    ArrayList<String> images;   
    ArrayList<String> Links;
    ArrayList<String> LinkName;
    ArrayList<String> Price;

    int y;
    ArrayList<String> ted;

    int i;



    String[] items={"CASUAL","DRESSES","CAREER","OUTWEAR","FOOTWEAR",
            "JEWELRY","ACCESSORIES"};
    private HttpClient client;
    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // data read in can be from 1-10
        images=new ArrayList();
        Links=new ArrayList();
        LinkName=new ArrayList();
        Price=new ArrayList();  



        // load in arrays from a file on a web server
        Loaddata();     

        // create a listvue based on the data loaded from the server
    setListAdapter(new IconicAdapter(this));

        // load in biytmap
    download();
    } // end function

    class IconicAdapter extends ArrayAdapter {
        Activity context;

        IconicAdapter(Activity context) {
            super(context, R.layout.row, LinkName);

            this.context=context;
        }

        public View getView(int position, View convertView,
                                                ViewGroup parent) {
            LayoutInflater inflater=context.getLayoutInflater();
            View row=inflater.inflate(R.layout.row, null);
            TextView label=(TextView)row.findViewById(R.id.label);

            label.setText( LinkName.get(position) );
            return(row);
        }
    }   


    private void download(){
     new AsyncTask<Void, Void, Bitmap>(){

       @Override
       protected Bitmap doInBackground(Void... params) {
         HttpClient client = new DefaultHttpClient();
         try {
           String uri = "http://www.besttechsolutions.biz/icon.png";
           HttpGet request = new HttpGet(uri);
           HttpResponse response = client.execute(request);
           return BitmapFactory.decodeStream(response.getEntity().getContent());
         } catch (ClientProtocolException e) {
           e.printStackTrace();
         } catch (IOException e) {
           e.printStackTrace();
         }
         return null;
       }

       @Override
       protected void onPostExecute(Bitmap image) {
         if(image == null){
             Log.d("ted", "could not download image");
//             Toast.makeText(Main.this, "Download failed", Toast.LENGTH_LONG).show();
         }
         else{
             // losd into ListView
    //       ImageView myimage = (ImageView) findViewById(R.id.myimage);
    //       myimage.setImageBitmap(image);
         }
       }
     }.execute();
    }


    if(image == null){
             Log.d("ted", "could not download image");
             getAdapter().setImage(image);
             //refresh
             getAdapter().notifyDataSetChanged();
     }

And add the setImage method to your IconicAdapter:

public void setImage(image){
     this.image = image;
}

You will need a field image in your adapter that you should check in getView and set the ImageView with it in case image!=null.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜