LadiobuttonとdistView内の編集を表示する方法は?
-
28-09-2019 - |
質問
ListViewの行内にRadioButtonとEdittextを実装したいと思います。リストを入力するためにArrayAdapterを使用しています。しかし、問題は、ラジオボタンを選択してリストを下にスクロールして再度スクロールするとき、選択されたラジオボタンが選択されていないことです。 Edittextのコンテンツと同じです。スクロールするとテキストが削除されます。
解決
アダプターを確認してください、あなたはおそらく正しく仕事をしていないことです bindView()
. 。もう一度設定する必要があります bindView()
その価値。
私は文を再定式化します、そしてあなたはおそらく理解するでしょう。 newView()
わずか5〜10回のビュー(画面に収まるように)を作成し、他の行に再利用されます。あなたが持っている場合 ListView
200行が入っていると、実際には5〜10回のビューしかありません。有効な変更でビューを更新する必要があります bindView()
. 。後で再利用するには、変更をオブジェクトに保存/保存する必要があります。
他のヒント
問題は、画面を残すすべてのビューが破壊されて、電話のメモリを保存する可能性があることです。電話がこれを行わない場合、1000のエントリを持つリストがデバイスのメモリを埋めます。
そのため、RadioButtonに発生している状態の変更とEdittextに挿入されたテキストは削除され、ユーザーがビューをスクロールアップすると再作成されます。
ラジオボタンの状態とアダプターの編集テキストを保存し、あなたの特別なアイテムのビューを再現する瞬間に状態を再適用する必要があります GetView 方法。
ベースアダプターのgetView関数。 edittext値を保存し、convertViewがnullでない場合、同じものを表示します。
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_row_person, null);
holder = new ViewHolder();
holder.PersonNameView = (TextView) convertView.findViewById(R.id.PersonNameView);
holder.SpendAmount = (EditText) convertView.findViewById(R.id.SpendAmt);
holder.SpendAmount.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
int position2 = holder.SpendAmount.getId();
EditText Caption = (EditText) holder.SpendAmount;
Person per= (Person)holder.SpendAmount.getTag();
//SpendAmount is of Double type
if(s.toString().length()>0){
per.setSpendAmount(Double.parseDouble(s.toString()));
per.setFlag(true);}
else{
Toast.makeText(getApplicationContext(), "Please enter some value", Toast.LENGTH_SHORT).show();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
holder.SpendAmount.setTag(listData.get(position));
convertView.setTag(holder);
} else {
((ViewHolder)convertView.getTag()).SpendAmount.setTag(listData.get(position));
holder = (ViewHolder) convertView.getTag();
}
holder.PersonNameView.setText(listData.get(position).getPersonName());
holder.SpendAmount.setText(listData.get(position).getSpendAmount().toString());
return convertView;
}
class ViewHolder {
TextView PersonNameView;
EditText SpendAmount ;
}