2014-10-07 51 views
0

我試圖實現水平ListView。在這個活動中,我有一個ImageView和一個水平ListView。如果我從該水平ListView中選擇任何圖像,該圖像將顯示在水平ListView上方。默認情況下,第一個圖像顯示在該ImageView上。在這裏,圖像不顯示在ImageView的:不在水平列表視圖中顯示圖像上的圖像

String Imagefile ="http://www.example.com/wp-content/uploads/2014/10/Screenshot_2014-10-07-11-52-52-1412678971.png"; 
selectedImage = (ImageView) findViewById(R.id.imageView1); 
imageLoader=new ImageLoader(DetailPage.this.getApplicationContext()); 
imageLoader.DisplayImage(Imagefile, selectedImage); 

現在的圖像顯示在ImageView的很好。但是如果我試圖設置動態圖像,圖像不會顯示在ImageView上。

GalleryImageAdapter adapter = new GalleryImageAdapter(DetailPage.this, categories); 
gallery.setAdapter(adapter); 
String Imagefile =categories[0].toString(); 
selectedImage = (ImageView) findViewById(R.id.imageView1); 
//imageLoader=new ImageLoader(DetailPage.this.getApplicationContext()); 
//imageLoader.DisplayImage(Imagefile, selectedImage); 
gallery.setOnItemClickListener(new OnItemClickListener() { 
public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
System.out.println("Selected-Image"+" "+categories[position].toString()); 
    //imageLoader=new ImageLoader(DetailImage.this.getApplicationContext()); 
    // imageLoader.DisplayImage(categories[position].toString(), selectedImage); 

      } 
     }); 
     } 
     class GalleryImageAdapter extends ArrayAdapter<String> 
    { 
    private final Context context; 
     private final String[] categories; 
     public ImageLoader imageLoader; 
     public GalleryImageAdapter(Context context, String[] categories) { 
     super(context, R.layout.rowlayout, categories); 
     this.context = context; 
     this.categories = categories; 
     imageLoader=new ImageLoader(context.getApplicationContext()); 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View i = inflater.inflate(R.layout.rowlayout, parent, false); 
     ImageView imageView = (ImageView) i.findViewById(R.id.icon); 
     // imageLoader=new ImageLoader(context.getApplicationContext()); 
     imageLoader.DisplayImage(categories[position], imageView); 
     System.out.println("Image"+" "+categories[position]); 
     return i; 
    } 
    }  

但我正在逐漸上的logcat的網址:

10-07 16:46:35.559: I/System.out(1105): default-Image "http://www.example.com/wp-content/uploads/2014/10/Screenshot_2014-10-07-11-52-52-1412678971.png" 
10-07 17:22:50.009: I/System.out(3607): Image http://example.com/wp-content/uploads/2014/10/Screenshot_2014-10-07-11-52-52-1412678971.png 

什麼是錯在我的代碼?

編輯:

是的,我已經改變了我的代碼,如:

 GalleryImageAdapter adapter = new GalleryImageAdapter(DetailImage.this, R.layout.rowlayout,categories); 

     class GalleryImageAdapter extends ArrayAdapter<String> 
     { 
     private final Context context; 
     private final String[] categories; 
     public ImageLoader imageLoader; 
     private int resourceId; 
     public GalleryImageAdapter(Context context, int resourceId,String[] categories) { 
     super(context, R.layout.rowlayout, categories); 
     this.context = context; 
     this.resourceId = resourceId; 
     this.categories = categories; 
     imageLoader=new ImageLoader(context.getApplicationContext()); 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View i = inflater.inflate(resourceId, parent, false); 

但現在仍然沒有顯示在ImageView的圖像。請驗證它。

編輯:

我需要動態數據精確的輸出像下面的教程:

http://www.learn-android-easily.com/2013/07/android-gallery-view-example.html

+0

檢查你的鏈接無法使用HTTP:// WWW。 example.com/wp-content/uploads/2014/10/Screenshot_2014-10-07-11-52-52-1412678971.png – 2014-10-07 11:36:57

+0

@NaveenTamrakar該鏈接僅是樣本一..我使用有效的網址...我們是限制 我們的網址public.that這就是爲什麼我張貼與示例url。 – 2014-10-07 11:38:14

+0

使用此圖片url http://techbeasts.com/wp-content/uploads/2013/08/Android1.jpg – 2014-10-07 11:39:41

回答

1

這裏是它爲我工作的罰款,因爲我不知道這ImageLoader的你用我用通用的圖像加載器和一個簡單的列表視圖修改代碼。

public class MainActivity extends ActionBarActivity { 
ImageView selectedImage; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ListView gallery; 
    gallery = (ListView) findViewById(R.id.lv_horiz); 
    final String[] categories; 
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
      getApplicationContext()).build(); 
    ImageLoader.getInstance().init(config); 
    categories = getResources().getStringArray(R.array.images); 
    GalleryImageAdapter adapter = new GalleryImageAdapter(
      getApplicationContext(), getResources().getStringArray(
        R.array.images)); 
    gallery.setAdapter(adapter);   
    selectedImage = (ImageView) findViewById(R.id.image); 
    gallery.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, 
       int position, long id) { 
      System.out.println("Selected-Image" + " " 
        + categories[position].toString()); 
      ImageLoader.getInstance().displayImage(
        categories[position].toString(), selectedImage); 

     } 
    }); 
} 

}

