0

In my database I have a row that store links from images from the internet. In the code below, I have taken the name and put in a ListView for each row. Now the KEY_LINK hold the links of the images, how I convert this array to a Bitmap and the change for each KEY_NAME in the ListView?

@SuppressWarnings("deprecation")
    private void populate(){
        Cursor cursor = entry.getAllRows();
        String[] from = new String[] {SQLHelper.KEY_NAME, SQLHelper.KEY_LINK};
        int[] to = new int[] {R.id.textView1, R.id.textView2};
        SimpleCursorAdapter nameAdapter;
        nameAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.item_list, cursor, from, to);
        ListView mylist = (ListView)findViewById(R.id.listView1);
        mylist.setAdapter(nameAdapter);



    }

1 Answer 1

1

android can not use url to set imageView.

the solution to your case is

for each Url String do this 1. create a HTTPGET request to KEY_LINK , this may in a AsyncTask 2. get the connection result. if you confirm it return a image, simply use this method:

    public final static Bitmap getImageFromUrl(final String paramURL) throws InterruptedException, ExecutionException {
    final ExecutorService exec = Executors.newSingleThreadExecutor();
    final Callable<Bitmap> task = new Callable<Bitmap>() {

        @Override
        public Bitmap call() throws Exception {
            {
                final URL uurl = new URL(paramURL);
                final HttpURLConnection urlConnection = (HttpURLConnection) uurl.openConnection();

                final InputStream is = urlConnection.getInputStream();
                final BufferedInputStream mBufferedInputStream = new BufferedInputStream(is);
                final Bitmap mBitmap = BitmapFactory.decodeStream(mBufferedInputStream);
                mBufferedInputStream.close();
                return mBitmap;
            }
        }
    };
    final Future<Bitmap> result = exec.submit(task);
    exec.shutdown();
    return result.get();
    }

then you get all image in local,so you can notify your adapter attached to ListView. override your adapter public View getView(int position, View convertView, ViewGroup parent), according to position ,update your custom ListView item(TextView,ImageView) also. there is a detail guide about how to create custom ListView.

http://www.vogella.com/tutorials/AndroidListView/article.html

Sign up to request clarification or add additional context in comments.

6 Comments

Thats good, but i searched a little more, and found Universal Image Loader, it may suit my needs in that case
wow, wonderful.this project may also helpful for others Universal Image Loader
Anyway, I dont know if I ask here or make another question, but how do I get the contents from a database to a String Array, or a ArrayList?
I'm not familiar with handling database. but you can have a look at Android show Data from Sqlite DB into Grid View,simply change grid view to list view. and a full tutorial 4.3 Database and Data Model at SQLite database.
Thanks, im now using Universal Imagem Loader and I recomend it!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.