This code is help you to display image in grideview from server in android.
ViewImage.java
| import android.app.Activity; |
| import android.os.Bundle; |
| import android.view.View; |
| import android.widget.AdapterView; |
| import android.widget.GridView; |
| import android.widget.Toast; |
| public class MyGridView extends Activity { |
| private GridView girGridView; |
| public void onCreate(Bundle savedInstanceState) { |
| super .onCreate(savedInstanceState); |
| setContentView(R.layout.main); |
| girGridView=(GridView) findViewById(R.id.gridView1_bir); |
| girGridView.setAdapter( new ImageAdapter( this )); |
| girGridView.setOnItemClickListener( new AdapterView.OnItemClickListener() { |
| public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) { |
| Toast.makeText(getApplicationContext(), GridViewConfig.getResim_list(). get(position), Toast.LENGTH_SHORT).show(); |
ImageAdapter.java
| import java.io.InputStream; |
| import android.R.integer; |
| import android.content.Context; |
| import android.graphics.drawable.Drawable; |
| import android.view.View; |
| import android.view.ViewGroup; |
| import android.widget.BaseAdapter; |
| import android.widget.GridView; |
| import android.widget.ImageView; |
| import android.widget.ListAdapter; |
| public class ImageAdapter extends BaseAdapter implements ListAdapter { |
| public ImageAdapter(Context context) { |
| GridViewConfig.addImageUrls(); |
| return GridViewConfig.getResim_list().size(); |
| public Object getItem( int position) { |
| return GridViewConfig.getResim_list().get(position); |
| public long getItemId( int position) { |
| public View getView( int position, View convertView, ViewGroup parent) { |
| imageView= new ImageView(context); |
| imageView.setLayoutParams( new GridView.LayoutParams( 100 , 100 )); |
| imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); |
| imageView.setPadding( 5 , 5 , 5 , 5 ); |
| imageView=(ImageView)convertView; |
| imageView.setImageDrawable(LoadImageFromURL(GridViewConfig. getResim_list().get(position))); |
| private Drawable LoadImageFromURL(String url) |
| InputStream is = (InputStream) new URL(url).getContent(); |
| Drawable d = Drawable.createFromStream(is, "src" ); |
main.xml
<?xml version= "1.0" encoding= "utf-8" ?> |
| android:layout_width= "fill_parent" |
| android:layout_height= "fill_parent" |
| android:orientation= "horizontal" > |
| android:layout_height= "wrap_content" |
| android:layout_width= "match_parent" |
| android:id= "@+id/gridView1_bir" > |
GridViewConfig.java
| import java.util.ArrayList; |
| public class GridViewConfig { |
| private static ArrayList<String> resim_list= new ArrayList<String>(); |
| public static ArrayList<String> getResim_list() { |
| public static void setResim_list(ArrayList<String> resim_list) { |
| GridViewConfig.resim_list = resim_list; |
| public static void addImageUrls(){ |
// Here you have to specify your image url path
Note : Also you have to get permission for the INTERNET in the AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.