Update Custom ListView in Android
I have a custom list view in which I show name,date and time for each list item. I have an Adapter which extends BaseAdapter. I want to update this whenever the data in my database changes. Here is my code :
public class UpcomingReminders extends Activity {
private Button addReminderButton;
public static DataHelper dh;
private ListView lv;
private DataSetObserver observer;
private EfficientAdapter adapter;
private static class EfficientAdapter extends BaseAdapter implements Observer {
private LayoutInflater mInflater;
private String date;
private String time;
//This list has the data to be shown in the listview
private List <Reminder> upcomingReminders;
//not sure how to use these
private final DataSetObservable mDataSetObservable = new DataSetObservable();
public void registerDataSetObserver(DataSetObserver observer) {
mDataSetObservable.registerObserver(observer);
}
public void notifyDataSetChanged() {
mDataSetObservable.notifyChanged();
}
public EfficientAdapter(Context context) {
//This will populate the list to be shown
upcomingReminders = dh.selectAllUpcoming();
mInflater = LayoutInflater.from(context);
}
public int getCount() {
// TODO Auto-generated method stub
return upcomingReminders.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.upcominglistview, null);
holder = new ViewHolder();
holder.reminderName = (TextView) convertView.findViewById(R.id.nameUpcoming);
holder.reminderDate = (TextView) convertView.findViewById(R.id.dateUpcoming);
holder.addedBy = (TextView) convertView.findViewById(R.id.AddedByUpcoming);
holder.reminderTime = (Text开发者_C百科View) convertView.findViewById(R.id.timeUpcoming);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
String str = upcomingReminders.get(position).getReminderName();
holder.reminderName.setText(str);
Calendar d = Calendar.getInstance();
d = upcomingReminders.get(position).getReminderDateAndTime();
date = ""+d.get(Calendar.MONTH)+"/"+d.get(Calendar.DAY_OF_MONTH)+"/"+d.get(Calendar.YEAR);
holder.reminderDate.setText(date);
time = ""+d.get(Calendar.HOUR_OF_DAY)+":"+d.get(Calendar.MINUTE);
holder.reminderTime.setText(time);
holder.reminderName.setOnClickListener(new textViewClickListener(position));
holder.reminderDate.setOnClickListener(new textViewClickListener(position));
return convertView;
}
class textViewClickListener implements OnClickListener {
int posit;
public textViewClickListener( int pos)
{
this.posit = pos;
}
public void onClick(View v) {
{// you can write the code what happens for the that click and
// you will get the selected row index in position
Bundle b = new Bundle();
b.putString("title", upcomingReminders.get(posit).getReminderName());
b.putString("date", date);
b.putString("time", time);
try {
Intent myIntent = new Intent();
Context ctx = v.getContext();
myIntent.setClass(ctx, ListItemDetails.class);
myIntent.putExtras(b);
ctx.startActivity(myIntent);
} catch (Exception e) {
// TODO: handle exception
Log.d("Activity call error:%s",e.getMessage());
}
}
}
}
static class ViewHolder {
TextView reminderName;
TextView reminderDate;
TextView reminderTime;
TextView addedBy;
}
public void update(Observable observable, Object data) {
// TODO Auto-generated method stub
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.dh = new DataHelper(this);
setContentView(R.layout.upcoming);
lv = (ListView)findViewById(R.id.list);
adapter = new EfficientAdapter(this);
lv.setAdapter(adapter);
//---------------------------------------
this.addReminderButton = (Button)findViewById(R.id.AddButton);
this.addReminderButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent();
i.setClass(v.getContext(),AddReminderInfo.class);
startActivity(i);
}
});
}
@Override
protected void onResume(){
super.onResume();
if(Globals.isReminderOK){
Globals.isReminderOK = false;
Log.i("ReminderID",""+Globals.reminderID);
dh.updateRow(Globals.reminderID,1);
dh.selectAllUpcoming();
adapter.notifyDataSetChanged();
}
}
}
After making some changes, I update the db using dh.upDateRow. This will delete a row which is currently shown in the list. How can I update the listview to show this change?
Thanks a lot.
Have you tried calling adapter.notifyDataSetChanged()
?
精彩评论