班適配器:

class GalleryImageAdapter extends ArrayAdapter<String> { 
private final Context context; 
private final String[] categories; 
public ImageLoader imageLoader; 

public GalleryImageAdapter(Context context, String[] categories) { 
    super(context, R.layout.rowlayout, categories); 
    this.context = context; 
    this.categories = categories; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
    View i = inflater.inflate(R.layout.rowlayout, parent, false); 
    ImageView imginlist = (ImageView) i.findViewById(R.id.iv_inlist); 
    TextView text = (TextView) i.findViewById(R.id.tv_name); 
    ImageLoader.getInstance().displayImage(categories[position], imginlist); 
    System.out.println("Image" + " " + categories[position]); 
    return i; 
} 

}

注:當您單擊列表視圖圖像上這隻會更改頂部的圖像(圖像在列表視圖中),如果你想改變圖像,當圖像從列表視圖中查看時,只需在行爲中聲明一個靜態方法ivity並從適配器getview中調用它。 PS PM我如果你需要樣品prjoect作爲郵編。

+0

是的,它工作正常,如果我使用通用圖像加載程序庫。感謝:) – 2014-10-16 11:13:01

+0

你歡迎@KrishnaVeni :)(y) – 2014-10-17 04:42:34

1

getView(...)

public GalleryImageAdapter(Context context, int resourceId, String[] categories) { 
    super(context, resourceId, categories); 
    this.context = context; 
    this.resourceId = resourceId; 
    this.categories = categories; 
    imageLoader=new ImageLoader(context.getApplicationContext()); 
} 

註釋掉那些創建內部構造ImageLoader's對象內線onItemClick

Activity,你爲GalleryImageAdapter通佈局創建對象paramater這樣

GalleryImageAdapter adapter = new GalleryImageAdapter(DetailPage.this, R.layout.rowlayout, categories); 

在代替getView(...)R.layout.rowlayout使用resourceId

PS:Public Constructors of ArrayAdapter

+0

是的,我改變了你的邏輯..但圖像沒有顯示。 – 2014-10-07 12:18:04

+0

可以請你用最新代碼更新你的問題 – kId 2014-10-07 12:37:01

+0

我已更新我的代碼 – 2014-10-07 12:52:28

0

嘗試改變這一行

View i = inflater.inflate(R.layout.rowlayout, parent, false); 

View i = inflater.inflate(R.layout.rowlayout, null); 
1

還好這是林思考的問題是在你的ImageLoader的class..try這您的主要活動或全球活動... 複製並粘貼此代碼非常

DisplayImageOptions options = new DisplayImageOptions.Builder() 
    .cacheInMemory() 
    .cacheOnDisc() 
    .imageScaleType(ImageScaleType.EXACTLY)  
    .build(); 
    // Load and display image 
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(c) 
    .defaultDisplayImageOptions(options) 
    .build(); 
    ImageLoader.getInstance().init(config); 

然後在數組構造拆除和更換 - (這是舊constructor-(你的))

public GalleryImageAdapter(Context context, int resourceId,String[] categories) { 
    super(context, R.layout.rowlayout, categories); 
    this.context = context; 
    this.resourceId = resourceId; 
    this.categories = categories; 
    imageLoader=new ImageLoader(context.getApplicationContext()); //remove this line 
    } 

現在你有─(editted構造函數)

public GalleryImageAdapter(Context context, int resourceId,String[] categories) { 
    super(context, R.layout.rowlayout, categories); 
    this.context = context; 
    this.resourceId = resourceId; 
    this.categories = categories; 
    } 

然後回落到您的getview並做到這一點 - (您可以複製粘貼此更換getview)

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View i = inflater.inflate(R.layout.rowlayout, parent, false); 
    ImageView imageView = (ImageView) i.findViewById(R.id.icon); 
    ImageLoader.getInstance().displayImage(categories[position], imageView); 
    System.out.println("Image"+" "+categories[position]); 
    return i; 
} 

注意:如果顯示選項沒有傳遞給ImageLoader.displayImage(...)方法,那麼將使用默認的Display Configuration Options(ImageLoaderConfiguration.defaultDisplayImageOptions(...))。

讓我知道,如果它可以幫助

+0

嘿謝謝你的幫助。它工作正常 – 2014-10-16 11:14:17

+0

至少+1會很好..大聲笑@KrishnaVeni – Elltz 2014-10-16 11:19:17

+0

那麼爲什麼我沒有得到50賞金聲望? @Elltz?她是否必須手動提供給我? :D LOL – 2014-10-17 04:44:22

0

在您的代碼中,您已經評論了創建imageloader對象並將該圖像設置爲selectedImage對象的行。

gallery.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
     System.out.println("Selected-Image"+" "+categories[position].toString()); 
     // imageLoader=new ImageLoader(DetailImage.this.getApplicationContext()); 
     // imageLoader.DisplayImage(categories[position].toString(), selectedImage); 
    } 
}); 

將其更改爲:

gallery.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
     System.out.println("Selected-Image"+" "+categories[position].toString()); 
     imageLoader=new ImageLoader(DetailImage.this.getApplicationContext()); 
     imageLoader.DisplayImage(categories[position].toString(), selectedImage); 
    } 
}); 
+0

已經試過了,但我沒有得到解決方案 – 2014-10-16 11:11:44

0

您需要使用排的網絡ImageView的

你只需要按照這個tutorial