I have a Cursor Adapter and I want to display a button and a TextView only if the value of a field (accessed from cursor) is between 1 and 4, if not, this views are removed.
So, I created a LayoutFile, with this views, and in CursorAdapter, I check if the field accessed from cursor is between 1 and 4, I remove the Views, otherwise, I add to the Layout:
class Accounts_List_CursorAdapter extends CursorAdapter{
//
Context context;
//
public Accounts_List_CursorAdapter(Context context, Cursor c) {
super(context, c, 0);
this.context = context;
}
@Override
//
//Inflate layout of the rows
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.row_list_accounts_data, parent, false);
}
@Override
//
//Set data and set changes to the row
public void bindView(View view, final Context context, Cursor cursor) {
//
//Find the elements
RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.layoutRowListAccountsData);
TextView tvAccountsName = (TextView) view.findViewById(R.id.tvAccountsName);
TextView tvAccountsInitValue = (TextView) view.findViewById(R.id.tvAccountsInitValue);
TextView tvAccountsType = (TextView) view.findViewById(R.id.tvAccountsType);
//
//Get data from cursor
final int accountId = cursor.getInt(cursor.getColumnIndexOrThrow(0));
final String accountsName = cursor.getString(cursor.getColumnIndexOrThrow(1));
final String accountsCurrValue = cursor.getString(cursor.getColumnIndexOrThrow(2));
final String accountsInitValue = cursor.getString(cursor.getColumnIndexOrThrow(3));
final String accountsType = cursor.getString(cursor.getColumnIndexOrThrow(4));
//
tvAccountsName.setText(accountsName);
tvAccountsCurrValue.setText("Current Value = " + accountsCurrValue);
//
if ((accountId >= 1) && (accountId <= 4)){
try {
relativeLayout.removeView(view.findViewById(R.id.cmdEditThisAccount));
relativeLayout.removeView(view.findViewById(R.id.tvAccountsInitValue));*
} catch (Exception e) {
e.printStackTrace();
}
}
else{
//
relativeLayout.addView(view.findViewById(R.id.cmdEditThisAccount));
relativeLayout.addView(view.findViewById(R.id.tvAccountsInitValue));
//
tvAccountsInitValue.setText("Init Value = " + accountsInitValue);
//
Button cmdEditThisAccount = (Button) view.findViewById(R.id.cmdEditThisAccount);
cmdEditThisAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, UpdateAccount.class);
context.startActivity(intent);
}
});
}
}
}
The problem is: When I run this code, it comes out this message: Java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
What am I doing wrong, or there is another way to hide and show dynamically the TextView and Button from Layout, according with the data returned by cursor?
Thanks in Advance!
Use View.setVisibility(FLAG)
i.e. view.findViewById(R.id.cmdEditThisAccount).setVisibility(View.GONE)