2016-08-11 28 views
0

我已創建卡片視圖。現在我想顯示帶有點擊圖像名稱的TOAST消息。請幫助...哪些圖像添加Android代碼:點擊圖片時,應該通過其圖片名稱顯示。

我的Java代碼(片段)..

public void prepareAlbums(){ 
     int[] covers = new int[]{ 
       R.drawable.album1, 
       R.drawable.album2, 
       R.drawable.album3, 
       R.drawable.album4, 
       R.drawable.album5, 
       R.drawable.album6, 
       R.drawable.album7, 
       R.drawable.album8, 
       R.drawable.album9, 
       R.drawable.album10, 
       R.drawable.album11}; 
     int j=0; 

     for(int i=0;i<=10;i++){ 


      Album a=new Album("Song"+i,i,covers[j]); 
      j++; 
      albumList.add(a); 
     } 
     adapter.notifyDataSetChanged(); 
    } 

我的XML代碼..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <android.support.v7.widget.CardView 
     android:id="@+id/card_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     android:layout_margin="@dimen/card_margin" 
     android:elevation="3dp" 
     card_view:cardCornerRadius="@dimen/card_album_radius"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      > 

      <ImageView 
       android:id="@+id/thumbnail" 
       android:layout_width="match_parent" 
       android:layout_height="@dimen/album_cover_height" 
       android:background="?attr/selectableItemBackgroundBorderless" 
       android:clickable="true" 
       android:onClick="clikii" 
       android:scaleType="fitXY" /> 

      <TextView 
       android:id="@+id/title" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/thumbnail" 
       android:paddingLeft="@dimen/album_title_padding" 
       android:paddingRight="@dimen/album_title_padding" 
       android:paddingTop="@dimen/album_title_padding" 
       android:textColor="@color/album_title" 
       android:textSize="@dimen/album_title" /> 

      <TextView 
       android:id="@+id/count" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@id/title" 
       android:paddingBottom="@dimen/songs_count_padding_bottom" 
       android:paddingLeft="@dimen/album_title_padding" 
       android:paddingRight="@dimen/album_title_padding" 
       android:textSize="@dimen/songs_count" /> 

      <ImageView 
       android:id="@+id/overflow" 
       android:layout_width="@dimen/ic_album_overflow_width" 
       android:layout_height="@dimen/ic_album_overflow_height" 
       android:layout_alignParentRight="true" 
       android:layout_below="@id/thumbnail" 
       android:layout_marginTop="@dimen/ic_album_overflow_margin_top" 
       android:scaleType="centerCrop" 
       android:src="@drawable/ic_dots" /> 

     </RelativeLayout> 

    </android.support.v7.widget.CardView> 

</LinearLayout> 

主要活動代碼,我用我的OnClick方法。 ..

public void clikii(View view){ 
     TextView as=(TextView)findViewById(R.id.title); 
     String qq=as.getText().toString(); 


      Toast.makeText(MainActivity.this,qq, Toast.LENGTH_SHORT).show(); 


    } 

enter image description here

當我用這個代碼僅第一image_title將顯示所有圖像..

我想從圖像獲取的名稱,並顯示在土司的短信..請人幫助..

+0

你可以分享設置點擊監聽器的代碼嗎? – Shaishav

+0

@Shaishav在xml:android:onClick =「clikii」 – X3Btel

回答

0
Try This 
    public void clikii(View view){ 
    TextView as=(TextView)findViewById(R.id.title); 
      as.setText("yourImageNAme"); 
    String qq=as.getText().toString(); 


     Toast.makeText(MainActivity.this,qq, Toast.LENGTH_SHORT).show(); 


}   
+0

不工作...第一個圖像名稱顯示所有圖像.. :( –

+0

getall圖像名稱在數組列表中,並獲得相應的圖像名稱,點擊位置並設置爲文本視圖和烤麪包 – YUVRAJ

+0

我已將Allready中的所有圖像設置爲一個ARRAY,稱爲covers(如代碼所示)。如何使用點擊位置代碼 –

0

試試這個代碼:

public void clikii(View view){ 
     ViewGroup parent = (ViewGroup) view; 
     TextView as = (TextView) parent.findViewById(R.id.title); 
     String qq = as.getText().toString(); 

     Toast.makeText(MainActivity.this, qq, Toast.LENGTH_SHORT).show(); 
    } 
+0

還需要在RelativeLayout上設置'clickii'而不是ImageView – X3Btel

+0

謝謝你.. :) IT工作.. –

+0

但它只在單擊文本視圖時工作..單擊圖像不工作' –