2012-02-13 7 views
0

リスト内の各アイテムにテキストビューとその横にイメージビューを含める必要があるListViewを作成したいとします。 textViewをクリックすると別のアクティビティに移動し、imageview/imagebuttonをクリックするとオーディオファイルが再生されます。リストビュー内のTextViewとImageButton上のリスナーを分離する

これは、これまで私が書いたコードの関連部分である:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    mDbHelper = new DbAdapter(this); 
    mDbHelper.open(); 

    temp=mDbHelper.fetchAllNotes(); 
    startManagingCursor(temp); 
    String[] from=new String[]{DbAdapter.KEY_TITLE};           

    int[] to= new int[]{R.id.tvList}; 
    words = new SimpleCursorAdapter(Main.this, R.layout.emptylist, temp, from, to); 
    Main.this.setListAdapter(words); 

    ImageView ivSpeak=(ImageView) findViewById(R.id.ivSpeak); 
    ivSpeak.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(Main.this, "Hi", Toast.LENGTH_SHORT).show(); 
      } 
    });} 

protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    Intent i=new Intent(Main.this,WordDetail.class); 
    i.putExtra(DbAdapter.KEY_ROWID, id); 
    startActivity(i); 
} 

main.xml(全体の活動のレイアウト)ファイルは、次のとおりです。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:background="@drawable/changedback"> 
    <ListView android:id="@android:id/list" android:layout_width="fill_parent" 
     android:layout_height="0dp" android:layout_weight="1" 
     android:textFilterEnabled="true" 
     android:background="@android:color/transparent" 
     android:cacheColorHint="#00000000"> 
    </ListView> </LinearLayout> 

emptylist.xml (リストビュー内の個々の項目のレイアウト)は次のとおりです。

<?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="?android:attr/listPreferredItemHeight" android:background="@android:color/transparent"> 
    <TextView android:id="@+id/tvList" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:singleLine="true" 
     android:ellipsize="marquee" 
     android:layout_alignParentLeft="true" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:layout_centerVertical="true" 
     android:background="@android:color/transparent"/> 
    <ImageView 
     android:id="@+id/ivSpeak" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:src="@drawable/ic_launcher" 
     android:layout_alignParentRight="true" 
     android:focusable="false" 
     android:background="@android:color/transparent"/> 
    </RelativeLayout> 

ivSpeak.setOnClickListener ClickListener()は私にnullポインタの例外を与えています。親切に助けてください。私はAndroidには全く新しいです

答えて

0

最終的に、私はそれを自分で考え出しました。私は、simplecursoradapterを拡張したカスタムアダプタを書き、初期コードのsimplecursorアダプタの代わりにそれを使用しなければなりませんでした。その後、それは働いていますが、正確な理由は何であるかはわかりません。もし誰かがそれを説明することができれば素晴らしいだろう。

これは... fayerthこんにちはお返事のおかげで、新しいカスタムアダプタ

class MySimpleCursorAdapter extends SimpleCursorAdapter{ 

     MySimpleCursorAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to){ 
      super(context, layout, cursor, from, to); 
     } 

     public View getView(int position, View convertView,ViewGroup parent) { 
      View row=super.getView(position, convertView, parent); 

      ImageView ivSpeak=(ImageView) row.findViewById(R.id.ivSpeak); 
      ivSpeak.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        Toast.makeText(Main.this, "Hi", Toast.LENGTH_SHORT).show(); 
        mp=MediaPlayer.create(Main.this, R.raw.correct); 
        mp.start(); 
       } 
      }); 

      return row; 
     } 
    } 
0

すべてのビューにはClickListenerバインディング機能setOnClickListener()があります。私はあなたが次のようなことをすることができると仮定します:

ImageView img = new ImageView(context); 
img.setOnClickListener(new OnClickListener() { 
    public void onClick() { 
     // Your action 
    } 
} 
+0

ためのコードであるが、これは動作しません。私は質問を編集しました。可能であれば見て、私を助けてください。 – ambit

関連する問題