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