Reputation: 483
I'm using a ListView to show data from a SQLite database file on my phone, when the activity starts, it show the data well, but when I open a second activity, the data has to change according to the operation I do in that activity, to implement this change I used the onActivityResult()
which have a RefreshList method when everything is correct in the resultCode.
Here's the code example:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==RESULT_CORRECT ){
if(resultCode==RESULT_OK){
RefreshList();
}
}else{
Toast.makeText(this,"Cancelled Operation",Toast.LENGTH_SHORT).show();
}
}
These are my global variables:
private String LOG_TAG=Credito.class.getSimpleName();
private DatabaseManager DB;
private ListView lstCREDITO;
private List<TarjetasCredito> mTarjetasCredito=new ArrayList<>();
private static TarjetasCredito tarjetasCredito;
private ArrayAdapter<TarjetasCredito> mAdapter;
private static int RESULT_CORRECT=1;
This is the RefreshList code that extends from an AsyncTask to do the work in the background and dont blow up the main UI thread:
private void RefreshList() {
GetDataAsync getDataAsync=new GetDataAsync();
getDataAsync.execute((Void)null);
}
private class GetDataAsync extends AsyncTask<Void,Void,Boolean>{
@Override
protected Boolean doInBackground(Void... params) {
try{
Cursor cursor=null;
DB.SQLSelect="SELECT COUNT(*) FROM "+TABLAS.vistas.visTarjetasCredito+"";
cursor=DB.db.rawQuery(DB.SQLSelect,null);
cursor.moveToFirst();
int cuantos=cursor.getInt(0);
if(cuantos!=0){
mTarjetasCredito.clear();
DB.SQLSelect="SELECT * FROM "+TABLAS.vistas.visTarjetasCredito+"";
cursor=DB.db.rawQuery(DB.SQLSelect,null);
cursor.moveToFirst();
for(int i=0;i<cursor.getCount();i++){
mTarjetasCredito.add(new TarjetasCredito(cursor.getString(0),cursor.getInt(1),cursor.getDouble(2),cursor.getDouble(3),cursor.getInt(4),cursor.getInt(5),cursor.getString(6),cursor.getString(7)));
cursor.moveToNext();
}
DB.db.close();
cursor.close();
return true;
}else{
return false;
}
}catch (Exception ex){
Log.e(LOG_TAG,ex.getMessage());
return false;
}
}
@Override
protected void onPostExecute(Boolean success) {
if(success){
populateListView();
creditoCallback();
}else{
Toast.makeText(getApplicationContext(),"No hay datos.",Toast.LENGTH_SHORT).show();
}
}
}
private void populateListView() {
mAdapter=new MyListAdapter();
lstCREDITO.setAdapter(null);
lstCREDITO.setAdapter(mAdapter);
}
private class MyListAdapter extends ArrayAdapter<TarjetasCredito>{
public MyListAdapter(){
super(getApplicationContext(),R.layout.item_view_credito,mTarjetasCredito);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView=convertView;
if(itemView==null){
itemView=getLayoutInflater().inflate(R.layout.item_view_credito,parent,false);
}
TarjetasCredito tarjetasCredito=mTarjetasCredito.get(position);
TextView numero=(TextView)itemView.findViewById(R.id.txtNUMEROTARJETA);
numero.setText(tarjetasCredito.getNumeroTarjeta());
TextView banco=(TextView)itemView.findViewById(R.id.txtBANCO);
banco.setText(tarjetasCredito.getNombreBanco());
TextView saldo=(TextView)itemView.findViewById(R.id.txtSALDO);
saldo.setText(String.valueOf(tarjetasCredito.getSaldoDisponible()));
return itemView;
}
}
I don't understand why it only works when the activity is started but not when the other activity closes and the method of the onActivityResult and the RefreshList works well. Please help me.
Upvotes: 0
Views: 440
Reputation: 181
First, make sure that your SQL query is returning data.
Second, you need to update the content of your Adapter. Do this by calling clear()
; then add the List
through addAll()
; then call notifyDataSetChanged()
to update the ListView.
private void populateListView() {
if(mAdapter==null) {
mAdapter=new MyListAdapter();
lstCREDITO.setAdapter(null);
lstCREDITO.setAdapter(mAdapter);
}
else {
mAdapter.clear();
mAdapter.addAll(mTarjetasCredito);
mAdapter.notifyDataSetChanged();
}
}
Upvotes: 1
Reputation: 4649
Change the data in mTarjetasCredito and call adapter.notifyDataSetChanged() instead of setting new adapter
Upvotes: 0