2016-06-08 25 views
1

我有應用程序使用Volley庫查看json數據到gridview(圖像&文本)。有一次,我點擊GridView的圖片和文字是傳遞給下一個活動 我可以使用的GridView適配器必須ArrayList的一個圖像和其他文本 這裏是適配器Json圖像傳遞到下一個活動

public class GridViewAdapter extends BaseAdapter implements Filterable { 
//Imageloader to load images 
private ImageLoader imageLoader; 
private ItemFilter mFilter = new ItemFilter(); 
//Context 
private Context context; 

//Array List that would contain the urls and the titles for the images 
private ArrayList<String> images; 
private ArrayList<String> names; 
public GridViewAdapter (Context context, ArrayList<String> images, ArrayList<String> names){ 
    //Getting all the values 
    this.context = context; 
    this.images = images; 
    this.names = names; 
} 

@Override 
public int getCount() { 
    return images.size(); 
} 

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

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

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    //Creating a linear layout 
    LinearLayout linearLayout = new LinearLayout(context); 
    linearLayout.setOrientation(LinearLayout.VERTICAL); 
    //HttpsURLConnection.setFollowRedirects(true); 
    //NetworkImageView 
    NetworkImageView networkImageView = new NetworkImageView(context); 
    //NetworkImageView networkImageView = (NetworkImageView) convertView 
      // .findViewById(R.id.gridimage); 
    //Initializing ImageLoader 
    imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader(); 
    imageLoader.get(images.get(position), ImageLoader.getImageListener(networkImageView, R.drawable.ic_launcher, android.R.drawable.ic_dialog_alert)); 

    //Setting the image url to load 
    networkImageView.setImageUrl(images.get(position),imageLoader); 

    //Creating a textview to show the title 
    TextView textView = new TextView(context); 
    textView.setText(names.get(position)); 

    //Scaling the imageview 
    networkImageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
    networkImageView.setLayoutParams(new GridView.LayoutParams(300,300)); 

    //Adding views to the layout 
    linearLayout.addView(textView); 
    linearLayout.addView(networkImageView); 

    //Returnint the layout 
    return linearLayout; 
} 

@Override 
public Filter getFilter() { 
    // TODO Auto-generated method stub 
    return mFilter; 
} 
    } 
+0

什麼似乎是問題 – Vucko

回答

0

爲你的GridView創建onitemclick上的活動監聽器。一旦網格視圖項目被點擊,您將獲得所選項目的位置。使用所選位置從您的數組列表中選擇圖像,並通過意向

通過添加網格視圖項點擊監聽,這裏面的代碼

String imageurl=yourimagestringarraylist.get(position) 
String txt=yourtextstringarraylist.get(position) 
Intent i= new intent(yourcurrentactivity.this,calling activity.class) 
I.putExtra("imageurl",imageurl); 
I.put extra("text",txt); 
StartActivity(i); 

通過

String image=getIntent.getStringExtra("imageurl",""); 
String text= getIntent.getStringExtra("text",""); 
獲取來自其他活動的圖片和文字
+0

我在哪裏可以設置圖像從字符串圖像 –

0

有2種方式(最簡單): 1.發送文本和img網址(如果圖片尺寸較大) 2.發送文本和位圖(小圖片時)。您可以在適配器收到位圖形式ImageLoader的回調裝載

linearLayout.setOnclickListener(new View.OnclickListener(){ 
    void onClick() { 
    Intent i = new Intent(convertView.getContext(), YourActivity.class); 
    i.putString(key, text); 
    i.putString(key, url); // if big img 
    i.putSerialazible(key, bitmap); // if small image 
    } 
}); 

在另一個活動接受它:

getIntent().getStringExtra(key); 
(Bitmap)getIntent().getSerializableExtra(key); 
+0

我怎樣才能把圖像視圖基於這個值。我可以嘗試,但我可以得到空指針異常 –

+0

你不能把圖像視圖!您可以從視圖(您要放置的位置)獲取drawable,getDrawable返回Bitmap和Bitmap作爲序列化對象可以放置到意圖附加區域。在你的情況,只需等待imageLoader設置圖像查看,執行bitmap = imageView.getDrawable()。比intent.putSerializable(位圖) – once2go

相關問題