2011-12-08 9 views
0

ギャラリーを作成しましたが、今度はコード内で変更したいと思います。ギャラリーの要素は2つのからなり、私は線の数を取得したい。ギャラリー要素を取得する

は、私が試した:

TextView top = (TextView)findViewById(R.id.top); 
top.getLineCount(); 

しかし、このアプリは、誰もがアイデアを持っていますtop.getLineCount();で墜落しましたか? CustomAdapter

 public View getView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater iteminflater = LayoutInflater.from(mContext); 
     View gallaryitem = iteminflater.inflate(R.layout.gallery_item, null); 
     ImageView imgView= (ImageView) gallaryitem .findViewById(R.id.item); 
     imgView.setImageResource(mImageIds[position]); 


     //TextView top = (TextView)convertView.findViewById(R.id.top); 
     //top.getLineCount(); 





     return gallaryitem ; 
    } 

LayoutFile

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:padding="10px" > 

    <ImageView 
    android:id="@+id/item" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:src="@drawable/icon" 
    android:gravity="center" 
    android:background="#4E4E4E"/> 


<TextView 
    android:id="@+id/bottom" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="moremore" 
    android:textSize="27sp" 

    android:layout_marginTop="275dip" 
    android:paddingTop="35dip" 
    android:textColor="#eeeeee" 
    android:background="#60000000" 
    android:textStyle="bold" 
    android:typeface="sans"/> 

<TextView 
    android:id="@+id/top" 
    android:layout_width="fill_parent" 
    android:text="testtesttesttesttesttesttest" 
    android:textSize="22sp" 
    android:layout_marginTop="280dip" 
    android:layout_marginLeft="10dip" 
    android:layout_marginRight="10dip" 
    android:textColor="#eeeeee" 
    android:background="#60ff0000" 
    android:textStyle="bold" 
    android:typeface="sans" 
    android:layout_height="wrap_content"/> 

</RelativeLayout> 
+0

? –

+0

CustomAdapterとLayoutfileを追加します –

+0

クラッシュ時にエラーが発生しました –

答えて

0

getViewメソッドは、あなたはあなたのギャラリーの子供を変更するためのカスタム・アダプターを作成する必要があります。

class MyAdapter extends ArrayAdapter<E>{ 
     public MyAdapter(Context context, int textViewResourceId, E[] objects) { 
      super(context, textViewResourceId, objects); 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      if(convertView== null) convertView = getLayoutInflater().inflate(R.layout.your_layout, null); 

      //Modify contents of your gallery here.. example: 
      TextView top = (TextView)convertView.findViewById(R.id.top); 
      top.getLineCount(); 

      return convertView; 
     } 
    } 

あなたのギャラリーに設定し、それをあなたが通常のアダプタを設定する方法:あなたがあなたのギャラリーでのコードの行のTextViewを入れている

myGallery.setAdapter(new MyAdapter(...)); 
+0

私はこの2行のコードを私のcustomadapterに追加すると、アプリケーションがクラッシュします...なぜアイデアが得られましたか? –

+0

ギャラリーに配置するレイアウトにTextViewがありますか?ビューが見つからない場合は、nullのlineCountを取得しようとしています。 – Jave

+0

CustomAdapterとLayoutfileを追加します –