カスタム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;
}
}
しかし、私は行全体をクリックしたくありません。 多くの行があり、常に2つのボタンがありますが、どのようにこれらのボタンが異なるのですか?たとえばinfoButton1がLektion1とinfoButton2を呼び出してLektion2を呼び出すため、行のためにクリックしたボタンを知っておく必要があります。 – krackmoe
OnItemClickに関する私の間違い。コードを動作させる最速の方法は、すべてのgetViewでBundleを再作成することをやめることです。たぶんsetTagメソッドを使ってIDをストアすることができます。主な問題は、すべての呼び出しで新しいバンドルを作成しているため、前のバンドルを削除することです(そのため、最新の行だけを取得できます) – momo