各行がcheckBox、TextView、およびButtonで構成されるlistViewがあります。ボタンはGONEに設定されています。ユーザーが textBox を選択すると、ボタンが表示されるようになります。ユーザーはこのボタンをクリックして行を削除します。
私は動作するcheckBoxのリスナーを実装しましたが、うまくいかないのは、適切なdeleteButtonが表示されるようにする方法です。私の現在のコードでは、ランダムなボタンが表示されます。
私は何をすべきか?チュートリアルまたはコード フラグメントへのポイントは非常に役立ちます。
public class DeleteCampaignCustomCursorAdapater extends CursorAdapter {
protected static final String TAG = null;
DatabaseHelper myDbHelper;
protected SQLiteDatabase db;
private final LayoutInflater mInflater;
protected ListView mListView;
TextView merchantNameView;
Button deleteButton;
CheckBox selectForDelete;
//ImageView moveButton;
public DeleteCampaignCustomCursorAdapater(Context context, Cursor c) {
super(context, c);
mInflater = LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, final Cursor cursor) {
myDbHelper = new DatabaseHelper(context); //Links to DatabaseHelper class
merchantNameView = (TextView) view.findViewById(R.id.deleteMerchantNameView);
deleteButton = (Button) view.findViewById(R.id.deleteButton);
selectForDelete = (CheckBox) view.findViewById(R.id.deleteCheckBox);
//moveButton = (ImageView) view.findViewById(R.id.moveButton);
merchantNameView.setText(cursor.getString(cursor.getColumnIndex("merchantName")));
final int rowID = cursor.getInt(cursor.getColumnIndex("_id"));
//final long rowID = mListView.getSelectedItemId();
selectForDelete.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
deleteButton.setVisibility(0);
}
else {
deleteButton.setVisibility(8);
}
}
});
//deleteButton.setOnClickListener(new DeleteEntryOnClicklistener(itemID));
deleteButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(rowID > 0) {
//Log.d(TAG, "Button Click. rowID = "+rowID);
myDbHelper.deleteRecordWithID(rowID);
cursor.requery();
notifyDataSetChanged();
}
}
});
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.delete_campaign_row_layout, null);
}
}
編集
バラクのアドバイスによる私の新しいコード。最初のバージョンは機能しましたが、行が削除されると、ビューが更新されるとその下の行がチェックされます。cursorOutOfBoundsException
これで、getView() でa を取得します。
public class DeleteCampaignCustomCursorAdapater extends CursorAdapter {
protected static final String TAG = null;
DatabaseHelper myDbHelper;
protected SQLiteDatabase db;
private final Activity context;
private LayoutInflater mInflater;
protected ListView mListView;
private static Cursor c;
public static int[] checked;
TextView merchantNameView;
Button deleteButton;
CheckBox selectForDelete;
//ImageView moveButton;
public DeleteCampaignCustomCursorAdapater(Context context, Cursor c) {
super(context, c);
this.context = (Activity) context;
mInflater = LayoutInflater.from(context);
DeleteCampaignCustomCursorAdapater.c = c;
initializeChecked();
}
@Override
public void bindView(View view, Context context, final Cursor cursor) {
/*myDbHelper = new DatabaseHelper(context); //Links to DatabaseHelper class
merchantNameView = (TextView) view.findViewById(R.id.deleteMerchantNameView);
deleteButton = (Button) view.findViewById(R.id.deleteButton);
//selectForDelete = (CheckBox) view.findViewById(R.id.deleteCheckBox);
//moveButton = (ImageView) view.findViewById(R.id.moveButton);
merchantNameView.setText(cursor.getString(cursor.getColumnIndex("merchantName")));
final int rowID = cursor.getInt(cursor.getColumnIndex("_id"));
//final long rowID = mListView.getSelectedItemId();
if (checked[rowID] == 0) {
selectForDelete.setChecked(false);
} else {
selectForDelete.setChecked(true);
}
selectForDelete.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// Make deleteButton appear
RelativeLayout ll = (RelativeLayout) buttonView.getParent();
deleteButton = (Button) ll.findViewById(R.id.deleteButton);
checked[rowID] = 1;
deleteButton.setVisibility(0);
}
else {
//Hide deleteButton
RelativeLayout ll = (RelativeLayout) buttonView.getParent();
deleteButton = (Button) ll.findViewById(R.id.deleteButton);
checked[rowID] = 1;
deleteButton.setVisibility(8);
}
}
});
deleteButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(rowID > 0) {
//Log.d(TAG, "Button Click. rowID = "+rowID);
myDbHelper.deleteRecordWithID(rowID);
cursor.requery();
notifyDataSetChanged();
initializeChecked(); // re-set the checked array
}
}
}); */
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.delete_campaign_row_layout, null);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
myDbHelper = new DatabaseHelper(context); //Links to DatabaseHelper class
if (convertView == null) {
mInflater = context.getLayoutInflater();
convertView = mInflater.inflate(R.layout.delete_campaign_row_layout, null);
}
merchantNameView = (TextView) convertView.findViewById(R.id.deleteMerchantNameView);
deleteButton = (Button) convertView.findViewById(R.id.deleteButton);
selectForDelete = (CheckBox) convertView.findViewById(R.id.deleteCheckBox);
merchantNameView.setText(c.getString(c.getColumnIndex("merchantName")));
final int rowID = c.getInt(c.getColumnIndex("_id"));
//final long rowID = mListView.getSelectedItemId();
if (checked[position] == 0) {
selectForDelete.setChecked(false);
} else {
selectForDelete.setChecked(true);
}
selectForDelete.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// Make deleteButton appear
RelativeLayout ll = (RelativeLayout) buttonView.getParent();
deleteButton = (Button) ll.findViewById(R.id.deleteButton);
checked[position] = 1;
deleteButton.setVisibility(0);
}
else {
//Hide deleteButton
RelativeLayout ll = (RelativeLayout) buttonView.getParent();
deleteButton = (Button) ll.findViewById(R.id.deleteButton);
checked[position] = 1;
deleteButton.setVisibility(8);
}
}
});
deleteButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(rowID > 0) {
//Log.d(TAG, "Button Click. rowID = "+rowID);
myDbHelper.deleteRecordWithID(rowID);
c.requery();
notifyDataSetChanged();
initializeChecked(); // re-set the checked array
}
}
});
return convertView;
}
public static void initializeChecked() {
checked = new int[c.getCount()];
int i = 0;
while (i < c.getCount()) {
checked[i] = 0;
i++;
}
}
}