毎分データを同期するサービスがあります。データが変更/作成されると、ListView を更新するためにアクティビティにブロードキャストを送信します。
ビューを更新するには、ビューを削除して正しい値で再作成したいのですが、その位置がわからず、textViews のコンテンツしか持っていません。
アクティビティ :
public void onCreate{
...
this.registerReceiver(this.updateListReceiver, new IntentFilter(
"com.android.updatelist"));
}
BroadcastReceiver updateListReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String id = intent.getExtras().getString("AccountId");
String name = intent.getExtras().getString("AccountName");
boolean isNew = intent.getExtras().getBoolean("isNew");
if (isNew) {
Account accountInList = new Account(id, name, null, null, null,
null);
adapter.add(accountInList);
} else {
}
}
};
サービス :
Intent intent = new Intent();
intent.setAction("com.android.updatelist");
intent.putExtra("AccountId",
updatedAccount.getAccountId());
intent.putExtra("AccountName",
updatedAccount.getName());
intent.putExtra("isNew", true);
sendBroadcast(intent);
アダプター:
public class AccountNameListAdapter extends ArrayAdapter<Account> {
private final Context context;
private final int layoutResourceId;
private final ArrayList<Account> data;
private final TableLayout table;
private final TextView tableName;
private final TextView tableIndustry;
private final TextView tablePhone;
private final TextView tableWebsite;
final AccountBDD accountBdd;
public AccountNameListAdapter(Context context, int layoutResourceId,
ArrayList<Account> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
table = (TableLayout) ((Activity) context)
.findViewById(R.id.tableLayout);
tableName = (TextView) ((Activity) context).findViewById(R.id.NameText);
tableIndustry = (TextView) ((Activity) context)
.findViewById(R.id.IndustryText);
tablePhone = (TextView) ((Activity) context)
.findViewById(R.id.PhoneText);
tableWebsite = (TextView) ((Activity) context)
.findViewById(R.id.WebsiteText);
accountBdd = SQLiteApplication.getAccountBdd();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
AccountHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
holder = new AccountHolder();
convertView.setClickable(true);
convertView.setFocusable(true);
holder.txtName = (TextView) convertView.findViewById(R.id.nom);
holder.txtId = (TextView) convertView.findViewById(R.id.id);
convertView.setTag(holder);
} else {
holder = (AccountHolder) convertView.getTag();
}
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
table.setVisibility(0);
String id = data.get(position).getAccountId();
Account displayAccount = accountBdd.getAccountWithAccountID(id);
tableName.setText(displayAccount.getName());
tableIndustry.setText(displayAccount.getIndustry());
tablePhone.setText(displayAccount.getPhone());
tableWebsite.setText(displayAccount.getWebsite());
}
});
holder.txtName.setText(data.get(position).getName());
holder.txtId.setText(data.get(position).getAccountId());
ImageButton img = (ImageButton) convertView.findViewById(R.id.check);
img.setTag(position);
return convertView;
}
static class AccountHolder {
TextView txtName;
TextView txtId;
}
}
どうすればいいですか?