I make a listview that the data are take from sqlite now i want to make it will sort by name or location using radiobutton
this is my code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = new DatabaseHelper(this);
RadioButton rbname = (RadioButton)findViewById(R.id.radio0);
RadioButton rblocation = (RadioButton)findViewById(R.id.radio1);
if (rbname.isChecked()){
Cursor = db
.getReadableDatabase()
.rawQuery("SELECT * FROM " + DatabaseHelper.LIST + " ORDER BY " + DatabaseHelper.NAME, null);
ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.row, Cursor,
new String[] {DatabaseHelper.NAME, DatabaseHelper.ADDRESS, DatabaseHelper.GENDER, DatabaseHelper.LOCATION},
new int[] {R.id.txtName, R.id.txtAddress, R.id.txtGender, R.id.txtLocation});
setListAdapter(adapter);
}
else if (rblocation.isChecked()){
Cursor = db
.getReadableDatabase()
.rawQuery("SELECT * FROM " + DatabaseHelper.LIST+ " ORDER BY " + DatabaseHelper.LOCATION, null);
ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.row, Cursor,
new String[] {DatabaseHelper.NAME, DatabaseHelper.ADDRESS, DatabaseHelper.GENDER, DatabaseHelper.LOCATION},
new int[] {R.id.txtName, R.id.txtAddress, R.id.txtGender, R.id.txtLocation});
setListAdapter(adapter);
}
registerForContextMenu(getListView());
}
I already try to put Cursor.requery()
already try to change ListAdapter to BaseAdapter so I can use notifydatasetchanged()
but nothing happen.. my listview still not update
please help me how to solve this problem
I had the same problem long time ago, what helped was to generate a refresh-method. So every time i needed a refresh, I simply called this method.
private void refresh() {
ListView yourListView = getListView();
list = getList(); //this is a List<string>
adapter = new ArrayAdapter<String> (this,R.layout.your_listview_item,list); //this is ArrayAdapter<String>
yourListView.setAdapter(adapter);
yourListView.invalidateViews();
}
This worked good in my ListActivity
What you need to do is put your code into into listeners so that your event activates after the button has been clicked and not the once in onCreate
. In addition getting a listview backed by a cursor to change is done be re-instantiating a cursor and calling changeCursor
on the adapter. I hope the following code makes things clearer:
private RadioButton rbname, rblocation;
private SimpleCursorAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = new DatabaseHelper(this);
rbname = (RadioButton)findViewById(R.id.radio0);
rblocation = (RadioButton)findViewById(R.id.radio1);
Cursor cursor = db
.getReadableDatabase()
.rawQuery("SELECT * FROM " + DatabaseHelper.LIST + " ORDER BY " + DatabaseHelper.NAME, null);
// a default cursor
adapter = new SimpleCursorAdapter(this, R.layout.row, Cursor,
new String[] {DatabaseHelper.NAME, DatabaseHelper.ADDRESS, DatabaseHelper.GENDER, DatabaseHelper.LOCATION},
new int[] {R.id.txtName, R.id.txtAddress, R.id.txtGender, R.id.txtLocation});
setListAdapter(adapter); // do the adapter stuff here to avoid repeating code
initListeners(); // set up the listeners for the radio buttons
registerForContextMenu(getListView());
}
private void initListeners() {
rbname.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
Cursor cursor = db
.getReadableDatabase()
.rawQuery("SELECT * FROM " + DatabaseHelper.LIST + " ORDER BY " + DatabaseHelper.NAME, null);
adapter.changeCursor(cursor); // change cursor in adapter so that listview changes
}
}
});
rblocation.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
Cursor cursor = db
.getReadableDatabase()
.rawQuery("SELECT * FROM " + DatabaseHelper.LIST+ " ORDER BY " + DatabaseHelper.LOCATION, null);
adapter.changeCursor(cursor);
}
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.