2017-09-26 104 views
-5

我需要幫助。我試圖使用滑動從服務器獲取我的圖像,但我不知道如何?請構建我的代碼,以便Glide可以工作或給我任何其他可能的解決方案。我只想用Glide和JSON顯示圖像列表視圖,JSON結果來自我的PHP腳本。除了圖像以外,我能夠獲取其他數據。這是我的代碼。我應該放置滑翔的地方?滑動以在列表視圖中顯示圖像

public class list extends ListActivity { 

     private static final String TAG_POST= "posts"; 
     private static final String TAG_ID="lecID"; 
     private static final String TAG_NAME="lecName"; 
     private static final String TAG_EMAIL="lecEmail"; 
     private static final String TAG_PASS="lecPass"; 
     private static final String TAG_IMAGE="image"; 
     private static final String LECTURER_LIST_URL="http://system.com/list.php"; 

     private JSONArray mComments =null; 
     private ArrayList<HashMap<String,String>> mcommentList; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.list_view); 
      ImageView imageView=(ImageView)findViewById(R.id.lec_Image); 
//upload is the folder to store my image 
      String leciv="http://fypesystem.com/upload/"; 
      Glide.with(this).load(leciv) 
        .thumbnail(0.5f) 
        .into(imageView); 

您的幫助真的很感激。

+0

你可以用添加您的問題JSON響應 –

+0

不能加載文件夾URL到ImageView的 – Kuls

回答

0

更改這些行特別是URL。

ImageView imageView=(ImageView)findViewById(R.id.lec_Image); 
//upload is the folder to store my image 
      String leciv="http://fypesystem.com/upload/9c419dd5659b0af4d14f642f454dcb07.jpg"; 
      Glide.with(this).load(leciv) 
        .thumbnail(0.5f) 
        .into(imageView); 
+0

如果我寫的上傳/ 9c419dd5659b0af4d14f642f454dcb07.jpg它只會獲取相同的圖像? – Lawrence

+0

你必須得到數組列表中的所有Url,然後逐個設置它們。告訴我你的json。 – Champandorid

+0

已經顯示在我上面的代碼中。 – Lawrence

4
public static void LoadImage(Context cx,ImageView imageView,int demoImage,String url) { 
    if (!((Activity) cx).isFinishing()){ 
     Glide.with(cx) 
       .load(url) 
       .diskCacheStrategy(DiskCacheStrategy.SOURCE) 
       .placeholder(demoImage) 
       .into(imageView); 
} 
} 

CX爲背景demoImage是圖像表示,直到你的形象是負載和網址,你從JSON獲得的圖像路徑。

+0

嗨@Omer Hayat,我應該在哪裏聲明LoadImage()? – Lawrence

+0

這是一種方法,您可以將它放在任何類中並使用它,或者您可以將它複製到當前活動或主要活動中,只需在加載圖像時只需傳遞願望參數即可調用它。 –

+0

但它顯示錯誤LoadImage();不能應用。 :( – Lawrence

2

您必須製作一個自定義適配器,並將該適配器傳遞給您從json創建的列表以及要用於適配器和您的活動環境的佈局。 然後,如果您使用的是listview,而不是listView的getView()函數,請添加此代碼,其中url應該是您存儲在列表中的值。Like this

String url = mcommentList.get(position).get(TAG_IMAGE );

myImageView應該從您通過適配器

Glide.with(上下文) .load(URL) .placeholder(R.drawable.ic_launcher) .into(myImageView)佈局的圖像;

可以通過這個鏈接,幫助

https://www.caveofprogramming.com/guest-posts/custom-listview-with-imageview-and-textview-in-android.html

https://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial