2011-12-10 19 views
0

カスタムSimpleCursorAdapterを作成しました。これは、1行にテキストと2つのボタンが必要なためです。カスタムSimpleCursorAdapterを作成する

ボタンをクリックすると、アクティビティLektionを呼び出す必要があります。 このLektionでは、彼はコールが来たボタンを知っているはずなので、onClickEventでLektionのIDをバンドルとして送信することを考えました。

しかし、このdoesntの仕事、それだけで常に最後のidを私が保存されます。..

私はこれをどのように行うことができますか?

public class LektionenCursorAdapter extends SimpleCursorAdapter{ 
    private Cursor c; 
    private Context context; 
    private Bundle bundle; 

    public LektionenCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { 
     super(context, layout, c, from, to); 
     this.c = c; 
     this.context = context; 
    } 


    public View getView(int pos, View inView, ViewGroup parent){ 
     if(inView == null){ 
      inView = View.inflate(context, R.layout.lektionenoverview_entry, null); 
     } 
     View row = inView; 

     this.c.moveToPosition(pos); 

     String id = this.c.getString(this.c.getColumnIndex("_id")); 
     Log.i("Cursor", id); 
     String description = this.c.getString(this.c.getColumnIndex("Description")); 
     String info = this.c.getString(this.c.getColumnIndex("Info")); 
     String test = this.c.getString(this.c.getColumnIndex("Test")); 

     TextView descriptionTextView = (TextView)row.findViewById(R.id.description); 
     Button infoButton = (Button)row.findViewById(R.id.infoButton); 
     Button testButton = (Button)row.findViewById(R.id.testButton); 

     bundle = new Bundle(); 
     bundle.putString("LektionNummer", id); 

     descriptionTextView.setText(id+". "+description); 

     if(info != null && info.length() > 0){   
      infoButton.setOnClickListener(new OnClickListener() {   
      public void onClick(View v) { 
       Intent intent = new Intent(context, Lektion.class); 
       intent.putExtras(bundle); 
       context.startActivity(intent);     
      } 
     }); 

     } 
     if(test != null && test.length() > 0){ 

     } 
     return row; 
    } 

} 

答えて

0

あなたは2本のラインとボタンを表示したいので、CursorAdapterを使用する必要はありませんが、あなたは他のシンプルなアダプタを使用することができます。

ビューまたはボタンをクリックしたときではなく、ビューごとにgetViewメソッドが複数回呼び出されます。バンドルを毎回再インスタンス化しています。これは、最後のgetView呼び出しのIDを保存するだけです(すべての呼び出しが前の呼び出しを破棄するため)。また、すべての呼び出しで新しいOnClickListenerも作成しています。

おそらく、OnItemClickListenerを使用し、getView()メソッドの外部にインスタンスを1つだけ作成したいとします。

+0

しかし、私は行全体をクリックしたくありません。 多くの行があり、常に2つのボタンがありますが、どのようにこれらのボタンが異なるのですか?たとえばinfoButton1がLektion1とinfoButton2を呼び出してLektion2を呼び出すため、行のためにクリックしたボタンを知っておく必要があります。 – krackmoe

+0

OnItemClickに関する私の間違い。コードを動作させる最速の方法は、すべてのgetViewでBundleを再作成することをやめることです。たぶんsetTagメソッドを使ってIDをストアすることができます。主な問題は、すべての呼び出しで新しいバンドルを作成しているため、前のバンドルを削除することです(そのため、最新の行だけを取得できます) – momo

関連する問題