我有一个每分钟同步数据的服务。当修改/创建数据时,我会向我的 Activity 发送广播以更新我的 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;
}
}
请问我该怎么办?