2014-02-18 51 views
0

我有一个Android应用程序,我需要从服务器检索图像,并且必须将它们显示到带有标题的网格视图中。所以请任何人都可以帮助我如何执行此任务机器人。从服务器的网格视图中加载图像

+0

http://sunil-android.blogspot.in/2013/09/lazy-loading-image- download-from.html使用此链接..使用gridview代替listview –

回答

1

这可以通过以下步骤来完成:

  1. 创建AsyncTaskLoader(这里是一个很好的教程http://www.androiddesignpatterns.com/2012/08/implementing-loaders.html)。在那里你可以从服务器加载你的图像。
  2. 例如,通过扩展BaseAdapter,创建自定义适配器来存储图像和创建视图。自己实现所需的功能。
  3. 在此适配器内部创建一个负责添加新数据的函数。一旦完成,请不要忘记拨打电话notifyDataSetChanged()
  4. 将此适配器连接到您的GridView
  5. 将加载的图像推送到您的适配器。

EDIT好的,这是构思的基本证明:

活动(装载机可以在onCreate方法来启动):

public class MyActivity extends Activity implements LoaderManager.LoaderCallbacks<ArrayList<Drawable>> { 

    //other stuff 

    @Override 
    public Loader<ArrayList<Drawable>> onCreateLoader(int id, Bundle args) { 
     return new ImageLoader(this); 
    } 

    @Override 
    public void onLoadFinished(Loader<ArrayList<Drawable>> loader, ArrayList<Drawable> data) { 
     myAdapter.pushData(data); 
    } 

    @Override 
    public void onLoaderReset(Loader<ArrayList<Drawable>> loader) { 

    } 
} 

装载机

public class ImageLoader extends AsyncTaskLoader<ArrayList<Drawable>> { 

    public ImageLoader(Context context) { 
     super(context); 
    } 

    @Override 
    public ArrayList<Drawable> loadInBackground() { 

     //load the stuff 
     return data; 
    } 

    @Override 
    protected void onStartLoading() { 
     if (data != null) { 
      deliverResult(data); 
     } 
     if (takeContentChanged() || data == null) { 
      forceLoad(); 
     } 
    } 
} 

适配器

public class MyAdapter extends BaseAdapter { 

    ArrayList<Drawable> data = new ArrayList<>(); 

    //other functions 

    public void pushData(ArrayList<Drawable> data){ 
     this.data = data; 
     notifyDataSetChanged(); 
    } 

} 
+0

所有的url都是.php文件。我试过这个,但它不适合我。 – Akash

+0

在这种情况下显示你试图做的 – nikis

+0

抱歉,但我刚刚删除了所有代码。可以给我一些示例代码,以便我可以在我的项目中尝试这个。我也无法下载任何从我的服务器的图像。而我只是一个初学者在android编程,所以我有较少的数据库连接知识。 – Akash

2

如果您正在使用URL加载图像从服务器,那么你可以使用毕加索。

对于使用Picasso - >

1)首先在built.gradle添加compile 'com.squareup.picasso:picasso:2.5.2'中的依赖关系。

2)在网格适配器java文件中添加导入"com.squareup.picasso.Picasso;"

3)现在将"Picasso.with(context).load(pImage[position]).into(imageView);"添加到适配器文件(从服务器数据加载网格)中。

compileSdkVersion 23 
buildToolsVersion "23.0.2" 
dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:23.1.1' 
    compile 'com.android.support:design:23.1.1' 
    compile 'com.squareup.picasso:picasso:2.5.2' 
} 

现在适配器文件在getView方法(注po是全局变量):

LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    po = position; 
    View gridView; 

    if (convertView == null) { 
     gridView = inflater.inflate(R.layout.dashboard_inner, null);// set image based on selected text    
    } else { 
     gridView = (View) convertView; 
    } 

    imageView = (ImageView) gridView.findViewById(R.id.grid_item_image); 
    TextView textView1 = (TextView)gridView.findViewById(R.id.grid_item_text1); 
    textView1.setText(pName[position]); 

    TextView textView2 = (TextView) gridView.findViewById(R.id.grid_item_text2); 
    textView2.setText(pPrice[position]); 
    textView2.setPaintFlags(textView2.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); 

    TextView textView3 = (TextView) gridView.findViewById(R.id.grid_item_text3); 
    textView3.setText(pSprice[position]); 

    TextView textView4 = (TextView) gridView.findViewById(R.id.grid_item_text4); 
    textView4.setText(pOffer[position]); 

    Picasso.with(context).load(pImage[position]).into(imageView); 
    return gridView; 
}