I have a service that synchronize data every minute. When a data is modified/created, I send a broadcast to my Activity in order to update my ListView.
To update the view, i want to delete it and recreate with right values but i don’t know its position, i only have the content of its textViews.
Activity :
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 {
}
}
};
Service :
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);
Adapter :
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;
}
}
How can i do please ?
Create a method in the adapter that takes the account_id as an input. It should look like this:
and from the activity that you receive the
BroadcastReceiverjust call this method and you are ok. It is much quicker than you think.UPDATE: The
Accountis an object. The remove does not work that way, because the address memory always changes.Try to find the position of the exact object among the data and try data.remove(position). Something like this, instead of
getAccountByID(int accountid)implement this:and then use the position to remove the exact spot:
Here you are! Now you have a full copy-paste example! I have done it many times, no need to mess up
CursorAdapteror anything like that, pure hard-coded java. I am pretty sure this actually works, if it doesn’t work in your example I will advise you to debug and make sure everything is ok, because I guess there is something else wrong with your code, and not that the adapter is not populated.