简体   繁体   中英

How to show multiple images in ImageView

I want to show image with text by rowwise.here is my xml file

and In RowItem Class contains im seting Bit Map object

Now im using hardcoded String array but i want to set using String array of URL I fetch this url form network using httpconnection also im added permisions in andoridManifest.xml file

im confusing im not geting any correct solution plz help me

i reffer this url for showing images http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-arrayadapter/ i want to do same only use url rather than hardcoded images

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <ImageView
            android:id="@+id/icon"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:contentDescription="@string/image"
            android:paddingLeft="10dp"
            android:paddingRight="10dp" />

        <TextView
            android:id="@+id/desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/title"
            android:layout_toRightOf="@+id/icon"
            android:paddingLeft="10dp"
            android:paddingTop="20dp"
            android:textColor="#3399FF"
            android:textSize="14dp" />
    </RelativeLayout>

My Java code

 import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import org.apache.http.HttpResponse;
    import org.apache.http.HttpStatus;
    import org.apache.http.StatusLine;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ListView;

    public class BdaylistActivity extends Activity implements
            OnItemClickListener {
        private ProgressDialog m_ProgressDialog = null;
         ListView listView;
            List<RowItem> rowItems;



        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_bdaylist);


            m_ProgressDialog = ProgressDialog.show(this,    
                    "Please wait...", "Retrieving data ...", true);
            new BirthdayTask().execute("https://gdirectoryqa.appspot.com/gDirectory/get_user_profile.htm");


        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {


            Intent intent = new Intent(this, ProfileViewActivity.class);
            intent.putExtra("BDay", true);
                   startActivity(intent);
        }


        class BirthdayTask extends AsyncTask<String, String, String>{

            @Override
            protected String doInBackground(String... uri) {
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response;
                String responseString = null;
                try {
                    response = httpclient.execute(new HttpGet(uri[0]));
                    StatusLine statusLine = response.getStatusLine();
                    if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                        ByteArrayOutputStream out = new ByteArrayOutputStream();
                        response.getEntity().writeTo(out);
                        out.close();
                        responseString = out.toString();
                    } else{
                        //Closes the connection.
                        response.getEntity().getContent().close();
                        throw new IOException(statusLine.getReasonPhrase());
                    }
                } catch (ClientProtocolException e) {
                    //TODO Handle problems..
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return responseString;
            }


            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);

                JSONArray jArr;
                ArrayList<String> firstLastNames=new ArrayList<String>();
                ArrayList<Integer> images=new ArrayList<Integer>();
                try {
                    jArr = new JSONArray(result);
                    for (int count = 0; count < jArr.length(); count++) {
                        JSONObject obj = jArr.getJSONObject(count);
                        JSONObject userGeneralInfoArray = jArr.getJSONObject(count).getJSONObject("userGeneralInfo"); 
                        firstLastNames.add(userGeneralInfoArray.getString("firstName")+" "+userGeneralInfoArray.getString("lastName"));
                        images.add(R.drawable.ic_launcher);
                    }

                    System.out.println("firstName="+firstLastNames);
                    rowItems = new ArrayList<RowItem>();

                    if(firstLastNames.size() == 0){
                        firstLastNames.add("No Results");
                        images.add(1);
                    }
                    String[] images=new String[]{"http://lh6.ggpht.com/K9UV3dTXSNPk9bGvshrOPPwIl7ExDUEpG2SzdOv8tGBqX4QFpyLRWgvFiyjZ29LNfrBMQPiTVdy14b7eOrodWsLNJDl4zGQ","http://lh6.ggpht.com/K9UV3dTXSNPk9bGvshrOPPwIl7ExDUEpG2SzdOv8tGBqX4QFpyLRWgvFiyjZ29LNfrBMQPiTVdy14b7eOrodWsLNJDl4zGQ","http://theopentutorials.com/wp-content/uploads/totlogo.png"};
                    for (int i = 0; i < firstLastNames.size(); i++) {
                        RowItem item = new RowItem(images.get(i), null, firstLastNames.get(i));
                        rowItems.add(item);
                    }

                    listView = (ListView) findViewById(R.id.list);
                    CustomListViewAdapter adapter = new CustomListViewAdapter(BdaylistActivity.this,
                            R.layout.list_item, rowItems);
                    listView.setAdapter(adapter);
                    listView.setOnItemClickListener(BdaylistActivity.this);
                    m_ProgressDialog.dismiss();

                } catch (JSONException e) {
                    e.printStackTrace();
                }



            }

        }


    }

Hope this might help.

When ever I want to fetch an image from the internet, and have it shown in my android application, I always use "Aquery"

https://code.google.com/p/android-query/

It's fast, easy to use, and very little code is needed to make the magic happen. :-)

In you CustomListViewAdapter class method

public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; RowItem rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
        holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
        holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    holder.txtDesc.setText(rowItem.getDesc());
    holder.txtTitle.setText(rowItem.getTitle());

holder.imageView.setImageResource(rowItem.getImageId());

    return convertView;
}

holder.imageView.setImageResource(rowItem.getImageId());

In this line at first convert url image to bitmap converting image from url to bitmap then set this bitmap in imageview

holder.imageView.setImageBitmap(Bitmap)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM