0
我有一个游标适配器,我想如果不显示一个按钮,并仅当一个场的(来自光标访问)的值是1和4之间一个TextView,根据删除查看的CursorAdapter,这视图被删除。动态添加/使用数据值
所以,我创建一个LayoutFile,与此观点,并在CursorAdapter的,我检查是否从光标访问的字段是1和4之间,我除去视图,否则,我添加到布局:
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);
}
});
}
}
}
的问题是:当我运行这段代码,它出来这个消息:Java.lang.IllegalStateException:指定的孩子已经有一个父。您必须先调用子对象的父对象的removeView()。
我在做什么错误,或者有另一种方法根据游标返回的数据动态地隐藏和显示来自布局的TextView和Button?
在此先感谢!
非常感谢你!你救了我的一天:这么简单,我在我的代码中做了很多事情,它崩溃了......只是改为setVisibility(View.Gone),现在应用程序工作正常! – tdmsoares