2016-04-04 97 views
0
import android.content.Context; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.*******.******.app.R; 
import com.*******.******.app.adapter.CustomAdapter; 
import com.*******.******.app.adapter.CustomPromoAdapter; 
import com.*******.******.app.pojo.Campaigns; 
import com.*******.******.app.pojo.ConsumerProfile; 

import org.springframework.web.client.RestTemplate; 

import java.util.ArrayList; 

public class Promo extends AppCompatActivity { 
    private ArrayList<String> imageurls = new ArrayList<>(); 
    private ArrayList<String> imgnames = new ArrayList<>(); 
    GridView gridview; 
    Context context; 
    ArrayList programName; 
    String campid,campImage,campName; 
    TextView name; 
    ImageView images; 
    // public static String[] programNameList = {"AppleMacBook", "HP_note_Book", "LG_NEXUS", "NokiaLumia", "SamsungRT", "SONY_BRAVIA", "Sansui"}; 
    // public static int[] programImages = {R.drawable.apple, R.drawable.hp, R.drawable.nexus, R.drawable.lumia, R.drawable.fridge, R.drawable.tv, R.drawable.tv1}; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_promo); 
     new HttpRequesrPromo().execute(); 
     new HttpLocationPromo().execute(); 
     gridview = (GridView) findViewById(R.id.gridView1); 
     //gv.setAdapter(new CustomPromoAdapter(this,programNameList,programImages)); 
     CustomPromoAdapter adapter = new CustomPromoAdapter(this,imageurls,imgnames); 
     gridview.setAdapter(adapter); 
     name = (TextView) findViewById(R.id.textView1); 
     images = (ImageView) findViewById(R.id.imageView1); 

    } 

    private class HttpRequesrPromo extends AsyncTask<Void, Void, Campaigns> { 

     @Override 
     protected Campaigns doInBackground(Void... params) { 

      try { 

       final String url = "http://myurl"; 


       RestTemplate restTemplate = new RestTemplate(); 
       Campaigns campa = restTemplate.getForObject(url, Campaigns.class); 
       return campa; 

      } catch (Exception e) { 
       Log.e("MainActivity", e.getMessage(), e); 

      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Campaigns campa) { 
      super.onPostExecute(campa); 
      campid = campa.getId(); 
      Log.d("Campaign Idddddd~~~~~~~~", "onPostExecute: " + campid); 

     } 
    } 

    private class HttpLocationPromo extends AsyncTask<Void, Void, Campaigns> { 

     @Override 
     protected Campaigns doInBackground(Void... params) { 
      try { 

       final String url = "http://myurl"; 


       RestTemplate restTemplate = new RestTemplate(); 
       Campaigns locationcampa = restTemplate.getForObject(url, Campaigns.class); 
       return locationcampa; 

      } catch (Exception e) { 
       Log.e("MainActivity", e.getMessage(), e); 

      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Campaigns locationcampa) { 
      super.onPostExecute(locationcampa); 
      campImage=locationcampa.getImage(); 
      campName=locationcampa.getName(); 
      imgnames.add(campName); 
      name.setText(campName); 

      Log.d("ImageUUURRRLLL", "onPostExecute: " + campImage); 
      Log.d("CampNNNAAMMEEE", "onPostExecute: " +campName); 
      imageurls.add(campImage); 



     } 
    } 
} 
  1. 這是我的活動在這裏我將圖像從靜止服務,並在列表中添加他們設置圖像資源到Android ImageView的。
  2. 我有一個GridView與CustomAdapter ImageView和TextView我想將圖像資源設置爲圖像視圖。
  3. 下面是我的適配器.......

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.app.R; 
import com.app.activity.MainActivity; 
import com.app.activity.Promo; 

import java.util.ArrayList; 

/** 
* Created by ns2 on 2/4/16. 
*/ 
public class CustomPromoAdapter extends BaseAdapter{ 
    ImageView imageView; 
    TextView textView; 
    String [] result; 
    Context context; 
    int [] imageId; 
    private static LayoutInflater inflater=null; 
    public CustomPromoAdapter(Promo promo, ArrayList<String> imageurls, ArrayList<String> imgnames) { 
     // TODO Auto-generated constructor stub 
     //result=programNameList; 
     // context=promo; 
     // imageId=programImages; 
     inflater = (LayoutInflater)context. 
       getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    } 

    @Override 
    public int getCount() { 
     return result.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     View view; 
     view=inflater.inflate(R.layout.promo_gridlist,null); 
     /* textView=(TextView)view.findViewById(R.id.textView1); 
     imageView=(ImageView)view.findViewById(R.id.imageView1); 
     imageView.setImageResource(imageId[position]); 
     textView.setText(imageId[position]);*/ 
     view.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(context, "You Clicked " + result[position], Toast.LENGTH_LONG).show(); 
      } 
     }); 
     return view; 
    } 
} 
  1. 這是我的適配器。

如何從REST API

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 

 
    android:orientation="vertical" > 
 

 
    <ImageView 
 
     android:id="@+id/imageView1" 
 
     android:layout_gravity="center" 
 
     android:layout_width="88dp" 
 
     android:layout_height="88dp" 
 
     android:layout_marginTop="5dp" 
 
     android:layout_marginBottom="5dp" 
 
     /> 
 

 
    <TextView 
 
     android:id="@+id/textView1" 
 
     android:layout_gravity="center" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:textSize="15dp" 
 
     android:layout_marginTop="5dp" 
 
     android:layout_marginBottom="5dp" 
 
     android:text="TextView" /> 
 

 
</LinearLayout>

  1. 這是我的佈局與的ImageView和TextView的。

<?xml version="1.0" encoding="utf-8"?> 
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent" 
 
    android:orientation="vertical" 
 

 
    tools:context=".activity.Promo" > 
 

 
    <GridView 
 
     android:id="@+id/gridView1" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignParentLeft="true" 
 
     android:layout_below="@+id/textView1" 
 
     android:numColumns="2" > 
 

 
    </GridView> 
 

 

 

 
</RelativeLayout>

  1. 這是我的主要佈局。
  2. 我有圖像網址在列表中現在我想將這些網址設置爲ImageView。
  3. 我是新來的Android可以任何人幫助我如何解決這個問題。
  4. 感謝他們........實現這一
+0

。利用滑翔的:)如果你願意的話,可以將URL傳遞給它和一個佔位符圖像並坐下來放鬆讓它處理圖像下載和顯示爲你:) –

回答

0

從服務器加載圖片的最佳方式是

Glide.with(mContext) 
     .load(imageUrl) 
     .diskCacheStrategy(DiskCacheStrategy.ALL) 
     .placeholder(R.drawable.default) 
     .error(R.drawable.default) 
     .override(400, 400) 
     .centerCrop() 
     .into(imageView); 

更多遵循這樣的: https://github.com/bumptech/glide

0

最好的辦法是用畢加索庫。

Picasso.with(上下文) .load(URL) .placeholder(R.drawable.user_placeholder) .error(R.drawable.user_placeholder_error) .into(ImageView的);

更多細節

+0

感謝您的支持.... – Rakesh

0

試試這個,

URL photoUrl = new URL(imageUrl); 
mIcon = BitmapFactory.decodeStream(photoUrl.openConnection() .getInputStream()); 

imageView.setImageBitmap(mIcon);