2015-11-27 47 views
0

我試圖讓電影海報長廊,在gallerry用戶點擊的項目,會出現圖像標題時,訪問對象屬性。如何從內部OnItemClickListener

我使用的自定義ArrayAdapter與grideview

MoviePoster類如下鏈接圖片:

public class MoviePoster { 


    String title; 
    int image; 

    public MoviePoster(int image, String title) { 
     this.image = image; 
     this.title = title; 
    } 
    public MoviePoster(){ 

    } 
} 

這裏是MainACtivity和圖像列表。

public class MainActivityFragment extends Fragment { 
    private MovieAdapter movieAdapter; 


    MoviePoster[] posters ={ 
      new MoviePoster(R.drawable.ironman, "ironman"), 
      new MoviePoster(R.drawable.jobs, "jobs"), 
      new MoviePoster(R.drawable.superman,"superman"), 
      new MoviePoster(R.drawable.terminator, "terminator"), 
      new MoviePoster(R.drawable.batman, "batman"), 
      new MoviePoster(R.drawable.linux, "linux"), 
      new MoviePoster(R.drawable.madara, "madara"), 

    }; 


    public MainActivityFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     final View rootView = inflater.inflate(R.layout.fragment_main, container, false); 

     movieAdapter = new MovieAdapter(getActivity(), Arrays.asList(posters)); 
     GridView gridView = (GridView) rootView.findViewById(R.id.MoviesGrid); 
     gridView.setAdapter(movieAdapter); 

     gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 

       Toast.makeText(getActivity(), "need to show image title here???",Toast.LENGTH_LONG).show(); 
      } 
     }); 
     return rootView; 
    } 
} 

我只是不知道如何記得每個項目的圖像標題。誰能幫忙?

+2

'MoviewPoster海報=(MoviewPoster)adapterView.getItemAtPosition(位置);' – Blackbelt

+0

請你解釋一下,我在哪裏可以使用此行代碼? @Blackbelt –

+0

你能證明你的適配器類@Mohamed易卜拉欣 –

回答

2

如何召回圖像標題爲每個項目

不要使用i參數onItemClick方法是在GridView中點擊項目的位置是:

public void onItemClick(AdapterView<?> adapterView, View view, 
               int i, long l) { 
    MoviewPoster poster = posters[i]; 
    String strTitle=poster. title; 
    } 

或者我們也可以做到這一點使用getItemAtPosition方法在意見提出。

+0

謝謝,它的工作原理 –